| 1 | use std::collections::BTreeSet;
|
| 2 |
|
| 3 | use crate::dsl::{Document, Inline};
|
| 4 |
|
| 5 | pub fn collect_parameter_names(document: &Document) -> Vec<String> {
|
| 6 | let mut names = BTreeSet::new();
|
| 7 | for block in &document.blocks {
|
| 8 | collect_from_block(block, &mut names);
|
| 9 | }
|
| 10 | names.into_iter().collect()
|
| 11 | }
|
| 12 |
|
| 13 | pub fn collect_parameter_names_from_source(source: &str) -> Result<Vec<String>, crate::dsl::ParseError> {
|
| 14 | let doc = crate::dsl::parse(source)?;
|
| 15 | Ok(collect_parameter_names(&doc))
|
| 16 | }
|
| 17 |
|
| 18 | pub fn substitute_inlines(inlines: &[Inline], values: &std::collections::HashMap<String, String>) -> Vec<Inline> {
|
| 19 | inlines
|
| 20 | .iter()
|
| 21 | .map(|inline| match inline {
|
| 22 | Inline::Text(t) => Inline::Text(t.clone()),
|
| 23 | Inline::Code(t) => Inline::Code(t.clone()),
|
| 24 | Inline::Param(name) => Inline::Text(values.get(name).cloned().unwrap_or_default()),
|
| 25 | Inline::Bold(inner) => Inline::Bold(substitute_inlines(inner, values)),
|
| 26 | Inline::Italic(inner) => Inline::Italic(substitute_inlines(inner, values)),
|
| 27 | })
|
| 28 | .collect()
|
| 29 | }
|
| 30 |
|
| 31 | pub fn substitute_string(input: &str, values: &std::collections::HashMap<String, String>) -> String {
|
| 32 | let mut out = String::new();
|
| 33 | let chars: Vec<char> = input.chars().collect();
|
| 34 | let mut i = 0;
|
| 35 | while i < chars.len() {
|
| 36 | if chars[i] == '$' && i + 1 < chars.len() && chars[i + 1] == '{' {
|
| 37 | let start = i + 2;
|
| 38 | let mut j = start;
|
| 39 | while j < chars.len() && chars[j] != '}' {
|
| 40 | j += 1;
|
| 41 | }
|
| 42 | if j < chars.len() {
|
| 43 | let name: String = chars[start..j].iter().collect();
|
| 44 | out.push_str(values.get(&name).map(String::as_str).unwrap_or(""));
|
| 45 | i = j + 1;
|
| 46 | continue;
|
| 47 | }
|
| 48 | }
|
| 49 | out.push(chars[i]);
|
| 50 | i += 1;
|
| 51 | }
|
| 52 | out
|
| 53 | }
|
| 54 |
|
| 55 | pub fn substitute_document(
|
| 56 | document: &Document,
|
| 57 | values: &std::collections::HashMap<String, String>,
|
| 58 | ) -> Document {
|
| 59 | use crate::dsl::Block;
|
| 60 |
|
| 61 | let blocks = document
|
| 62 | .blocks
|
| 63 | .iter()
|
| 64 | .map(|block| match block {
|
| 65 | Block::Paragraph { align, inlines } => Block::Paragraph {
|
| 66 | align: *align,
|
| 67 | inlines: substitute_inlines(inlines, values),
|
| 68 | },
|
| 69 | Block::Heading { level, inlines } => Block::Heading {
|
| 70 | level: *level,
|
| 71 | inlines: substitute_inlines(inlines, values),
|
| 72 | },
|
| 73 | Block::Rule => Block::Rule,
|
| 74 | Block::Feed(n) => Block::Feed(*n),
|
| 75 | Block::Cut => Block::Cut,
|
| 76 | Block::Image { path, width, align } => Block::Image {
|
| 77 | path: substitute_string(path, values),
|
| 78 | width: *width,
|
| 79 | align: *align,
|
| 80 | },
|
| 81 | Block::Barcode {
|
| 82 | mode,
|
| 83 | symbology,
|
| 84 | data,
|
| 85 | height,
|
| 86 | align,
|
| 87 | show_text,
|
| 88 | } => Block::Barcode {
|
| 89 | mode: *mode,
|
| 90 | symbology: *symbology,
|
| 91 | data: substitute_string(data, values),
|
| 92 | height: *height,
|
| 93 | align: *align,
|
| 94 | show_text: *show_text,
|
| 95 | },
|
| 96 | Block::Qr {
|
| 97 | mode,
|
| 98 | data,
|
| 99 | size,
|
| 100 | ecc,
|
| 101 | version,
|
| 102 | align,
|
| 103 | } => Block::Qr {
|
| 104 | mode: *mode,
|
| 105 | data: substitute_string(data, values),
|
| 106 | size: *size,
|
| 107 | ecc: *ecc,
|
| 108 | version: *version,
|
| 109 | align: *align,
|
| 110 | },
|
| 111 | })
|
| 112 | .collect();
|
| 113 | Document { blocks }
|
| 114 | }
|
| 115 |
|
| 116 | fn collect_from_block(block: &crate::dsl::Block, names: &mut BTreeSet<String>) {
|
| 117 | use crate::dsl::Block;
|
| 118 |
|
| 119 | match block {
|
| 120 | Block::Paragraph { inlines, .. } | Block::Heading { inlines, .. } => {
|
| 121 | collect_from_inlines(inlines, names);
|
| 122 | }
|
| 123 | Block::Image { path, .. } => collect_from_raw(path, names),
|
| 124 | Block::Barcode { data, .. } | Block::Qr { data, .. } => collect_from_raw(data, names),
|
| 125 | Block::Rule | Block::Feed(_) | Block::Cut => {}
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | fn collect_from_inlines(inlines: &[Inline], names: &mut BTreeSet<String>) {
|
| 130 | for inline in inlines {
|
| 131 | match inline {
|
| 132 | Inline::Param(name) => {
|
| 133 | names.insert(name.clone());
|
| 134 | }
|
| 135 | Inline::Bold(inner) | Inline::Italic(inner) => collect_from_inlines(inner, names),
|
| 136 | Inline::Text(_) | Inline::Code(_) => {}
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | fn collect_from_raw(text: &str, names: &mut BTreeSet<String>) {
|
| 142 | let chars: Vec<char> = text.chars().collect();
|
| 143 | let mut i = 0;
|
| 144 | while i < chars.len() {
|
| 145 | if chars[i] == '$' && i + 1 < chars.len() && chars[i + 1] == '{' {
|
| 146 | let start = i + 2;
|
| 147 | let mut j = start;
|
| 148 | while j < chars.len() && chars[j] != '}' {
|
| 149 | j += 1;
|
| 150 | }
|
| 151 | if j < chars.len() {
|
| 152 | let name: String = chars[start..j].iter().collect();
|
| 153 | names.insert(name);
|
| 154 | i = j + 1;
|
| 155 | continue;
|
| 156 | }
|
| 157 | }
|
| 158 | i += 1;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | #[cfg(test)]
|
| 163 | mod tests {
|
| 164 | use super::*;
|
| 165 | use std::collections::HashMap;
|
| 166 |
|
| 167 | #[test]
|
| 168 | fn collects_unique_sorted_names() {
|
| 169 | let doc = crate::dsl::parse("${B}\n${A}\n${B}").unwrap();
|
| 170 | assert_eq!(collect_parameter_names(&doc), vec!["A", "B"]);
|
| 171 | }
|
| 172 |
|
| 173 | #[test]
|
| 174 | fn substitutes_all_occurrences() {
|
| 175 | let doc = crate::dsl::parse("Hi ${Name} and ${Name}").unwrap();
|
| 176 | let mut values = HashMap::new();
|
| 177 | values.insert("Name".into(), "Ada".into());
|
| 178 | let sub = substitute_document(&doc, &values);
|
| 179 | let crate::dsl::Block::Paragraph { inlines, .. } = &sub.blocks[0] else {
|
| 180 | panic!();
|
| 181 | };
|
| 182 | assert!(matches!(
|
| 183 | inlines.as_slice(),
|
| 184 | [
|
| 185 | Inline::Text(a),
|
| 186 | Inline::Text(b),
|
| 187 | Inline::Text(c),
|
| 188 | Inline::Text(d),
|
| 189 | ] if a == "Hi " && b == "Ada" && c == " and " && d == "Ada"
|
| 190 | ));
|
| 191 | }
|
| 192 | }
|