[librsvg] xml: make steal_result return a Result
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] xml: make steal_result return a Result
- Date: Sun, 13 Jan 2019 17:32:00 +0000 (UTC)
commit 39e121469820a42acdb78fbc053805d64297cb3b
Author: Paolo Borelli <pborelli gnome org>
Date: Sun Jan 13 12:18:42 2019 +0100
xml: make steal_result return a Result
This is not only cleaner, but also safer since callers do not
have to remember to call validate_tree manually.
rsvg_internals/src/handle.rs | 6 +-----
rsvg_internals/src/svg.rs | 4 +---
rsvg_internals/src/xml.rs | 24 +++++++++---------------
3 files changed, 11 insertions(+), 23 deletions(-)
---
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index b6d1bd77..fa26b27b 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -274,12 +274,8 @@ impl Handle {
fn close_internal(&mut self) -> Result<(), LoadingError> {
let mut r = self.load.borrow_mut();
let mut load = r.take().unwrap();
-
let mut xml = load.close()?;
-
- xml.validate_tree()?;
-
- *self.svg.borrow_mut() = Some(Rc::new(xml.steal_result()));
+ *self.svg.borrow_mut() = Some(Rc::new(xml.steal_result()?));
Ok(())
}
diff --git a/rsvg_internals/src/svg.rs b/rsvg_internals/src/svg.rs
index 4866392f..848700f6 100644
--- a/rsvg_internals/src/svg.rs
+++ b/rsvg_internals/src/svg.rs
@@ -51,9 +51,7 @@ impl Svg {
xml_state_load_from_possibly_compressed_stream(&mut xml, load_flags, stream, cancellable)?;
- xml.validate_tree()?;
-
- Ok(xml.steal_result())
+ xml.steal_result()
}
pub fn lookup(&self, fragment: &Fragment) -> Option<RsvgNode> {
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index fd1d8a5a..d02a92c6 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -113,26 +113,20 @@ impl XmlState {
self.tree = Some(Tree::new(root));
}
- pub fn validate_tree(&self) -> Result<(), LoadingError> {
- if let Some(ref tree) = self.tree {
- if tree.root_is_svg() {
- Ok(())
- } else {
+ pub fn steal_result(&mut self) -> Result<Svg, LoadingError> {
+ match self.tree {
+ None => Err(LoadingError::SvgHasNoElements),
+ Some(ref tree) if !tree.root_is_svg() => {
Err(LoadingError::RootElementIsNotSvg)
}
- } else {
- Err(LoadingError::SvgHasNoElements)
+ _ => Ok(Svg::new(
+ self.tree.take().unwrap(),
+ self.ids.take().unwrap(),
+ self.load_options.clone(),
+ )),
}
}
- pub fn steal_result(&mut self) -> Svg {
- Svg::new(
- self.tree.take().unwrap(),
- self.ids.take().unwrap(),
- self.load_options.clone(),
- )
- }
-
fn context(&self) -> Context {
// We can unwrap since the stack is never empty
self.context_stack.last().unwrap().clone()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]