1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use once_cell::sync::Lazy;
use regex::Regex;
use serde_generate::{rust, CodeGeneratorConfig};
use serde_reflection::Registry;
use std::{collections::BTreeMap, io::BufRead};
pub fn quote_container_definitions(
registry: &Registry,
) -> std::result::Result<BTreeMap<String, String>, Box<dyn std::error::Error>> {
let config = CodeGeneratorConfig::new("crate".to_string()).with_serialization(false);
let generator = rust::CodeGenerator::new(&config)
.with_derive_macros(Vec::new())
.with_track_visibility(false);
generator.quote_container_definitions(registry)
}
#[allow(clippy::while_let_on_iterator)]
pub fn update_rust_quotes<R>(
reader: R,
definitions: &BTreeMap<String, String>,
) -> std::io::Result<String>
where
R: BufRead,
{
let mut result = String::new();
let mut lines = reader.lines();
while let Some(line) = lines.next() {
let line = line?;
result += &line;
result += "\n";
if let Some(name) = match_begin_command(&line) {
match definitions.get(&name) {
Some(content) => {
eprintln!("[*] Replacing quote for {}", name);
result += "```rust\n";
result += content;
result += "```\n";
while let Some(line) = lines.next() {
let line = line?;
if match_end_command(&line) {
result += &line;
result += "\n";
break;
}
}
}
None => {
eprintln!(
"[-] No definition available for {}. Leaving quote untouched",
name
);
}
}
}
}
Ok(result)
}
static BEGIN_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"<!-- @begin-diemdoc name=([^ ]*) -->").unwrap());
fn match_begin_command(line: &str) -> Option<String> {
BEGIN_RE.captures(line).map(|cap| cap[1].to_string())
}
fn match_end_command(line: &str) -> bool {
line.contains("<!-- @end-diemdoc -->")
}