mod check;
mod index;
mod new;
mod render;
mod schema;
mod tests;
use crate::config::{GlobalArgs, GlobalConfig};
#[derive(Debug, clap::Parser)]
#[command(author, version, about, long_about = None)]
pub struct Command {
#[clap(subcommand)]
subcommand: SubCommand,
#[clap(flatten)]
global: GlobalArgs,
}
#[derive(Debug, clap::Parser)]
pub struct OutputArgs {
#[clap(long, short)]
output: Option<std::path::PathBuf>,
#[clap(long, short, conflicts_with = "output")]
stdout: bool,
}
#[derive(Debug, clap::Subcommand)]
enum SubCommand {
New(new::NewCommand),
Render(render::RenderCommand),
Schema(schema::SchemaCommand),
Check(check::CheckCommand),
Index(index::IndexCommand),
}
impl Command {
pub fn run(self) -> anyhow::Result<()> {
let g: GlobalConfig = self.global.try_into()?;
match &self.subcommand {
SubCommand::New(c) => c.run(&g),
SubCommand::Render(c) => c.run(&g),
SubCommand::Schema(c) => c.run(&g),
SubCommand::Check(c) => c.run(&g),
SubCommand::Index(c) => c.run(),
}
}
}
impl OutputArgs {
fn write_schema(&self, default_path: &str, data: &str) -> anyhow::Result<()> {
if let Some(path) = &self.output {
if path.is_dir() {
let path = path.join(default_path);
std::fs::write(&path, data)?;
println!("Wrote schema to '{}'", path.display());
} else {
std::fs::write(path, data)?;
println!("Wrote schema to '{}'", path.display());
}
} else if self.stdout {
println!("{}", data);
} else {
let path = std::path::PathBuf::from(default_path);
std::fs::write(&path, data)?;
println!("Wrote schema to '{}'", path.display());
}
Ok(())
}
}
pub(crate) fn plural(count: usize) -> &'static str {
if count == 1 {
""
} else {
"s"
}
}
#[derive(Debug, Clone, PartialEq, clap::ValueEnum)]
pub enum OnOff {
On,
Off,
}