Skip to content

Commit 5f79740

Browse files
committed
Migration to roxmltree
1 parent 3dd4d53 commit 5f79740

File tree

10 files changed

+238
-208
lines changed

10 files changed

+238
-208
lines changed

Cargo.lock

Lines changed: 7 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cmsis-pack/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ edition = "2018"
1818
bytes = "1.0"
1919
futures = "0.3.8"
2020
log = "0.4.8"
21-
minidom = "0.12.0"
21+
roxmltree = "0.20.0"
2222
serde = { version = "1.0.118", features = ["derive"] }
2323
serde_json = "1.0"
2424
tokio = { version = "1.0", features = ["macros", "rt"] }
25-
reqwest = { version = "0.12.0", default_features = false, features = [
25+
reqwest = { version = "0.12.0", default-features = false, features = [
2626
"rustls-tls-native-roots",
2727
"trust-dns",
2828
"stream",

rust/cmsis-pack/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub mod utils;
88

99
extern crate futures;
1010
extern crate log;
11-
extern crate minidom;
1211
extern crate reqwest;
12+
extern crate roxmltree;
1313
extern crate serde;
1414
extern crate serde_json;
1515
extern crate tokio;

rust/cmsis-pack/src/pack_index/mod.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::prelude::*;
22
use anyhow::Error;
3-
use minidom::Element;
3+
use roxmltree::Node;
44

55
#[derive(Debug, Clone)]
66
pub struct PdscRef {
@@ -31,7 +31,7 @@ pub struct Vidx {
3131
}
3232

3333
impl FromElem for PdscRef {
34-
fn from_elem(e: &Element) -> Result<Self, Error> {
34+
fn from_elem(e: &Node) -> Result<Self, Error> {
3535
assert_root_name(e, "pdsc")?;
3636
Ok(Self {
3737
url: attr_map(e, "url")?,
@@ -47,7 +47,7 @@ impl FromElem for PdscRef {
4747
}
4848

4949
impl FromElem for Pidx {
50-
fn from_elem(e: &Element) -> Result<Self, Error> {
50+
fn from_elem(e: &Node) -> Result<Self, Error> {
5151
assert_root_name(e, "pidx")?;
5252
Ok(Self {
5353
url: attr_map(e, "url")?,
@@ -58,20 +58,35 @@ impl FromElem for Pidx {
5858
}
5959

6060
impl FromElem for Vidx {
61-
fn from_elem(root: &Element) -> Result<Self, Error> {
61+
fn from_elem(root: &Node) -> Result<Self, Error> {
6262
assert_root_name(root, "index")?;
6363
let vendor = child_text(root, "vendor")?;
6464
let url = child_text(root, "url")?;
65+
66+
let mut timestamp: Option<String> = None;
67+
let mut vendor_index: Vec<Pidx> = Vec::new();
68+
let mut pdsc_index: Vec<PdscRef> = Vec::new();
69+
for child in root.children() {
70+
match child.tag_name().name() {
71+
"timestamp" => {
72+
timestamp = Some(child).map(|e| e.text().unwrap_or_default().to_string())
73+
}
74+
"vindex" => {
75+
vendor_index = Pidx::vec_from_children(child.children());
76+
}
77+
"pindex" => {
78+
pdsc_index = PdscRef::vec_from_children(child.children());
79+
}
80+
_ => continue,
81+
}
82+
}
83+
6584
Ok(Vidx {
6685
vendor,
6786
url,
68-
timestamp: get_child_no_ns(root, "timestamp").map(Element::text),
69-
vendor_index: get_child_no_ns(root, "vindex")
70-
.map(|e| Pidx::vec_from_children(e.children()))
71-
.unwrap_or_default(),
72-
pdsc_index: get_child_no_ns(root, "pindex")
73-
.map(|e| PdscRef::vec_from_children(e.children()))
74-
.unwrap_or_default(),
87+
timestamp,
88+
vendor_index,
89+
pdsc_index,
7590
})
7691
}
7792
}
@@ -200,5 +215,9 @@ mod test {
200215
let response = Vidx::from_string(good_string).unwrap();
201216
assert_eq!(response.vendor, String::from("Vendor"));
202217
assert_eq!(response.url, "Url");
218+
assert_eq!(
219+
response.timestamp,
220+
Some(String::from("Fri Sep 1 13:26:41 CDT"))
221+
);
203222
}
204223
}

rust/cmsis-pack/src/pdsc/component.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::path::PathBuf;
22
use std::str::FromStr;
33

44
use anyhow::{format_err, Error};
5-
use minidom::Element;
5+
use roxmltree::Node;
66
use serde::Serialize;
77

88
use crate::utils::prelude::*;
@@ -79,7 +79,7 @@ pub struct FileRef {
7979
}
8080

8181
impl FromElem for FileRef {
82-
fn from_elem(e: &Element) -> Result<Self, Error> {
82+
fn from_elem(e: &Node) -> Result<Self, Error> {
8383
assert_root_name(e, "file")?;
8484
Ok(Self {
8585
path: attr_map(e, "name")?,
@@ -112,7 +112,7 @@ pub struct ComponentBuilder {
112112
}
113113

114114
impl FromElem for ComponentBuilder {
115-
fn from_elem(e: &Element) -> Result<Self, Error> {
115+
fn from_elem(e: &Node) -> Result<Self, Error> {
116116
assert_root_name(e, "component")?;
117117
let vendor: Option<String> = attr_map(e, "Cvendor").ok();
118118
let class: Option<String> = attr_map(e, "Cclass").ok();
@@ -122,18 +122,18 @@ impl FromElem for ComponentBuilder {
122122
let class_string = class.clone().unwrap_or_else(|| "Class".into());
123123
let group_string = group.clone().unwrap_or_else(|| "Group".into());
124124
let sub_group_string = sub_group.clone().unwrap_or_else(|| "SubGroup".into());
125-
let files = get_child_no_ns(e, "files")
126-
.map(move |child| {
127-
log::debug!(
128-
"Working on {}::{}::{}::{}",
129-
vendor_string,
130-
class_string,
131-
group_string,
132-
sub_group_string,
133-
);
134-
FileRef::vec_from_children(child.children())
135-
})
136-
.unwrap_or_default();
125+
let files = if let Some(node) = e.children().find(|c| c.tag_name().name() == "files") {
126+
log::debug!(
127+
"Working on {}::{}::{}::{}",
128+
vendor_string,
129+
class_string,
130+
group_string,
131+
sub_group_string,
132+
);
133+
FileRef::vec_from_children(node.children())
134+
} else {
135+
Vec::new()
136+
};
137137
Ok(Self {
138138
vendor,
139139
class,
@@ -187,7 +187,7 @@ impl Bundle {
187187
}
188188

189189
impl FromElem for Bundle {
190-
fn from_elem(e: &Element) -> Result<Self, Error> {
190+
fn from_elem(e: &Node) -> Result<Self, Error> {
191191
assert_root_name(e, "bundle")?;
192192
let name: String = attr_map(e, "Cbundle")?;
193193
let class: String = attr_map(e, "Cclass")?;
@@ -198,8 +198,8 @@ impl FromElem for Bundle {
198198
let components = e
199199
.children()
200200
.filter_map(move |chld| {
201-
if chld.name() == "component" {
202-
ComponentBuilder::from_elem(chld).ok()
201+
if chld.tag_name().name() == "component" {
202+
ComponentBuilder::from_elem(&chld).ok()
203203
} else {
204204
None
205205
}
@@ -217,10 +217,8 @@ impl FromElem for Bundle {
217217
}
218218
}
219219

220-
fn child_to_component_iter(
221-
e: &Element,
222-
) -> Result<Box<dyn Iterator<Item = ComponentBuilder>>, Error> {
223-
match e.name() {
220+
fn child_to_component_iter(e: &Node) -> Result<Box<dyn Iterator<Item = ComponentBuilder>>, Error> {
221+
match e.tag_name().name() {
224222
"bundle" => {
225223
let bundle = Bundle::from_elem(e)?;
226224
Ok(Box::new(bundle.into_components().into_iter()))
@@ -230,8 +228,9 @@ fn child_to_component_iter(
230228
Ok(Box::new(Some(component).into_iter()))
231229
}
232230
_ => Err(format_err!(
233-
"element of name {} is not allowed as a descendant of components",
234-
e.name()
231+
"element of name {} is not allowed as a descendant of components ({:?})",
232+
e.tag_name().name(),
233+
e
235234
)),
236235
}
237236
}
@@ -240,11 +239,12 @@ fn child_to_component_iter(
240239
pub struct ComponentBuilders(pub(crate) Vec<ComponentBuilder>);
241240

242241
impl FromElem for ComponentBuilders {
243-
fn from_elem(e: &Element) -> Result<Self, Error> {
242+
fn from_elem(e: &Node) -> Result<Self, Error> {
244243
assert_root_name(e, "components")?;
245244
Ok(ComponentBuilders(
246245
e.children()
247-
.flat_map(move |c| match child_to_component_iter(c) {
246+
.filter(|e| e.is_element())
247+
.flat_map(move |c| match child_to_component_iter(&c) {
248248
Ok(iter) => iter,
249249
Err(e) => {
250250
log::error!("when trying to parse component: {}", e);

rust/cmsis-pack/src/pdsc/condition.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Error;
2-
use minidom::Element;
2+
use roxmltree::Node;
33

44
use crate::utils::prelude::*;
55

@@ -12,7 +12,7 @@ pub struct ConditionComponent {
1212
}
1313

1414
impl FromElem for ConditionComponent {
15-
fn from_elem(e: &Element) -> Result<Self, Error> {
15+
fn from_elem(e: &Node) -> Result<Self, Error> {
1616
Ok(ConditionComponent {
1717
device_family: attr_map(e, "Dfamily").ok(),
1818
device_sub_family: attr_map(e, "Dsubfamily").ok(),
@@ -31,13 +31,13 @@ pub struct Condition {
3131
}
3232

3333
impl FromElem for Condition {
34-
fn from_elem(e: &Element) -> Result<Self, Error> {
34+
fn from_elem(e: &Node) -> Result<Self, Error> {
3535
assert_root_name(e, "condition")?;
3636
let mut accept = Vec::new();
3737
let mut deny = Vec::new();
3838
let mut require = Vec::new();
39-
for elem in e.children() {
40-
match elem.name() {
39+
for elem in e.children().filter(|e| e.is_element()) {
40+
match elem.tag_name().name() {
4141
"accept" => {
4242
accept.push(ConditionComponent::from_elem(e)?);
4343
}
@@ -49,7 +49,10 @@ impl FromElem for Condition {
4949
}
5050
"description" => {}
5151
_ => {
52-
log::warn!("Found unkonwn element {} in components", elem.name());
52+
log::warn!(
53+
"Found unkonwn element {} in components",
54+
elem.tag_name().name()
55+
);
5356
}
5457
}
5558
}
@@ -66,11 +69,12 @@ impl FromElem for Condition {
6669
pub struct Conditions(pub Vec<Condition>);
6770

6871
impl FromElem for Conditions {
69-
fn from_elem(e: &Element) -> Result<Self, Error> {
72+
fn from_elem(e: &Node) -> Result<Self, Error> {
7073
assert_root_name(e, "conditions")?;
7174
Ok(Conditions(
7275
e.children()
73-
.flat_map(|c| Condition::from_elem(c).ok_warn())
76+
.filter(|e| e.is_element())
77+
.flat_map(|c| Condition::from_elem(&c).ok_warn())
7478
.collect(),
7579
))
7680
}

0 commit comments

Comments
 (0)