diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index cf2b551..14e3f22 100644 --- a/src/main.rs +++ b/src/main.rs | |||
| @@ -18,7 +18,9 @@ fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> std::io::Result<()> | |||
| 18 | /* There is always *at least* the base directory as a parent */ | 18 | /* There is always *at least* the base directory as a parent */ |
| 19 | fs::create_dir_all(path.parent().unwrap())?; | 19 | fs::create_dir_all(path.parent().unwrap())?; |
| 20 | fs::write(path, content)?; | 20 | fs::write(path, content)?; |
| 21 | } else { } | 21 | } else { |
| 22 | /* Print error on stderr */ | ||
| 23 | } | ||
| 22 | Ok(()) | 24 | Ok(()) |
| 23 | } | 25 | } |
| 24 | 26 | ||
| @@ -30,6 +32,9 @@ fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> std::io::Result<()> | |||
| 30 | * } | 32 | * } |
| 31 | * | 33 | * |
| 32 | * let mut text = Cow::from("This is some text..."); | 34 | * let mut text = Cow::from("This is some text..."); |
| 35 | * while MACRO.is_match(&text) { | ||
| 36 | * text = MACRO.replace_all(&text, _closure); | ||
| 37 | * } | ||
| 33 | * | 38 | * |
| 34 | * The problem with this version is that due to how `Cow` works, the value returned by | 39 | * The problem with this version is that due to how `Cow` works, the value returned by |
| 35 | * `replace_all` cannot live more than the borrowed `text` passed as a parameter. This is | 40 | * `replace_all` cannot live more than the borrowed `text` passed as a parameter. This is |
| @@ -42,10 +47,6 @@ fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> std::io::Result<()> | |||
| 42 | * the `replace_all` function is applied as long as some replacement is possible (`while` | 47 | * the `replace_all` function is applied as long as some replacement is possible (`while` |
| 43 | * condition). In other words, all calls to `replace_all` always return an `Cow::Owned` value. | 48 | * condition). In other words, all calls to `replace_all` always return an `Cow::Owned` value. |
| 44 | * | 49 | * |
| 45 | * while MACRO.is_match(&text) { | ||
| 46 | * text = MACRO.replace_all(&text, _closure); | ||
| 47 | * } | ||
| 48 | * | ||
| 49 | * This is how you would solve the problem instead: | 50 | * This is how you would solve the problem instead: |
| 50 | * | 51 | * |
| 51 | * while let Cow::Owned(new_text) = MACRO.replace_all(&text, _closure) { | 52 | * while let Cow::Owned(new_text) = MACRO.replace_all(&text, _closure) { |
| @@ -55,9 +56,6 @@ fn write_to_file<P: AsRef<Path>>(path: P, content: &str) -> std::io::Result<()> | |||
| 55 | * In this case, the matched `Cow::Owned` is not concerned by any lifetime (type is `Cow<'_,str>`) | 56 | * In this case, the matched `Cow::Owned` is not concerned by any lifetime (type is `Cow<'_,str>`) |
| 56 | * of the borrowed value `text`. Moreover `text` takes ownership of `new_text: String` using | 57 | * of the borrowed value `text`. Moreover `text` takes ownership of `new_text: String` using |
| 57 | * the `Cow::from()` function. No heap allocation is performed, and the string is not copied. | 58 | * the `Cow::from()` function. No heap allocation is performed, and the string is not copied. |
| 58 | * | ||
| 59 | * println!("{}", text) | ||
| 60 | * | ||
| 61 | */ | 59 | */ |
| 62 | fn build(blocks: &Blocks) { | 60 | fn build(blocks: &Blocks) { |
| 63 | lazy_static! { | 61 | lazy_static! { |
| @@ -98,14 +96,19 @@ fn main() -> Result<()> { | |||
| 98 | let mut blocks: Blocks = HashMap::new(); | 96 | let mut blocks: Blocks = HashMap::new(); |
| 99 | pandoc.blocks.iter().for_each(|block| | 97 | pandoc.blocks.iter().for_each(|block| |
| 100 | if let Block::CodeBlock((id,classes,attrs), code) = block { | 98 | if let Block::CodeBlock((id,classes,attrs), code) = block { |
| 101 | // dbg!(block); | 99 | /* Only process blocks with an ID */ |
| 102 | if !id.is_empty() { | 100 | if !id.is_empty() { |
| 103 | // TODO: si puo' migliorare | 101 | let key = { |
| 104 | let mut key = attrs.iter() | 102 | if let Some(path) = attrs.iter().find(|(k,_)| k == "path") { |
| 105 | .find_map(|(k,v)| if k == "path" { Some(v.clone()) } else { None }) | 103 | format!("{}{}", path.1, id) |
| 106 | .unwrap_or(String::from("")); | 104 | } else { |
| 107 | key.push_str(id); | 105 | id.to_string() |
| 108 | /* Insert (or replace) block of code. */ | 106 | } |
| 107 | }; | ||
| 108 | /* Insert (or replace) block of code. In case of ID clash, the standard | ||
| 109 | * behaviour is to append the new code to the existing snippet. Use the class | ||
| 110 | * `.override` to override the previously encountered snippets. | ||
| 111 | */ | ||
| 109 | if classes.iter().any(|c| c == "override") { | 112 | if classes.iter().any(|c| c == "override") { |
| 110 | blocks.insert(key, Cow::from(code)); | 113 | blocks.insert(key, Cow::from(code)); |
| 111 | } else { | 114 | } else { |
| @@ -125,4 +128,3 @@ fn main() -> Result<()> { | |||
| 125 | pandoc.execute().unwrap(); | 128 | pandoc.execute().unwrap(); |
| 126 | Ok(()) | 129 | Ok(()) |
| 127 | } | 130 | } |
| 128 | |||
