catalist 0.8.0

Cat, but for lists
Documentation
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use structopt::clap::arg_enum;

arg_enum! {
    /// TODO: Document this
    #[derive(Debug, PartialEq)]
    pub enum QuoteStyle {
        Always,
        Auto,
        Never
    }
}

impl QuoteStyle {
    pub fn naive_surround(&self, text: &str) -> String {
        match self {
            Self::Always => format!("\"{}\"", text),
            Self::Never => text.into(),
            Self::Auto => {
                if text.contains(|s: char| s.is_ascii_whitespace()) {
                    format!("\"{}\"", text)
                } else {
                    text.into()
                }
            }
        }
    }
}

impl From<&str> for QuoteStyle {
    fn from(string: &str) -> Self {
        match string.to_lowercase().as_str() {
            "always" => QuoteStyle::Always,
            "never" => QuoteStyle::Never,
            _ => QuoteStyle::Auto,
        }
    }
}

/// TODO: Document this
pub fn content_of(text: &str) -> Option<&str> {
    let trimmed = text.trim();
    if trimmed.is_empty() || trimmed.starts_with('#') {
        None
    } else {
        for (i, _) in trimmed.match_indices('#') {
            // only a hash preceeded by whitespace is a valid comment marker
            if trimmed
                .chars()
                .nth(i - 1)
                .expect("no char before hash")
                .is_ascii_whitespace()
            {
                // don't know how much whitespace was at the end,
                // so trim it to be safe
                return Some(trimmed[..i - 1].trim_end());
            }
        }
        // if the loop hasn't returned something, it returns the trimmed input
        Some(trimmed)
    }
}

/// TODO: Document this
pub fn file_reader(file: &str) -> Result<Box<dyn BufRead>, io::Error> {
    match file {
        "-" => Ok(Box::new(BufReader::new(io::stdin()))),
        _ => Ok(Box::new(BufReader::new(File::open(&file)?))),
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_content_of() {
        // No text, or only whitespace, is not content
        assert_eq!(content_of(""), None);
        assert_eq!(content_of(" "), None);
        // Surrounding whitespace isn't kept, but internal whitespace is
        assert_eq!(content_of(" yes "), Some("yes"));
        assert_eq!(content_of(" yes  yes "), Some("yes  yes"));
        // Lines starting with a comment are not content
        assert_eq!(content_of("# test"), None);
        assert_eq!(content_of("  # test"), None);
        // A hash preceeded by whitespace is not content
        assert_eq!(content_of("yes # no"), Some("yes"));
        // A hash not preceeded by whitespace is content
        assert_eq!(content_of("yes# yes"), Some("yes# yes"));
    }

    #[test]
    fn test_quotes_from_str() {
        // Defaults to Auto
        assert_eq!(QuoteStyle::from("test"), QuoteStyle::Auto);
        // "always" and "never" return their specific values, case insensitive
        assert_eq!(QuoteStyle::from("always"), QuoteStyle::Always);
        assert_eq!(QuoteStyle::from("ALWAYS"), QuoteStyle::Always);
        assert_eq!(QuoteStyle::from("never"), QuoteStyle::Never);
        assert_eq!(QuoteStyle::from("NEVER"), QuoteStyle::Never);
    }
}