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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use super::{Kind, Result, Unimplemented};
bitflags!{
    /// - append:  append mode (makes sense only for output; conv=notrunc
    /// suggested)
    ///
    /// - direct:  use direct I/O for data
    ///
    /// - directory:  fail unless a directory
    ///
    /// - dsync:  use synchronized I/O for data
    ///
    /// #### sync   likewise, but also for metadata
    ///
    /// - nonblock:  use non-blocking I/O
    ///
    /// - noatime:  do not update access time
    ///
    /// - nocache:  Request to drop cache.  See also oflag=sync
    ///
    /// - noctty:  do not assign controlling terminal from file
    ///
    /// - nofollow:  do not follow symlinks
    ///
    /// - seek_bytes: treat 'seek=N' as a byte count (oflag only)
    ///
    ///
    pub struct Out: u32 {
        /// append mode (makes sense only for output; conv=notrunc suggested)
        const APPEND = 0x0001;
        /// use direct I/O for data
        const DIRECT = 0x0002;
        /// fail unless a directory
        const DIRECTORY = 0x0004;
        /// use synchronized I/O for data
        const DSYNC = 0x0008;
        /// likewise, but also for metadata
        const SYNC = 0x0010;
        /// use non-blocking I/O
        const NONBLOCK = 0x0020;
        /// do not update access time
        const NOATIME = 0x0040;
        /// Request to drop cache.  See also oflag=sync
        const NOCACHE = 0x0080;
        /// do not assign controlling terminal from file
        const NOCTTY = 0x0100;
        /// do not follow symlinks
        const NOFOLLOW = 0x0200;
    }
}
impl Out {
    /// create a new `flags::Out` by parsing a comma-and-optional-whitespace
    /// separated list of arguments ```
    /// #use dd:opts::flags;
    /// assert_eq!(flags::Out::new("directory,  append  ").unwrap(),
    /// flags::Out::DIRECTORY | flags::In::APPEND); ```
    #[allow(unreachable_patterns)]
    pub fn new(s: &str) -> Result<Self> {
        let mut flags = Self::default();

        for flag in s.split(',').map(str::trim).filter(|s| s.len() > 0) {
            Self::check_if_implemented(flag)?;
            flags |= Self::from_str(flag)?;
        }
        Ok(flags)
    }

    fn from_str(flag: &str) -> Result<Self> {
        Ok(match flag {
            "append" => Self::APPEND,
            "sync" => Self::SYNC,
            "direct" => Self::DIRECT,
            "directory" => Self::DIRECTORY,
            "dsync" => Self::DSYNC,
            "nonblock" => Self::NONBLOCK,
            "noatime" => Self::NOATIME,
            "nocache" => Self::NOCACHE,
            "noctty" => Self::NOCTTY,
            "nofollow" => Self::NOFOLLOW,
            f => return Self::unknown(f.to_owned()),
        })
    }
}

impl Default for Out {
    fn default() -> Out { Out::empty() }
}

impl Unimplemented for Out {
    const KIND: Kind = Kind::OFlag;
    const UNIMPLEMENTED: &'static [&'static str] = &[
        "direct",
        "directory",
        "dsync",
        "nonblock",
        "noatime",
        "nocache",
        "noctty",
        "nofollow",
    ];
}