-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathlib.rs
More file actions
177 lines (148 loc) · 5.39 KB
/
lib.rs
File metadata and controls
177 lines (148 loc) · 5.39 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
pub mod cli;
pub mod executor;
mod nom_ext;
pub mod parser;
#[cfg(test)]
mod tests;
use std::io::Write;
use anyhow::{Context, Result};
use nom::Finish;
use crate::parser::fmt_nom_error;
const BEGIN_MDSH: &str = "<!-- BEGIN mdsh -->";
const END_MDSH: &str = "<!-- END mdsh -->";
pub trait Processor<'a> {
fn process_piece(&mut self, piece: MdPiece<'a>) -> Result<()>;
fn process(&'a mut self, input: &'a str, input_pipe: &cli::FileArg) -> Result<()> {
// TODO: consider streaming directly from BufReader or smth,
// see https://github.com/rust-bakery/nom/issues/1145
let mut iter = nom::combinator::iterator(input, parser::markdown_piece());
for piece in iter.by_ref() {
self.process_piece(piece)
.context("processing markdown piece")?;
}
let (remaining_input, _) = iter
.finish()
.finish()
.map_err(fmt_nom_error(input, &format!("{input_pipe:?}")))
.context("parsing markdown")?;
self.process_piece(MdPiece::RawLine(remaining_input))
.context("processing remaining unparsed input")?;
Ok(())
}
}
pub struct Cleaner<W> {
pub out: W,
}
impl<W> Cleaner<W> {
pub fn new(out: W) -> Self {
Self { out }
}
}
impl<'a, W: Write> Processor<'a> for Cleaner<W> {
fn process_piece(&mut self, piece: MdPiece<'a>) -> Result<()> {
match piece {
MdPiece::FencedBlock => (),
MdPiece::Action((source, _action)) => {
self.out.write_all(source.as_bytes())?;
}
MdPiece::RawLine(raw_line) => {
self.out.write_all(raw_line.as_bytes())?;
}
}
Ok(())
}
}
#[derive(Debug)]
pub enum MdPiece<'a> {
FencedBlock,
Action(parser::ActionWithSource<'a>),
RawLine(&'a str),
}
#[cfg(test)]
pub(crate) mod test {
use crate::{cli::FileArg, executor::TheProcessor, Cleaner, Processor};
pub(crate) fn process(input: &str) -> anyhow::Result<String> {
let mut buf = Vec::new();
TheProcessor::new(std::ffi::OsStr::new("."), &mut buf)
.process(input, &FileArg::StdHandle)?;
Ok(String::from_utf8(buf)?)
}
fn process_clean(input: &str) -> anyhow::Result<String> {
let mut buf = Vec::new();
Cleaner::new(&mut buf).process(input, &FileArg::StdHandle)?;
Ok(String::from_utf8(buf)?)
}
macro_rules! assert_process_eq {
($i:tt, $o:tt) => {
const ANSI_R: &str = "\x1b[1;31m";
const ANSI_G: &str = "\x1b[1;32m";
const ANSI_B: &str = "\x1b[1;34m";
const ANSI_0: &str = "\x1b[0m";
const EOF_M: &str = "\x1b[1;33mEOF\x1b[0m";
let input = $i;
println!("{ANSI_B}INPUT ============={ANSI_0}\n{}{EOF_M}", input);
let result = process(&input)
.inspect_err(|e| println!("{e}"))
.expect("processing");
let expected = $o;
if result != expected {
println!("{ANSI_R}RESULT ============{ANSI_0}\n{}{EOF_M}", result);
println!("{ANSI_G}EXPECTED =========={ANSI_0}\n{}{EOF_M}", expected);
}
assert_eq!(result, expected, "unexpected processing result");
let result2 = process(&result)
.inspect_err(|e| println!("{e}"))
.expect("second processing");
if result != result2 {
println!("{ANSI_R}RESULT 2 =========={ANSI_0}\n{}{EOF_M}", result2);
println!("{ANSI_G}EXPECTED =========={ANSI_0}\n{}{EOF_M}", expected);
}
assert_eq!(result, result2, "processing is not idempotent");
};
}
pub(crate) use assert_process_eq;
#[test]
fn test_whole_file() {
let file_in = String::from_utf8(std::fs::read("spec.clear.md").unwrap()).unwrap();
let file_out = String::from_utf8(std::fs::read("spec.processed.md").unwrap()).unwrap();
let result = process(&file_in)
.inspect_err(|e| println!("{e}"))
.expect("processing");
if result != file_out {
std::fs::write("/tmp/tmp.md", &result).unwrap();
}
assert_eq!(
result, file_out,
"unexpected result, see `diff /tmp/tmp.md spec.clear.md`"
);
}
#[test]
fn test_whole_file_idempotency() {
let file_in = String::from_utf8(std::fs::read("spec.processed.md").unwrap()).unwrap();
let result = process(&file_in)
.inspect_err(|e| println!("{e}"))
.expect("processing");
if result != file_in {
std::fs::write("/tmp/tmp2.md", &result).unwrap();
}
assert_eq!(
result, file_in,
"unexpected result, see `diff /tmp/tmp2.md spec.processed.md`"
);
}
#[test]
fn test_whole_file_cleaner() {
let file_in = String::from_utf8(std::fs::read("spec.processed.md").unwrap()).unwrap();
let file_out = String::from_utf8(std::fs::read("spec.clear.md").unwrap()).unwrap();
let result = process_clean(&file_in)
.inspect_err(|e| println!("{e}"))
.expect("processing");
if result != file_out {
std::fs::write("/tmp/tmp3.md", &result).unwrap();
}
assert_eq!(
result, file_out,
"unexpected result, see `diff /tmp/tmp3.md spec.clear.md`"
);
}
}