Convert ShellError::DatetimeParseError to named fields (#10991)

# Description

Part of #10700

# User-Facing Changes

None

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

# After Submitting

N/A
This commit is contained in:
Eric Hodel 2023-11-08 04:04:02 -08:00 committed by GitHub
parent d3ec3dc66b
commit 55316a9f27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 12 deletions

View File

@ -288,7 +288,10 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
Value::date(dt, *span)
}
None => Value::error(
ShellError::DatetimeParseError(input.debug_value(), *span),
ShellError::DatetimeParseError {
msg: input.debug_value(),
span: *span,
},
*span,
),
},
@ -298,7 +301,10 @@ fn action(input: &Value, args: &Arguments, head: Span) -> Value {
Value::date(dt, *span)
}
None => Value::error(
ShellError::DatetimeParseError(input.debug_value(), *span),
ShellError::DatetimeParseError {
msg: input.debug_value(),
span: *span,
},
*span,
),
},

View File

@ -79,7 +79,10 @@ fn helper(value: Value, head: Span) -> Value {
}
Value::Date { val, .. } => Value::string(humanize_date(val), head),
_ => Value::error(
ShellError::DatetimeParseError(value.debug_value(), head),
ShellError::DatetimeParseError {
msg: value.debug_value(),
span: head,
},
head,
),
}

View File

@ -121,7 +121,13 @@ fn helper(val: Value, head: Span) -> Value {
parse_date_into_table(n, head)
}
Value::Date { val, .. } => parse_date_into_table(val, head),
_ => Value::error(DatetimeParseError(val.debug_value(), head), head),
_ => Value::error(
DatetimeParseError {
msg: val.debug_value(),
span: head,
},
head,
),
}
}

View File

@ -120,7 +120,13 @@ fn helper(val: Value, head: Span) -> Value {
parse_date_into_table(n, head)
}
Value::Date { val, .. } => parse_date_into_table(val, head),
_ => Value::error(DatetimeParseError(val.debug_value(), head), head),
_ => Value::error(
DatetimeParseError {
msg: val.debug_value(),
span: head,
},
head,
),
}
}

View File

@ -122,7 +122,10 @@ fn helper(value: Value, head: Span, timezone: &Spanned<String>) -> Value {
_to_timezone(dt.with_timezone(dt.offset()), timezone, head)
}
_ => Value::error(
ShellError::DatetimeParseError(value.debug_value(), head),
ShellError::DatetimeParseError {
msg: value.debug_value(),
span: head,
},
head,
),
}

View File

@ -15,13 +15,19 @@ pub(crate) fn parse_date_from_string(
LocalResult::Single(d) => Ok(d),
LocalResult::Ambiguous(d, _) => Ok(d),
LocalResult::None => Err(Value::error(
ShellError::DatetimeParseError(input.to_string(), span),
ShellError::DatetimeParseError {
msg: input.into(),
span,
},
span,
)),
}
}
Err(_) => Err(Value::error(
ShellError::DatetimeParseError(input.to_string(), span),
ShellError::DatetimeParseError {
msg: input.into(),
span,
},
span,
)),
}

View File

@ -157,7 +157,10 @@ fn format_helper(value: Value, formatter: &str, formatter_span: Span, head_span:
}
}
_ => Value::error(
ShellError::DatetimeParseError(value.debug_value(), head_span),
ShellError::DatetimeParseError {
msg: value.debug_value(),
span: head_span,
},
head_span,
),
}
@ -175,7 +178,10 @@ fn format_helper_rfc2822(value: Value, span: Span) -> Value {
}
}
_ => Value::error(
ShellError::DatetimeParseError(value.debug_value(), span),
ShellError::DatetimeParseError {
msg: value.debug_value(),
span,
},
span,
),
}

View File

@ -650,7 +650,7 @@ pub enum ShellError {
/// * "2020-04-12 22:10:57 +02:00"
/// * "2020-04-12T22:10:57.213231+02:00"
/// * "Tue, 1 Jul 2003 10:52:37 +0200""#
#[error("Unable to parse datetime: [{0}].")]
#[error("Unable to parse datetime: [{msg}].")]
#[diagnostic(
code(nu::shell::datetime_parse_error),
help(
@ -663,7 +663,11 @@ pub enum ShellError {
* "Tue, 1 Jul 2003 10:52:37 +0200""#
)
)]
DatetimeParseError(String, #[label("datetime parsing failed")] Span),
DatetimeParseError {
msg: String,
#[label("datetime parsing failed")]
span: Span,
},
/// A network operation failed.
///