Skip to content

Commit 5ed68f8

Browse files
committed
Support using an attribute for column value
1 parent e1f9ea2 commit 5ed68f8

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/main.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ struct Column<'a> {
3737
name: String,
3838
path: String,
3939
value: RefCell<String>,
40+
attr: Option<&'a str>,
4041
convert: Option<&'a str>,
4142
search: Option<&'a str>,
4243
replace: Option<&'a str>,
4344
consol: Option<&'a str>,
4445
subtable: Option<Table<'a>>
4546
}
47+
impl std::fmt::Debug for Column<'_> {
48+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49+
f.debug_struct("Column")
50+
.field("name", &self.name)
51+
.field("path", &self.path)
52+
.field("attr", &self.attr)
53+
.finish()
54+
}
55+
}
4656

4757
struct Geometry {
4858
gtype: u8,
@@ -101,11 +111,13 @@ fn add_table<'a>(rowpath: &str, outfile: Option<&str>, colspec: &'a Vec<Yaml>) -
101111
Some(add_table(&path, Some(&file), col["cols"].as_vec().expect("Subtable 'cols' entry is not an array")))
102112
}
103113
};
114+
let attr = col["attr"].as_str();
104115
let convert = col["convert"].as_str();
105116
let search = col["search"].as_str();
106117
let replace = col["replace"].as_str();
107118
let consol = col["consol"].as_str();
108-
table.columns.push(Column { name: name.to_string(), path, value: RefCell::new(String::new()), convert, search, replace, consol, subtable });
119+
let column = Column { name: name.to_string(), path, value: RefCell::new(String::new()), attr, convert, search, replace, consol, subtable };
120+
table.columns.push(column);
109121
}
110122
table
111123
}
@@ -125,7 +137,8 @@ fn main() -> std::io::Result<()> {
125137

126138
let mut reader;
127139
reader = Reader::from_file(&args[2]).unwrap();
128-
reader.trim_text(true);
140+
reader.trim_text(true)
141+
.expand_empty_elements(true);
129142

130143
let mut path = String::new();
131144
let mut buf = Vec::new();
@@ -208,6 +221,26 @@ fn main() -> std::io::Result<()> {
208221
table = &table.columns[i].subtable.as_ref().unwrap();
209222
break;
210223
}
224+
if let Some(request) = table.columns[i].attr {
225+
for res in e.attributes() {
226+
if let Ok(attr) = res {
227+
if let Ok(key) = reader.decode(attr.key) {
228+
if key == request {
229+
if let Ok(value) = reader.decode(&attr.value) {
230+
table.columns[i].value.borrow_mut().push_str(value)
231+
}
232+
else { eprintln!("Failed to decode attribute {} for column {}", request, table.columns[i].name); }
233+
break;
234+
}
235+
}
236+
else { eprintln!("Failed to decode an attribute for column {}", table.columns[i].name); }
237+
}
238+
else { eprintln!("Error reading attributes for column {}", table.columns[i].name); }
239+
}
240+
if table.columns[i].value.borrow().is_empty() {
241+
eprintln!("Column {} requested attribute {} not found", table.columns[i].name, request);
242+
}
243+
}
211244
match table.columns[i].convert {
212245
None => (),
213246
Some("xml-to-text") => xmltotext = true,
@@ -235,6 +268,7 @@ fn main() -> std::io::Result<()> {
235268
}
236269
for i in 0..table.columns.len() {
237270
if path == table.columns[i].path {
271+
if table.columns[i].attr.is_some() { break; }
238272
match table.columns[i].consol {
239273
None => {
240274
if !table.columns[i].value.borrow().is_empty() {

0 commit comments

Comments
 (0)