use crate::{Command, DocumentCommand, File, FileCommand, FileFormat, Options, TransformCommand};
use somedoc::write::OutputFormat;
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "cargo-atelier", about = "Tools for the Smithy IDL.")]
pub(crate) struct CommandLine {
#[structopt(long, short = "v", parse(from_occurrences))]
verbose: i8,
#[cfg(feature = "color")]
#[structopt(long, short)]
no_color: bool,
#[structopt(subcommand)]
cmd: SubCommand,
}
#[derive(Debug, StructOpt)]
pub(crate) enum SubCommand {
Lint {
#[structopt(long, short)]
in_file: Option<PathBuf>,
#[structopt(short, long, default_value = "smithy")]
read_format: FileFormat,
},
Validate {
#[structopt(long, short)]
in_file: Option<PathBuf>,
#[structopt(short, long, default_value = "smithy")]
read_format: FileFormat,
},
Convert {
#[structopt(long, short)]
in_file: Option<PathBuf>,
#[structopt(short, long, default_value = "smithy")]
read_format: FileFormat,
#[structopt(long, short)]
out_file: Option<PathBuf>,
#[structopt(short, long, default_value = "json")]
write_format: FileFormat,
#[structopt(short, long)]
namespace: Option<String>,
},
Document {
#[structopt(long, short)]
in_file: Option<PathBuf>,
#[structopt(short, long, default_value = "smithy")]
read_format: FileFormat,
#[structopt(long, short)]
out_file: Option<PathBuf>,
#[structopt(short, long, default_value = "markdown")]
write_format: OutputFormat,
},
}
#[derive(Debug)]
pub struct CommandLineError {}
pub fn parse() -> Result<Command, Box<dyn Error>> {
let args = CommandLine::from_args();
let options = Options {
use_color: !args.no_color,
};
match args.cmd {
SubCommand::Lint {
in_file,
read_format,
} => Ok(Command::Lint(
FileCommand {
input_file: File {
file_name: in_file,
format: read_format,
},
},
options,
)),
SubCommand::Validate {
in_file,
read_format,
} => Ok(Command::Validate(
FileCommand {
input_file: File {
file_name: in_file,
format: read_format,
},
},
options,
)),
SubCommand::Convert {
in_file,
read_format,
out_file,
write_format,
namespace,
} => Ok(Command::Convert(
TransformCommand {
input_file: File {
file_name: in_file,
format: read_format,
},
output_file: File {
file_name: out_file,
format: write_format,
},
namespace,
},
options,
)),
SubCommand::Document {
in_file,
read_format,
out_file,
write_format,
} => Ok(Command::Document(
DocumentCommand {
input_file: File {
file_name: in_file,
format: read_format,
},
output_file: File {
file_name: out_file,
format: FileFormat::Smithy,
},
output_format: write_format,
},
options,
)),
}
}
impl Default for CommandLineError {
fn default() -> Self {
Self {}
}
}
impl Display for CommandLineError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "An error occurred processing command line options")
}
}
impl Error for CommandLineError {}
impl CommandLineError {
pub fn boxed() -> Box<Self> {
Box::new(Self::default())
}
}