90 lines
2.2 KiB
Rust
90 lines
2.2 KiB
Rust
use crate::error::FormatyError;
|
|
use crate::formatter::format::Format;
|
|
use crate::parser::InputTypes;
|
|
use formatter::FormatConfig;
|
|
use std::path::PathBuf;
|
|
use structopt::StructOpt;
|
|
|
|
mod byte_stream;
|
|
mod error;
|
|
mod formatter;
|
|
mod parser;
|
|
|
|
#[derive(Debug, StructOpt)]
|
|
#[structopt(name = "Formaty", about = "Arbitrary Binary Data Formatting")]
|
|
pub struct Args {
|
|
#[structopt(
|
|
short = "c",
|
|
long = "config",
|
|
env = "FORMATY_CONFIG",
|
|
parse(from_os_str),
|
|
help = "Path to the format config"
|
|
)]
|
|
config: Option<PathBuf>,
|
|
|
|
#[structopt(
|
|
short = "g",
|
|
long = "global_config",
|
|
env = "FORMATY_GLOBAL_CONFIG",
|
|
parse(from_os_str),
|
|
help = "Path to the global config directory"
|
|
)]
|
|
global_config: Option<PathBuf>,
|
|
|
|
#[structopt(
|
|
help = "Input data type",
|
|
short = "i",
|
|
long = "input_type",
|
|
default_value = "array"
|
|
)]
|
|
input_type: InputTypes,
|
|
|
|
#[structopt(help = "Base of the input values", short = "b", long = "base")]
|
|
base: Option<u32>,
|
|
|
|
#[structopt(help = "Input data from file", short = "f", long = "file")]
|
|
input_file: Option<PathBuf>,
|
|
|
|
#[structopt(help = "Input data from stdin", short = "s", long = "stdin")]
|
|
input_stdin: bool,
|
|
|
|
#[structopt(help = "Format to parse data as")]
|
|
format: String,
|
|
|
|
#[structopt(help = "Raw data")]
|
|
data: Vec<String>,
|
|
}
|
|
|
|
fn init() -> Result<(Vec<u8>, Format), FormatyError> {
|
|
let args: Args = Args::from_args();
|
|
|
|
let config = FormatConfig::new(&args.config, &args.global_config)?;
|
|
|
|
let format = config.get_format(&args.format)?;
|
|
|
|
let data = if args.input_stdin {
|
|
args.input_type.parse_stdin(args.base)?
|
|
} else if let Some(input_file) = args.input_file {
|
|
args.input_type.parse_file(&input_file, args.base)?
|
|
} else {
|
|
args.input_type.parse_arg_input(args.data, args.base)?
|
|
};
|
|
|
|
Ok((data, format))
|
|
}
|
|
|
|
fn main() {
|
|
let (data, format) = match init() {
|
|
Ok((data, format)) => (data, format),
|
|
Err(e) => {
|
|
println!("Error initializing: {}", e);
|
|
return;
|
|
}
|
|
};
|
|
|
|
match format.format_data(&data) {
|
|
Ok(data) => println!("{}", data),
|
|
Err(_) => print!("Unable to parse data"),
|
|
}
|
|
}
|