Added default formatters + error handling
This commit is contained in:
8 files changed
+337
-21
No files matched your search
+20
-14
@@ -2,16 +2,18 @@ use crate::parser::parse_bytes_from_input_arg;
|
||||
use formatter::FormatConfig;
|
||||
use std::path::PathBuf;
|
||||
use structopt::StructOpt;
|
||||
use crate::error::FormatyError;
|
||||
|
||||
mod byte_stream;
|
||||
mod formatter;
|
||||
mod parser;
|
||||
mod error;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "Formaty", about = "Arbitrary Binary Data Formatting")]
|
||||
pub struct Args {
|
||||
#[structopt(parse(from_os_str), help = "Path to the format config")]
|
||||
config: PathBuf,
|
||||
#[structopt(short = "c", long="config", parse(from_os_str), help = "Path to the format config")]
|
||||
config: Option<PathBuf>,
|
||||
|
||||
#[structopt(help = "Format to parse data as")]
|
||||
format: String,
|
||||
@@ -20,23 +22,27 @@ pub struct Args {
|
||||
data: Vec<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn formaty() -> Result<(), FormatyError> {
|
||||
let args: Args = Args::from_args();
|
||||
|
||||
let config = FormatConfig::new(&args.config).unwrap();
|
||||
let config = FormatConfig::new(&args.config)?;
|
||||
|
||||
let format = match config.formats.iter().find(|f| f.name == args.format) {
|
||||
None => {
|
||||
println!("Format not found in config file");
|
||||
return;
|
||||
}
|
||||
Some(format) => format,
|
||||
};
|
||||
let format = config.formats.iter().find(|f| f.name == args.format).ok_or(FormatyError::FormatNotFound(args.format.to_string()))?;
|
||||
|
||||
let data = parse_bytes_from_input_arg(args.data).unwrap();
|
||||
|
||||
match format.format_data(&data) {
|
||||
Ok(data_str) => println!("{}", data_str),
|
||||
Err(_) => println!("Error formatting data"),
|
||||
let data = format.format_data(&data)?;
|
||||
|
||||
println!("{}", data);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match formaty() {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("Error formatting data: {}", e.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user