mirror of
https://github.com/nushell/nushell.git
synced 2024-11-29 03:44:19 +01:00
a little better error handling
This commit is contained in:
parent
61345c48b4
commit
bc9a230e5e
@ -188,16 +188,24 @@ fn groups_to_table(
|
|||||||
record_map.insert("count".to_string(), Value::int(items.len() as i64, span));
|
record_map.insert("count".to_string(), Value::int(items.len() as i64, span));
|
||||||
|
|
||||||
if let Some(sum_col) = maybe_sum_column.clone() {
|
if let Some(sum_col) = maybe_sum_column.clone() {
|
||||||
let (sum_col_name, sum) = sum_celllpath(sum_col, &items, span, true);
|
match sum_celllpath(sum_col, &items, span) {
|
||||||
// add sum
|
Ok((sum_col_name, sum)) => {
|
||||||
record_map.insert(sum_col_name.clone() + "_sum", Value::float(sum, span));
|
// add sum
|
||||||
let avg = if !items.is_empty() {
|
record_map
|
||||||
sum / items.len() as f64
|
.insert(sum_col_name.clone() + "_sum", Value::float(sum, span));
|
||||||
} else {
|
let avg = if !items.is_empty() {
|
||||||
0.0
|
sum / items.len() as f64
|
||||||
};
|
} else {
|
||||||
// add avg
|
0.0
|
||||||
record_map.insert(sum_col_name + "_avg", Value::float(avg, span));
|
};
|
||||||
|
// add avg
|
||||||
|
record_map.insert(sum_col_name + "_avg", Value::float(avg, span));
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
// It seems a little odd to be adding an error to the record
|
||||||
|
record_map.insert("error".to_string(), Value::error(err, span));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Value::record(record_map, span)
|
Value::record(record_map, span)
|
||||||
@ -207,7 +215,7 @@ fn groups_to_table(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sum_celllpath(column: Value, items: &[Value], span: Span, is_sum: bool) -> (String, f64) {
|
fn sum_celllpath(column: Value, items: &[Value], span: Span) -> Result<(String, f64), ShellError> {
|
||||||
if let Value::CellPath { val, .. } = column {
|
if let Value::CellPath { val, .. } = column {
|
||||||
let sum: f64 = items
|
let sum: f64 = items
|
||||||
.iter()
|
.iter()
|
||||||
@ -219,14 +227,12 @@ fn sum_celllpath(column: Value, items: &[Value], span: Span, is_sum: bool) -> (S
|
|||||||
.unwrap_or(0.0)
|
.unwrap_or(0.0)
|
||||||
})
|
})
|
||||||
.sum();
|
.sum();
|
||||||
(val.to_column_name(), sum)
|
Ok((val.to_column_name(), sum))
|
||||||
} else {
|
} else {
|
||||||
eprintln!("sum_col type: {:#?}", column.get_type());
|
Err(ShellError::TypeMismatch {
|
||||||
if is_sum {
|
err_message: format!("Only CellPath's are allowed. Found {}.", column.get_type()),
|
||||||
("sum".to_string(), 0.0f64)
|
span,
|
||||||
} else {
|
})
|
||||||
("avg".to_string(), 0.0f64)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user