-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.rs
More file actions
150 lines (136 loc) · 4.38 KB
/
main.rs
File metadata and controls
150 lines (136 loc) · 4.38 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
use std::{
fs::File,
io::{self, prelude::*},
};
use anyhow::Context;
use clap::Parser;
use mdsh::{
cli::{FileArg, Opt, Parent},
executor::TheProcessor,
Cleaner, Processor,
};
fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
let clean = opt.clean;
let frozen = opt.frozen;
let inputs = opt.inputs;
if let [_, _, ..] = &inputs[..] {
opt.output
.is_none()
.then_some(())
.context("--output is not compatible with multiple inputs")?;
opt.work_dir
.is_none()
.then_some(())
.context("--work-dir is not compatible with multiple inputs")?;
for input in inputs {
let work_dir = input
.clone()
.parent()
.context("an input file has no parent directory")?;
let output = input.clone();
process_file(&input, &output, &work_dir, clean, frozen)?;
}
} else if let [input, ..] = &inputs[..] {
let output = opt.output.unwrap_or_else(|| input.clone());
let work_dir: Parent = opt.work_dir.map_or_else(
|| {
input
.clone()
.parent()
.context("the input file has no parent directory.")
},
|buf| Ok(Parent::from_parent_path_buf(buf)),
)?;
process_file(input, &output, &work_dir, clean, frozen)?;
}
Ok(())
}
fn process_file(
input: &FileArg,
output: &FileArg,
work_dir: &Parent,
clean: bool,
frozen: bool,
) -> anyhow::Result<()> {
let input_content = read_file(input)?;
let work_dir = work_dir.as_path_buf().as_os_str();
match (input, output) {
(FileArg::File(inf), FileArg::File(outf)) if inf == outf => {
let mut buffer = Vec::with_capacity(8192);
if clean {
Cleaner::new(&mut buffer).process(&input_content, input)?;
} else {
TheProcessor::new(work_dir, &mut buffer).process(&input_content, input)?;
}
let file_unmodified_check = !frozen || input_content.as_bytes() == buffer;
let mut out =
File::create(outf).with_context(|| format!("failed to write file {outf:?}"))?;
out.write(buffer.trim_ascii_end())?;
out.write(b"\n")?;
file_unmodified_check
.then_some(())
.context("File modified")?;
}
(_, FileArg::File(outf)) => {
let mut outf_handle = File::create(outf)
.with_context(|| format!("failed to open file {outf:?} for writing"))?;
if clean {
Cleaner::new(&mut outf_handle).process(&input_content, input)?;
} else {
TheProcessor::new(work_dir, &mut outf_handle).process(&input_content, input)?;
}
}
(_, FileArg::StdHandle) => {
if clean {
Cleaner::new(&mut io::stdout()).process(&input_content, input)?;
} else {
TheProcessor::new(work_dir, &mut io::stdout()).process(&input_content, input)?;
}
}
}
Ok(())
}
fn read_file(f: &FileArg) -> anyhow::Result<String> {
let mut buffer = String::with_capacity(8192);
match f {
FileArg::StdHandle => {
let stdin = io::stdin();
let mut handle = stdin.lock();
handle
.read_to_string(&mut buffer)
.context("failed to read from stdin")?;
}
FileArg::File(path_buf) => {
File::open(path_buf)
.with_context(|| format!("failed to open file {:?}", path_buf.display()))?
.read_to_string(&mut buffer)
.with_context(|| format!("failed to read file {:?}", path_buf.display()))?;
}
}
Ok(buffer)
}
/*
fn trail_nl<T: AsRef<str>>(s: T) -> String {
let r = s.as_ref();
if r.ends_with('\n') {
r.to_string()
} else {
format!("{}\n", r)
}
}
// make sure that the string starts and ends with new lines
fn wrap_nl(s: String) -> String {
if s.starts_with('\n') {
trail_nl(s)
} else if s.ends_with('\n') {
format!("\n{}", s)
} else {
format!("\n{}\n", s)
}
}
// remove all ANSI escape characters
fn filter_ansi(s: String) -> String {
RE_ANSI_FILTER.replace_all(&s, "").to_string()
}
*/