rsm: Split First into two options, and generate Set earlier during parsing.
This commit is contained in:
parent
d680c31cf9
commit
61839042bd
1 changed files with 34 additions and 49 deletions
83
src/rsm.rs
83
src/rsm.rs
|
@ -12,18 +12,13 @@ use error::Error;
|
||||||
|
|
||||||
use ns;
|
use ns;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct First {
|
|
||||||
pub index: Option<usize>,
|
|
||||||
pub id: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Set {
|
pub struct Set {
|
||||||
pub after: Option<String>,
|
pub after: Option<String>,
|
||||||
pub before: Option<String>,
|
pub before: Option<String>,
|
||||||
pub count: Option<usize>,
|
pub count: Option<usize>,
|
||||||
pub first: Option<First>,
|
pub first: Option<String>,
|
||||||
|
pub first_index: Option<usize>,
|
||||||
pub index: Option<usize>,
|
pub index: Option<usize>,
|
||||||
pub last: Option<String>,
|
pub last: Option<String>,
|
||||||
pub max: Option<usize>,
|
pub max: Option<usize>,
|
||||||
|
@ -36,68 +31,61 @@ impl<'a> TryFrom<&'a Element> for Set {
|
||||||
if !elem.is("set", ns::RSM) {
|
if !elem.is("set", ns::RSM) {
|
||||||
return Err(Error::ParseError("This is not a RSM element."));
|
return Err(Error::ParseError("This is not a RSM element."));
|
||||||
}
|
}
|
||||||
let mut after = None;
|
let mut set = Set {
|
||||||
let mut before = None;
|
after: None,
|
||||||
let mut count = None;
|
before: None,
|
||||||
let mut first = None;
|
count: None,
|
||||||
let mut index = None;
|
first: None,
|
||||||
let mut last = None;
|
first_index: None,
|
||||||
let mut max = None;
|
index: None,
|
||||||
|
last: None,
|
||||||
|
max: None,
|
||||||
|
};
|
||||||
for child in elem.children() {
|
for child in elem.children() {
|
||||||
if child.is("after", ns::RSM) {
|
if child.is("after", ns::RSM) {
|
||||||
if after.is_some() {
|
if set.after.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one after."));
|
return Err(Error::ParseError("Set can’t have more than one after."));
|
||||||
}
|
}
|
||||||
after = Some(child.text());
|
set.after = Some(child.text());
|
||||||
} else if child.is("before", ns::RSM) {
|
} else if child.is("before", ns::RSM) {
|
||||||
if before.is_some() {
|
if set.before.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one before."));
|
return Err(Error::ParseError("Set can’t have more than one before."));
|
||||||
}
|
}
|
||||||
before = Some(child.text());
|
set.before = Some(child.text());
|
||||||
} else if child.is("count", ns::RSM) {
|
} else if child.is("count", ns::RSM) {
|
||||||
if count.is_some() {
|
if set.count.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one count."));
|
return Err(Error::ParseError("Set can’t have more than one count."));
|
||||||
}
|
}
|
||||||
count = Some(child.text().parse()?);
|
set.count = Some(child.text().parse()?);
|
||||||
} else if child.is("first", ns::RSM) {
|
} else if child.is("first", ns::RSM) {
|
||||||
if first.is_some() {
|
if set.first.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one first."));
|
return Err(Error::ParseError("Set can’t have more than one first."));
|
||||||
}
|
}
|
||||||
first = Some(First {
|
set.first_index = match child.attr("index") {
|
||||||
index: match child.attr("index") {
|
Some(index) => Some(index.parse()?),
|
||||||
Some(index) => Some(index.parse()?),
|
None => None,
|
||||||
None => None,
|
};
|
||||||
},
|
set.first = Some(child.text());
|
||||||
id: child.text(),
|
|
||||||
});
|
|
||||||
} else if child.is("index", ns::RSM) {
|
} else if child.is("index", ns::RSM) {
|
||||||
if index.is_some() {
|
if set.index.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one index."));
|
return Err(Error::ParseError("Set can’t have more than one index."));
|
||||||
}
|
}
|
||||||
index = Some(child.text().parse()?);
|
set.index = Some(child.text().parse()?);
|
||||||
} else if child.is("last", ns::RSM) {
|
} else if child.is("last", ns::RSM) {
|
||||||
if last.is_some() {
|
if set.last.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one last."));
|
return Err(Error::ParseError("Set can’t have more than one last."));
|
||||||
}
|
}
|
||||||
last = Some(child.text());
|
set.last = Some(child.text());
|
||||||
} else if child.is("max", ns::RSM) {
|
} else if child.is("max", ns::RSM) {
|
||||||
if max.is_some() {
|
if set.max.is_some() {
|
||||||
return Err(Error::ParseError("Set can’t have more than one max."));
|
return Err(Error::ParseError("Set can’t have more than one max."));
|
||||||
}
|
}
|
||||||
max = Some(child.text().parse()?);
|
set.max = Some(child.text().parse()?);
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::ParseError("Unknown child in set element."));
|
return Err(Error::ParseError("Unknown child in set element."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Set {
|
Ok(set)
|
||||||
after: after,
|
|
||||||
before: before,
|
|
||||||
count: count,
|
|
||||||
first: first,
|
|
||||||
index: index,
|
|
||||||
last: last,
|
|
||||||
max: max,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,14 +104,10 @@ impl<'a> Into<Element> for &'a Set {
|
||||||
elem.append_child(Element::builder("count").ns(ns::RSM).append(format!("{}", self.count.unwrap())).build());
|
elem.append_child(Element::builder("count").ns(ns::RSM).append(format!("{}", self.count.unwrap())).build());
|
||||||
}
|
}
|
||||||
if self.first.is_some() {
|
if self.first.is_some() {
|
||||||
let first = self.first.clone().unwrap();
|
|
||||||
elem.append_child(Element::builder("first")
|
elem.append_child(Element::builder("first")
|
||||||
.ns(ns::RSM)
|
.ns(ns::RSM)
|
||||||
.attr("index", match first.index {
|
.attr("index", self.first_index.map(|index| format!("{}", index)))
|
||||||
Some(index) => Some(format!("{}", index)),
|
.append(self.first.clone()).build());
|
||||||
None => None,
|
|
||||||
})
|
|
||||||
.append(first.id.clone()).build());
|
|
||||||
}
|
}
|
||||||
if self.index.is_some() {
|
if self.index.is_some() {
|
||||||
elem.append_child(Element::builder("index").ns(ns::RSM).append(format!("{}", self.index.unwrap())).build());
|
elem.append_child(Element::builder("index").ns(ns::RSM).append(format!("{}", self.index.unwrap())).build());
|
||||||
|
@ -188,6 +172,7 @@ mod tests {
|
||||||
before: None,
|
before: None,
|
||||||
count: None,
|
count: None,
|
||||||
first: None,
|
first: None,
|
||||||
|
first_index: None,
|
||||||
index: None,
|
index: None,
|
||||||
last: None,
|
last: None,
|
||||||
max: None,
|
max: None,
|
||||||
|
|
Loading…
Reference in a new issue