From 11c38216ac93b433f03b0bc102bd7f0badbf9a8f Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Wed, 17 Apr 2024 18:49:33 -0300 Subject: [PATCH] refactor: simplify links mod --- src/links.rs | 35 +++++++++-------------------------- src/main.rs | 23 +++++------------------ 2 files changed, 14 insertions(+), 44 deletions(-) diff --git a/src/links.rs b/src/links.rs index a7f23e8..c234566 100644 --- a/src/links.rs +++ b/src/links.rs @@ -1,30 +1,10 @@ use std::cell::RefCell; -use std::path::PathBuf; use comrak::arena_tree::Node; use comrak::nodes::{Ast, NodeLink, NodeValue}; use crate::utils; -pub struct ParseOptions { - pub alias_prop: Option, - pub path_root: PathBuf, - pub to_complete_paths: bool, - pub remove_unalised: bool, - pub remove_unfound: bool, -} -impl Default for ParseOptions { - fn default() -> Self { - ParseOptions { - alias_prop: None, - path_root: PathBuf::new(), - to_complete_paths: true, - remove_unalised: true, - remove_unfound: true, - } - } -} - pub fn iterate_links<'a, F>(ast: &'a Node<'a, RefCell>, iterator: F) where F: Fn(&mut NodeLink), @@ -37,14 +17,17 @@ where }); } +pub fn replace_links<'a>(ast: &'a Node<'a, RefCell>, from: &'a str, to: &'a str) { + iterate_links(ast, |l| { + if l.url == from { + l.url = String::from(to) + } + }); +} + pub fn get_links<'a>(ast: &'a Node<'a, RefCell>) -> Vec { let links: RefCell> = RefCell::new(vec![]); - let _ = utils::iter_nodes(ast, &|node| { - if let NodeValue::Link(l) = &node.data.borrow().value { - links.borrow_mut().push(l.url.clone()); - } - Ok::<(), ()>(()) - }); + iterate_links(ast, |l| links.borrow_mut().push(l.url.clone())); let r = links.borrow().to_vec(); r } diff --git a/src/main.rs b/src/main.rs index 63e0f89..abcf3be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use core::panic; use std::io::Write; use clap::{ArgAction, Parser, Subcommand}; @@ -35,9 +34,6 @@ enum Commands { #[arg(short, long, num_args = 2, value_names = ["FROM", "TO"])] replace_url: Vec, - - #[arg(long, default_value = ".")] - root: clio::ClioPath, }, Frontmatter { #[command(subcommand)] @@ -97,28 +93,19 @@ fn main() { let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options()); let result = match cli.command { - Commands::Links { - list, - root, - replace_url, - } => { + Commands::Links { list, replace_url } => { let list = if replace_url.len() == 0 && !list { true } else { list }; - replace_url.chunks(2).for_each(|p| { - links::iterate_links(ast, |l| { - if l.url == p[0] { - l.url = (*p[1]).to_string() - } - }) - }); + replace_url + .chunks(2) + .for_each(|p| links::replace_links(ast, &p[0], &p[1])); if list { - let links = links::get_links(ast); - cli::ResultType::List(links) + cli::ResultType::List(links::get_links(ast)) } else { let mut str = vec![]; match comrak::format_commonmark(ast, &utils::default_options(), &mut str) {