sheeter-engine 0.6.0

A CLI tool for converting data files between formats. v0.1.0 supports CSV <-> JSON.
Documentation
use clap::{Parser, Subcommand, ValueEnum};
use sheeter_engine::{convert, Format};
use std::fs;
use std::path::PathBuf;

#[derive(Parser)]
#[command(
    name = "sheeter",
    version,
    about = "Convert data files between formats"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Convert a data file from one format to another
    Convert {
        /// Source format (csv, tsv, json, yaml, xml, toml)
        #[arg(long, ignore_case = true)]
        from: FormatArg,
        /// Target format (csv, tsv, json, yaml, xml, toml)
        #[arg(long, ignore_case = true)]
        to: FormatArg,
        /// Input file
        input: PathBuf,
        /// Output file
        output: PathBuf,
    },
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum FormatArg {
    Csv,
    Tsv,
    Json,
    Yaml,
    Xml,
    Toml,
}

impl From<FormatArg> for Format {
    fn from(f: FormatArg) -> Self {
        match f {
            FormatArg::Csv => Self::Csv,
            FormatArg::Tsv => Self::Tsv,
            FormatArg::Json => Self::Json,
            FormatArg::Yaml => Self::Yaml,
            FormatArg::Xml => Self::Xml,
            FormatArg::Toml => Self::Toml,
        }
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();
    match cli.command {
        Commands::Convert {
            from,
            to,
            input,
            output,
        } => {
            let content = fs::read_to_string(&input)?;
            let result = convert(from.into(), to.into(), &content)?;
            fs::write(&output, result)?;
        }
    }
    Ok(())
}