Repository: nushell/nushell
File: crates/nu-protocol/src/ast/unit.rs (Rust)

Code before:
/// enum cases in [`Unit`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Error)]
pub struct ParseUnitError(());

impl fmt::Display for ParseUnitError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "invalid file size or duration unit")
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Unit {
    Filesize(FilesizeUnit),

    // Duration units
    Nanosecond,
    Microsecond,
    Millisecond,
    Second,
    Minute,
    Hour,
    Day,
    Week,
}

/// TODO: something like `Filesize::from_unit` in the future?
fn duration_mul_and_check(size: i64, factor: i64, span: Span) -> Result<Value, ShellError> {
    match size.checked_mul(factor) {
        Some(val) => Ok(Value::duration(val, span)),
        None => Err(ShellError::GenericError {
            error: "duration too large".into(),
            msg: "duration too large".into(),
            span: Some(span),
            help: None,
            inner: vec![],
        }),
    }
}

impl Unit {
    pub fn build_value(self, size: i64, span: Span) -> Result<Value, ShellError> {
        match self {
            Unit::Filesize(unit) => {
                if let Some(filesize) = Filesize::from_unit(size, unit) {
                    Ok(filesize.into_value(span))
                } else {
                    Err(ShellError::GenericError {
                        error: "filesize too large".into(),
                        msg: "filesize too large".into(),
                        span: Some(span),
                        help: None,

Review comment (suggestion):
```suggestion
// TODO: something like `Filesize::from_unit` in the future?
```

Code after:
/// enum cases in [`Unit`].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Error)]
pub struct ParseUnitError(());

impl fmt::Display for ParseUnitError {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "invalid file size or duration unit")
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Unit {
    Filesize(FilesizeUnit),

    // Duration units
    Nanosecond,
    Microsecond,
    Millisecond,
    Second,
    Minute,
    Hour,
    Day,
    Week,
}

// TODO: something like `Filesize::from_unit` in the future?
fn duration_mul_and_check(size: i64, factor: i64, span: Span) -> Result<Value, ShellError> {
    match size.checked_mul(factor) {
        Some(val) => Ok(Value::duration(val, span)),
        None => Err(ShellError::GenericError {
            error: "duration too large".into(),
            msg: "duration too large".into(),
            span: Some(span),
            help: None,
            inner: vec![],
        }),
    }
}

impl Unit {
    pub fn build_value(self, size: i64, span: Span) -> Result<Value, ShellError> {
        match self {
            Unit::Filesize(unit) => {
                if let Some(filesize) = Filesize::from_unit(size, unit) {
                    Ok(filesize.into_value(span))
                } else {
                    Err(ShellError::GenericError {
                        error: "filesize too large".into(),
                        msg: "filesize too large".into(),
                        span: Some(span),
                        help: None,