nu_protocol/ast/
unit.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::{Filesize, FilesizeUnit, IntoValue, ShellError, Span, Value};
use serde::{Deserialize, Serialize};

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

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

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)),
            Unit::Microsecond => Ok(Value::duration(size * 1000, span)),
            Unit::Millisecond => Ok(Value::duration(size * 1000 * 1000, span)),
            Unit::Second => Ok(Value::duration(size * 1000 * 1000 * 1000, span)),
            Unit::Minute => match size.checked_mul(1000 * 1000 * 1000 * 60) {
                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![],
                }),
            },
            Unit::Hour => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60) {
                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![],
                }),
            },
            Unit::Day => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24) {
                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![],
                }),
            },
            Unit::Week => match size.checked_mul(1000 * 1000 * 1000 * 60 * 60 * 24 * 7) {
                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![],
                }),
            },
        }
    }
}