From 48d4b0eb999c28f8b6236582d769f3b4abcc5e3f Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Mon, 29 Apr 2024 16:03:47 -0300 Subject: [PATCH] feat(cli): command for converting the format --- src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index a5a618e..eac8137 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,10 @@ enum Commands { #[arg(short, long, num_args = 2, value_names = ["FROM", "TO"])] rename_prop: Vec, }, + Convert { + #[arg(short, long)] + format: convert::Formats, + }, } fn main() { @@ -73,12 +77,6 @@ fn main() { let arena = comrak::Arena::new(); let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options()); - match convert::npf::from(ast) { - Ok(test) => println!("{}", serde_json::to_string_pretty(&test).unwrap()), - Err(err) => println!("{err:#?}"), - }; - // println!("{:#?}", ast); - let result = match cli.command { Commands::Links { list, replace_url } => { let list = if replace_url.len() == 0 && !list { @@ -137,12 +135,44 @@ fn main() { cli::ResultType::Markdown(ast) } } - _ => cli::ResultType::Err(cli::Error { - description: "".to_string(), - code: cli::ErrorCode::EPRSG, - url: None, - fix: None, - }), + Commands::Convert { format } => match format { + convert::Formats::NPF => match convert::npf::from(ast) { + Ok(npf) => { + let function = if cli.input.is_tty() { + serde_json::to_string_pretty + } else { + serde_json::to_string + }; + + match function(&npf).map_err(|e| { + cli::ResultType::Err(cli::Error { + description: format!( + "Failed to parse Tumblr NPF struct to JSON string + on line {}, column {}. Used vector: \n{:#?}", + e.line(), + e.column(), + &npf + ), + code: cli::ErrorCode::EPRSG, + url: None, + fix: None, + }) + }) { + Ok(s) => cli::ResultType::String(s), + Err(e) => e, + } + } + Err(err) => cli::ResultType::Err(cli::Error { + description: format!( + "Failed to convert to Tumblr NPF format, due to error:\n{:#?}", + err + ), + code: cli::ErrorCode::ECNPF, + url: None, + fix: None, + }), + }, + }, }; if let cli::ListFormat::JSON = &cli.list_format { @@ -239,6 +269,7 @@ mod cli { #[derive(Debug)] pub enum ErrorCode { EPRSG, + ECNPF, EIORD, EIOWR, EIOTY, @@ -261,6 +292,7 @@ mod cli { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let title = match self.code { ErrorCode::EPRSG => "Parsing error", + ErrorCode::ECNPF => "Error converting to NPF format", ErrorCode::EIORD => "IO error on read operation", ErrorCode::EIOWR => "IO error on write operation", ErrorCode::EIOTY => "IO error on input/output type",