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
178
179
use crate::Result;
use regex::Regex;
use std::path::Path;
#[derive(Copy, Clone, PartialEq)]
enum Operation {
InsertBefore,
InsertBeforeAll,
InsertAfter,
InsertAfterAll,
Prepend,
Append,
RemoveLine,
RemoveLineAll,
Replace,
ReplaceAll,
}
pub fn insert_before(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::InsertBefore)
}
pub fn insert_before_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::InsertBeforeAll)
}
pub fn insert_after(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::InsertAfter)
}
pub fn insert_after_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::InsertBeforeAll)
}
pub fn prepend(file: &Path, text: &str) -> Result<()> {
modify(file, None, Some(text), Operation::Prepend)?;
Ok(())
}
pub fn append(file: &Path, text: &str) -> Result<()> {
modify(file, None, Some(text), Operation::Append)?;
Ok(())
}
pub fn remove_line(file: &Path, regex: &Regex) -> Result<bool> {
modify(file, Some(regex), None, Operation::RemoveLine)
}
pub fn remove_line_all(file: &Path, regex: &Regex) -> Result<bool> {
modify(file, Some(regex), None, Operation::RemoveLineAll)
}
pub fn contains(file: &Path, regex: &Regex) -> Result<bool> {
let content = std::fs::read_to_string(file)?;
for line in content.lines() {
if regex.is_match(line) {
return Ok(true);
}
}
Ok(false)
}
pub fn replace(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::Replace)
}
pub fn replace_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
modify(file, Some(regex), Some(text), Operation::ReplaceAll)
}
fn modify(
file: &Path,
regex: Option<&Regex>,
text: Option<&str>,
operation: Operation,
) -> Result<bool> {
let mut output = String::new();
let orig = std::fs::read_to_string(file)?;
let mut modified = false;
let do_all = match operation {
Operation::InsertAfterAll
| Operation::InsertBeforeAll
| Operation::RemoveLineAll
| Operation::ReplaceAll => true,
_ => false,
};
for line in orig.lines() {
if modified && !do_all {
output += line;
} else {
match operation {
Operation::Prepend => {
output += text.unwrap();
output += line;
modified = true;
}
Operation::Append => {
modified = true;
}
_ => {
if regex.unwrap().is_match(line) {
let m = regex.unwrap().find(line).unwrap();
match operation {
Operation::InsertBefore | Operation::InsertBeforeAll => {
let (first, second) = line.split_at(m.start());
output += &format!("{}{}{}", first, text.unwrap(), second);
}
Operation::InsertAfter | Operation::InsertAfterAll => {
let (first, second) = line.split_at(m.end());
output += &format!("{}{}{}", first, text.unwrap(), second);
}
Operation::RemoveLine | Operation::RemoveLineAll => {
continue;
}
Operation::Replace => {
output += ®ex.unwrap().replace(line, text.unwrap());
}
Operation::ReplaceAll => {
output += ®ex.unwrap().replace_all(line, text.unwrap());
}
_ => unreachable!(),
}
modified = true;
} else {
output += line;
}
}
}
}
output += "\n";
}
if operation == Operation::Append {
output += text.unwrap();
}
std::fs::write(file, output)?;
Ok(modified)
}