dd-lib 0.2.1

library functions for a clone of the unix coreutil dd
Documentation
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 OFlag: 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 OFlag {
    /// create a new `flags::OFlag` by parsing a comma-and-optional-whitespace
    /// separated list of arguments ```
    /// #use dd:opts::flags;
    /// assert_eq!(flags::OFlag::new("directory,  append  ").unwrap(),
    /// flags::OFlag::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 OFlag {
    fn default() -> OFlag { OFlag::empty() }
}

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