#![warn(clippy::all, clippy::nursery, clippy::pedantic, clippy::cargo)]
use argh::FromArgs;
use castwright::{CastWright, Error, ErrorType, VERSION};
use disperror::DispError;
use std::{
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
path::Path,
};
#[derive(FromArgs)]
#[argh(help_triggers("-h", "--help"))]
struct Args {
#[argh(option, short = 'i')]
input: Option<String>,
#[argh(option, short = 'o')]
output: Option<String>,
#[argh(switch, short = 'x')]
execute: bool,
#[argh(switch, short = 't')]
timestamp: bool,
#[argh(switch, short = 'v')]
version: bool,
}
fn file(path: &str, create: bool) -> Result<File, Error> {
let path = Path::new(path);
let file = if create {
File::create(path)
} else {
File::open(path)
};
file.map_err(|e| ErrorType::Io(e).with_line(0))
}
fn link(text: &str, url: &str) {
print!("\x1b]8;;{url}\x07{text}\x1b]8;;\x07");
}
fn version() {
let github_base = "https://github.com/PRO-2684/castwright";
println!("🎥 CastWright v{VERSION}");
link("GitHub", github_base);
print!(" | ");
link("Releases", &format!("{github_base}/releases"));
print!(" | ");
link("Docs.rs", "https://docs.rs/castwright");
print!(" | ");
link("Crates.io", "https://crates.io/crates/castwright");
println!();
}
fn main() -> Result<(), DispError<Error>> {
let args: Args = argh::from_env();
if args.version {
version();
return Ok(());
}
let mut reader: &mut dyn BufRead = match &args.input {
Some(path) => &mut BufReader::new(file(path, false)?),
None => &mut std::io::stdin().lock(),
};
let mut writer: &mut BufWriter<dyn Write> = match &args.output {
Some(path) => &mut BufWriter::new(file(path, true)?),
None => &mut BufWriter::new(std::io::stdout().lock()),
};
CastWright::new()
.execute(args.execute)
.timestamp(args.timestamp)
.preview(args.output.is_some())
.run(&mut reader, &mut writer)?;
Ok(())
}