use std::io::Read;
use std::path::PathBuf;
use std::process::ExitCode;
const VERSION: &str = env!("CARGO_PKG_VERSION");
const USAGE: &str = "Usage: jots [OPTIONS] [FILE]
Jot Syntax parser. Reads from FILE or stdin, emits parsed metadata as JSON.
Options:
--version Print version and exit
--no-loc Omit `loc` and `raw` from emitted construct objects
--unicode Parse the Iota Syntaxis variant (Unicode XID identifiers)
--category-root W Require every category path to start with segment W
-h, --help Show this help
";
fn main() -> ExitCode {
let args: Vec<String> = std::env::args().skip(1).collect();
let mut files: Vec<PathBuf> = Vec::new();
let mut no_loc = false;
let mut unicode = false;
let mut category_root: Option<String> = None;
let mut args = args.into_iter();
while let Some(arg) = args.next() {
match arg.as_str() {
"--version" => {
println!("jots {}", VERSION);
return ExitCode::SUCCESS;
}
"--help" | "-h" => {
print!("{}", USAGE);
return ExitCode::SUCCESS;
}
"--no-loc" => no_loc = true,
"--unicode" => unicode = true,
"--category-root" => match args.next() {
Some(v) => category_root = Some(v),
None => {
eprintln!("Error: --category-root requires a value");
return ExitCode::FAILURE;
}
},
s if s.starts_with('-') => {
eprintln!("Error: unknown option '{}'", s);
return ExitCode::FAILURE;
}
_ => files.push(PathBuf::from(arg)),
}
}
let mut cfg = jots::config::load();
if no_loc {
cfg.set_bool("emit_loc", false);
}
cfg.unicode = unicode;
if let Some(root) = category_root {
cfg.set("category_root", jots::FlagValue::Raw(root));
}
let text = match files.first() {
Some(path) => match std::fs::read_to_string(path) {
Ok(t) => t,
Err(e) => {
eprintln!("Error: cannot open '{}': {}", path.display(), e);
return ExitCode::FAILURE;
}
},
None => {
let mut buf = String::new();
if let Err(e) = std::io::stdin().read_to_string(&mut buf) {
eprintln!("Error reading stdin: {}", e);
return ExitCode::FAILURE;
}
buf
}
};
let result = jots::parse(&text, &cfg);
match serde_json::to_string_pretty(&result) {
Ok(s) => {
println!("{}", s);
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("Error encoding JSON: {}", e);
ExitCode::FAILURE
}
}
}