jxcape 0.3.1

A command line tool for creating JSON values
Documentation
use std::io::BufRead;

pub mod arrays;
pub mod cli;
pub mod env;
pub mod json;
pub mod objects;
pub mod strings;
pub mod testing;

pub fn non_empty_separator(s: &str) -> Result<String, String> {
    if s.is_empty() {
        Err("separator must not be empty".to_string())
    } else {
        Ok(s.to_string())
    }
}

pub fn read_all(input: &mut dyn BufRead) -> std::io::Result<String> {
    let mut buffer = String::new();
    for l in input.lines() {
        buffer.push_str(l?.as_str());
        buffer.push('\n')
    }
    // Remove the excess final newline
    buffer.pop();
    Ok(buffer)
}

pub trait ValueReader<T> {
    fn value(&self, alt_input: &mut dyn BufRead) -> std::io::Result<T>;
}

impl<T> dyn ValueReader<T> {
    pub fn value_stdin(&self) -> std::io::Result<T> {
        let mut buffer = std::io::stdin().lock();
        self.value(&mut buffer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::testing::{TestBuffer, TestInput};

    #[test]
    fn test_read_all_from_one_line() {
        let input = TestInput::OneLine("Hello, world!".to_string());
        let mut buffer = TestBuffer::from(input);
        let result = read_all(&mut buffer).unwrap();
        assert_eq!(result, "Hello, world!");
    }

    #[test]
    fn test_read_all_from_multiple_lines() {
        let input = TestInput::MultipleLines(vec![
            "Hello, world!".to_string(),
            "This is a test".to_string(),
        ]);
        let mut buffer = TestBuffer::from(input);
        let result = read_all(&mut buffer).unwrap();
        assert_eq!(result, "Hello, world!\nThis is a test");
    }

    #[test]
    fn non_empty_separator_accepts_non_empty_string() {
        assert_eq!(non_empty_separator("="), Ok("=".to_string()));
        assert_eq!(non_empty_separator("=="), Ok("==".to_string()));
        assert_eq!(non_empty_separator(":"), Ok(":".to_string()));
    }

    #[test]
    fn non_empty_separator_rejects_empty_string() {
        assert!(non_empty_separator("").is_err());
    }
}