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

Code before:
    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,
                        inner: vec![],
                    })
                }
            }
            Unit::Nanosecond => Ok(Value::duration(size, span)),

Review comment (bug):
For @cptpiepmatz 's war on `GenericError` maybe `InvalidValue` or one of the overflow variants could fit this application.

Code after:
    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,
                        inner: vec![],
                    })
                }
            }
            Unit::Nanosecond => Ok(Value::duration(size, span)),