Skip to content

Commit 3dd254e

Browse files
committed
Add search/replace functionality
1 parent 7ca9fe2 commit 3dd254e

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ use quick_xml::events::Event;
99
use yaml_rust::YamlLoader;
1010

1111
#[derive(Debug)]
12-
struct Column {
12+
struct Column<'a> {
1313
name: String,
1414
path: String,
1515
value: String,
16+
search: Option<&'a str>,
17+
replace: Option<&'a str>,
1618
raw: bool
1719
}
1820

@@ -41,14 +43,16 @@ fn main() {
4143
let mut columns = Vec::new();
4244

4345
for col in colspec {
44-
let name = col["name"].as_str().unwrap();
46+
let name = col["name"].as_str().expect("Column has no 'name' entry in configuration file");
47+
let search = col["search"].as_str();
48+
let replace = col["replace"].as_str();
4549
let mut raw = false;
4650
if !col["raw"].is_badvalue() { raw = col["raw"].as_bool().unwrap() }
4751
let colpath = col["path"].as_str().unwrap();
48-
// println!("Column name {} path {}", name, colpath);
52+
// if search != None && replace != None { eprintln!("Column name {} search {} replace {}", name, search.unwrap(), replace.unwrap()); }
4953
let mut path = String::from(rowpath);
5054
path.push_str(colpath);
51-
columns.push(Column { name: name.to_string(), path: path, value: String::new(), raw: raw });
55+
columns.push(Column { name: name.to_string(), path, value: String::new(), search, replace, raw });
5256
}
5357

5458
let mut raw = false;
@@ -81,7 +85,7 @@ fn main() {
8185
}
8286
for i in 0..columns.len() {
8387
if path == columns[i].path {
84-
columns[i].value.push_str(&e.unescape_and_decode(&reader).unwrap());
88+
columns[i].value.push_str(&e.unescape_and_decode(&reader).unwrap().replace("\\", "\\\\"));
8589
}
8690
}
8791
},
@@ -91,6 +95,9 @@ fn main() {
9195
if i > 0 { print!("\t"); }
9296
if columns[i].value.is_empty() { print!("\\N"); }
9397
else {
98+
if columns[i].search != None && columns[i].replace != None {
99+
columns[i].value = columns[i].value.replace(columns[i].search.unwrap(), columns[i].replace.unwrap());
100+
}
94101
print!("{}", columns[i].value);
95102
columns[i].value.clear();
96103
}
@@ -104,6 +111,9 @@ fn main() {
104111
for i in 0..columns.len() {
105112
if path == columns[i].path {
106113
raw = false;
114+
if columns[i].search != None && columns[i].replace != None {
115+
rawstr = rawstr.replace(columns[i].search.unwrap(), columns[i].replace.unwrap());
116+
}
107117
columns[i].value.push_str(&rawstr);
108118
rawstr.clear();
109119
break;

0 commit comments

Comments
 (0)