2022-04-09 18:40:37 +00:00
|
|
|
use crate::error::FormatyError;
|
2022-04-10 16:42:05 +00:00
|
|
|
use crate::formatter::format::Format;
|
2021-09-11 18:21:34 +00:00
|
|
|
use crate::parser::parse_bytes_from_input_arg;
|
|
|
|
use formatter::FormatConfig;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
mod byte_stream;
|
2022-04-09 18:40:37 +00:00
|
|
|
mod error;
|
2021-09-11 18:21:34 +00:00
|
|
|
mod formatter;
|
|
|
|
mod parser;
|
|
|
|
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
|
|
#[structopt(name = "Formaty", about = "Arbitrary Binary Data Formatting")]
|
|
|
|
pub struct Args {
|
2022-04-09 18:40:37 +00:00
|
|
|
#[structopt(
|
|
|
|
short = "c",
|
|
|
|
long = "config",
|
|
|
|
parse(from_os_str),
|
|
|
|
help = "Path to the format config"
|
|
|
|
)]
|
2022-04-09 18:34:15 +00:00
|
|
|
config: Option<PathBuf>,
|
2021-09-11 18:21:34 +00:00
|
|
|
|
2021-10-09 20:09:21 +00:00
|
|
|
#[structopt(help = "Format to parse data as")]
|
2021-09-11 18:21:34 +00:00
|
|
|
format: String,
|
|
|
|
|
2021-10-09 20:09:21 +00:00
|
|
|
#[structopt(help = "Raw data")]
|
2021-09-11 18:21:34 +00:00
|
|
|
data: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:42:05 +00:00
|
|
|
fn init() -> Result<(Vec<u8>, Format), FormatyError> {
|
2021-09-11 18:21:34 +00:00
|
|
|
let args: Args = Args::from_args();
|
|
|
|
|
2022-04-09 18:34:15 +00:00
|
|
|
let config = FormatConfig::new(&args.config)?;
|
2021-09-11 18:21:34 +00:00
|
|
|
|
2022-04-09 18:40:37 +00:00
|
|
|
let format = config
|
|
|
|
.formats
|
|
|
|
.iter()
|
|
|
|
.find(|f| f.name == args.format)
|
2022-04-10 16:42:05 +00:00
|
|
|
.ok_or_else(|| FormatyError::FormatNotFound(args.format.to_string()))?
|
|
|
|
.clone();
|
2021-09-11 18:21:34 +00:00
|
|
|
|
|
|
|
let data = parse_bytes_from_input_arg(args.data).unwrap();
|
2022-04-10 16:42:05 +00:00
|
|
|
Ok((data, format))
|
2022-04-09 18:34:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2022-04-10 16:42:05 +00:00
|
|
|
let (data, format) = match init() {
|
|
|
|
Ok((data, format)) => (data, format),
|
2022-04-09 18:34:15 +00:00
|
|
|
Err(e) => {
|
2022-04-10 16:42:05 +00:00
|
|
|
println!("Error initializing: {}", e);
|
|
|
|
return;
|
2022-04-09 18:34:15 +00:00
|
|
|
}
|
2022-04-10 16:42:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match format.format_data(&data) {
|
|
|
|
Ok(data) => println!("{}", data),
|
|
|
|
Err(_) => print!("Unable to parse data"),
|
2021-10-09 15:30:02 +00:00
|
|
|
}
|
2021-09-11 18:21:34 +00:00
|
|
|
}
|