mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 12:55:47 +02:00
Spanned<DatetimeFormat> to allow for more precise error
This commit is contained in:
@ -19,7 +19,7 @@ const ALLOWED_COLUMNS: [&str; 10] = [
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
zone_options: Option<Spanned<Zone>>,
|
zone_options: Option<Spanned<Zone>>,
|
||||||
format_options: Option<DatetimeFormat>,
|
format_options: Option<Spanned<DatetimeFormat>>,
|
||||||
cell_paths: Option<Vec<CellPath>>,
|
cell_paths: Option<Vec<CellPath>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,13 +144,16 @@ impl Command for IntoDatetime {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let format_options = call
|
let format_options = call
|
||||||
.get_flag::<String>(engine_state, stack, "format")?
|
.get_flag::<Spanned<String>>(engine_state, stack, "format")?
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map(|fmt| DatetimeFormat(fmt.to_string()));
|
.map(|fmt| Spanned {
|
||||||
|
item: DatetimeFormat(fmt.item.to_string()),
|
||||||
|
span: fmt.span,
|
||||||
|
});
|
||||||
|
|
||||||
let args = Arguments {
|
let args = Arguments {
|
||||||
format_options,
|
|
||||||
zone_options,
|
zone_options,
|
||||||
|
format_options,
|
||||||
cell_paths,
|
cell_paths,
|
||||||
};
|
};
|
||||||
operate(action, args, input, call.head, engine_state.signals())
|
operate(action, args, input, call.head, engine_state.signals())
|
||||||
@ -670,21 +673,20 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
match dateformat {
|
||||||
// match dateformat {
|
None => (),
|
||||||
// None => (),
|
Some(dt) => {
|
||||||
// Some(_) => {
|
return Value::error(
|
||||||
// return Value::error(
|
ShellError::IncompatibleParameters {
|
||||||
// ShellError::IncompatibleParameters {
|
left_message: "got a record as input".into(),
|
||||||
// left_message: "got a record as input".into(),
|
left_span: head,
|
||||||
// left_span: head,
|
right_message: "cannot be used with records".into(),
|
||||||
// right_message:"cannot be used with records".into(),
|
right_span: dt.span,
|
||||||
// right_span: input.span(),
|
},
|
||||||
// },
|
head,
|
||||||
// head,
|
);
|
||||||
// );
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
dbg!(input);
|
dbg!(input);
|
||||||
return merge_record(record, head, *internal_span);
|
return merge_record(record, head, *internal_span);
|
||||||
@ -780,7 +782,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|||||||
|
|
||||||
let parse_as_string = |val: &str| {
|
let parse_as_string = |val: &str| {
|
||||||
match dateformat {
|
match dateformat {
|
||||||
Some(dt_format) => match DateTime::parse_from_str(val, &dt_format.0) {
|
Some(dt_format) => match DateTime::parse_from_str(val, &dt_format.item.0) {
|
||||||
Ok(dt) => {
|
Ok(dt) => {
|
||||||
match timezone {
|
match timezone {
|
||||||
None => {
|
None => {
|
||||||
@ -829,7 +831,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(reason) => {
|
Err(reason) => {
|
||||||
match NaiveDateTime::parse_from_str(val, &dt_format.0) {
|
match NaiveDateTime::parse_from_str(val, &dt_format.item.0) {
|
||||||
Ok(d) => {
|
Ok(d) => {
|
||||||
let dt_fixed =
|
let dt_fixed =
|
||||||
Local.from_local_datetime(&d).single().unwrap_or_default();
|
Local.from_local_datetime(&d).single().unwrap_or_default();
|
||||||
@ -838,7 +840,7 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
|
|||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
Value::error (
|
Value::error (
|
||||||
ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt_format.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },
|
ShellError::CantConvert { to_type: format!("could not parse as datetime using format '{}'", dt_format.item.0), from_type: reason.to_string(), span: head, help: Some("you can use `into datetime` without a format string to enable flexible parsing".to_string()) },
|
||||||
head,
|
head,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user