From 473a02de1b99ed9ae91f7136d1ab778c28f4855e Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Tue, 2 Apr 2024 12:35:46 -0300 Subject: [PATCH] fix: handle errors --- src/convert.rs | 60 ++++++++++++++++++++++++++------------------------ src/main.rs | 6 +++-- src/utils.rs | 29 +++++++++--------------- 3 files changed, 46 insertions(+), 49 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 8fee437..1796660 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -63,8 +63,10 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell>) -> Result } NodeValue::Text(t) => text.push_str(&format!("{} ", &t)), _ => (), - } - }); + }; + + Ok::<(), Error>(()) + })?; let text = text.borrow().trim().to_string().replace(" ", " "); let mut block = content_types::Text::from(text); @@ -76,43 +78,40 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell>) -> Result }; npf.borrow_mut().content.push(ContentType::Text(block)); + + Ok::<(), Error>(()) } NodeValue::BlockQuote => { - let block_quote = to_tumblr_npf(node); + let block_quote = to_tumblr_npf(node)?; let final_block = RefCell::new(content_types::Text::new()); - block_quote - .unwrap() - .borrow_mut() - .content - .iter_mut() - .for_each(|b| { - if let ContentType::Text(t) = b { - let mut fb = final_block.borrow_mut(); - let text_len = fb.text.chars().count(); + block_quote.borrow_mut().content.iter_mut().for_each(|b| { + if let ContentType::Text(t) = b { + let mut fb = final_block.borrow_mut(); + let text_len = fb.text.chars().count(); - if let Some(formattings) = &t.formatting { - for formatting in formattings { - match fb.formatting { - Some(ref mut v) => v.push(Formatting { + if let Some(formattings) = &t.formatting { + for formatting in formattings { + match fb.formatting { + Some(ref mut v) => v.push(Formatting { + start: text_len + formatting.start, + end: text_len + formatting.end, + ..formatting.clone() + }), + None => { + fb.formatting = Some(vec![Formatting { start: text_len + formatting.start, end: text_len + formatting.end, ..formatting.clone() - }), - None => { - fb.formatting = Some(vec![Formatting { - start: text_len + formatting.start, - end: text_len + formatting.end, - ..formatting.clone() - }]); - } + }]); } } } - fb.text.push_str(&format!("{}\n\n", &t.text)); - } else { } - }); + fb.text.push_str(&format!("{}\n\n", &t.text)); + } else { + } + }); println!("{node:#?}"); println!("{:#?}", final_block.borrow()); @@ -130,6 +129,7 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell>) -> Result }) .for_each(|b| npf.borrow_mut().content.push(b.clone())); */ + Ok(()) } NodeValue::Heading(h) => { let mut text = content_types::Text::from(utils::extract_text(node)); @@ -157,9 +157,11 @@ pub fn to_tumblr_npf<'a>(ast: &'a Node<'a, RefCell>) -> Result } npf.borrow_mut().content.push(ContentType::Text(text)); + + Ok(()) } - _ => (), - }); + _ => Ok(()), + })?; Ok(npf) } diff --git a/src/main.rs b/src/main.rs index a1bcd68..3ee45df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -94,7 +94,7 @@ fn main() { return; } - match &cli.command { + let _: Result<()> = match &cli.command { Commands::Links { path_root, alias_prop, @@ -124,6 +124,7 @@ fn main() { } }; } + Ok(()) }), Commands::Frontmatter { command } => utils::iter_nodes(&ast, &|node| { if let NodeValue::FrontMatter(ref mut f) = &mut node.data.borrow_mut().value { @@ -162,8 +163,9 @@ fn main() { Err(e) => panic!("{e:#?}"), }; } + Ok(()) }), - _ => (), + _ => Ok(()), }; let _ = comrak::format_commonmark(&ast, &mdparser::utils::default_options(), &mut cli.output); diff --git a/src/utils.rs b/src/utils.rs index cc63edd..ffecf07 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -14,34 +14,25 @@ pub fn default_options() -> comrak::Options { opts } -pub fn iter_nodes<'a, F>(node: &'a comrak::nodes::AstNode<'a>, f: &F) +pub fn iter_nodes<'a, F, E>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Result<(), E> where - F: Fn(&'a comrak::nodes::AstNode<'a>), + F: Fn(&'a comrak::nodes::AstNode<'a>) -> Result<(), E>, { - f(node); + f(node)?; for c in node.children() { - iter_nodes(c, f) + iter_nodes(c, f)? } + Ok(()) } -pub fn iter_nodes_shallow<'a, F>(node: &'a comrak::nodes::AstNode<'a>, f: &F) +pub fn iter_nodes_shallow<'a, F, E>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Result<(), E> where - F: Fn(&'a comrak::nodes::AstNode<'a>), + F: Fn(&'a comrak::nodes::AstNode<'a>) -> Result<(), E>, { for c in node.children() { - f(c) + f(c)? } -} - -pub fn extract_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String { - let text = RefCell::new(String::new()); - iter_nodes(node, &|node| { - if let NodeValue::Text(t) = &node.data.borrow().value { - text.borrow_mut().push_str(&t); - } - }); - let r = text.borrow().to_string(); - r + Ok(()) } pub fn iter_nodes_r<'a, F, T>(node: &'a comrak::nodes::AstNode<'a>, f: &F) -> Option @@ -60,12 +51,14 @@ where } None } + pub fn extract_text<'a>(node: &'a comrak::nodes::AstNode<'a>) -> String { let text = RefCell::new(String::new()); let _ = iter_nodes(node, &|node| { if let NodeValue::Text(t) = &node.data.borrow().value { text.borrow_mut().push_str(&t); } + Ok::<(), std::fmt::Error>(()) }); let r = text.borrow().to_string(); r