2022-04-09 18:40:37 +00:00
|
|
|
use crate::error::FormatyError;
|
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-09 18:34:15 +00:00
|
|
|
fn formaty() -> Result<(), 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)
|
|
|
|
.ok_or_else(|| FormatyError::FormatNotFound(args.format.to_string()))?;
|
2021-09-11 18:21:34 +00:00
|
|
|
|
|
|
|
let data = parse_bytes_from_input_arg(args.data).unwrap();
|
|
|
|
|
2022-04-09 18:34:15 +00:00
|
|
|
let data = format.format_data(&data)?;
|
|
|
|
|
|
|
|
println!("{}", data);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
match formaty() {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error formatting data: {}", e.to_string())
|
|
|
|
}
|
2021-10-09 15:30:02 +00:00
|
|
|
}
|
2021-09-11 18:21:34 +00:00
|
|
|
}
|