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 {
#[arg(long, ignore_case = true)]
from: FormatArg,
#[arg(long, ignore_case = true)]
to: FormatArg,
input: PathBuf,
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(())
}