[librsvg: 4/5] allowed_url: do not use Href::with_fragment



commit 4850162f05077c2356bba1db03ecfd0a42b7b930
Author: Paolo Borelli <pborelli gnome org>
Date:   Thu Jan 17 12:05:55 2019 +0100

    allowed_url: do not use Href::with_fragment
    
    A plain match in the callers is actually shorter and more clear

 rsvg_internals/src/allowed_url.rs | 33 +++------------------------------
 rsvg_internals/src/handle.rs      |  7 ++-----
 rsvg_internals/src/iri.rs         | 10 +++-------
 3 files changed, 8 insertions(+), 42 deletions(-)
---
diff --git a/rsvg_internals/src/allowed_url.rs b/rsvg_internals/src/allowed_url.rs
index 232b419c..f409fada 100644
--- a/rsvg_internals/src/allowed_url.rs
+++ b/rsvg_internals/src/allowed_url.rs
@@ -183,12 +183,9 @@ impl Fragment {
     }
 
     pub fn parse(href: &str) -> Result<Fragment, HrefError> {
-        let href = Href::with_fragment(href)?;
-
-        if let Href::WithFragment(f) = href {
-            Ok(f)
-        } else {
-            unreachable!();
+        match Href::parse(&href)? {
+            Href::PlainUrl(_) => Err(HrefError::FragmentRequired),
+            Href::WithFragment(f) => Ok(f),
         }
     }
 
@@ -239,15 +236,6 @@ impl Href {
             (_, _) => Err(HrefError::ParseError),
         }
     }
-
-    pub fn with_fragment(href: &str) -> Result<Href, HrefError> {
-        use self::Href::*;
-
-        match Href::parse(href)? {
-            PlainUrl(_) => Err(HrefError::FragmentRequired),
-            r @ WithFragment(_) => Ok(r),
-        }
-    }
 }
 
 #[cfg(test)]
@@ -377,21 +365,6 @@ mod tests {
         assert_eq!(Href::parse("uri#"), Err(HrefError::ParseError));
     }
 
-    #[test]
-    fn href_with_fragment() {
-        assert_eq!(
-            Href::with_fragment("#foo").unwrap(),
-            Href::WithFragment(Fragment::new(None, "foo".to_string()))
-        );
-
-        assert_eq!(
-            Href::with_fragment("uri#foo").unwrap(),
-            Href::WithFragment(Fragment::new(Some("uri".to_string()), "foo".to_string()))
-        );
-
-        assert_eq!(Href::with_fragment("uri"), Err(HrefError::FragmentRequired));
-    }
-
     #[test]
     fn parses_fragment() {
         assert_eq!(
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index ca49adad..d00f37bc 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -448,9 +448,8 @@ impl Handle {
         let svg_ref = self.svg.borrow();
         let svg = svg_ref.as_ref().unwrap();
 
-        let href = Href::with_fragment(id).map_err(DefsLookupErrorKind::HrefError)?;
-
-        match href {
+        match Href::parse(&id).map_err(DefsLookupErrorKind::HrefError)? {
+            Href::PlainUrl(_) => Err(DefsLookupErrorKind::CannotLookupExternalReferences),
             Href::WithFragment(fragment) => {
                 if let Some(uri) = fragment.uri() {
                     // The public APIs to get geometries of individual elements, or to render
@@ -479,8 +478,6 @@ impl Handle {
                     None => Err(DefsLookupErrorKind::NotFound),
                 }
             }
-
-            _ => unreachable!(), // we explicitly requested a with_fragment after all
         }
     }
 
diff --git a/rsvg_internals/src/iri.rs b/rsvg_internals/src/iri.rs
index 35f7d884..45a45f9d 100644
--- a/rsvg_internals/src/iri.rs
+++ b/rsvg_internals/src/iri.rs
@@ -48,13 +48,9 @@ impl Parse for IRI {
                 .expect_exhausted()
                 .map_err(|_| ParseError::new("expected url"))?;
 
-            let href =
-                Href::with_fragment(&url).map_err(|_| ParseError::new("could not parse href"))?;
-
-            if let Href::WithFragment(fragment) = href {
-                Ok(IRI::Resource(fragment))
-            } else {
-                Err(ParseError::new("href requires a fragment identifier"))
+            match Href::parse(&url).map_err(|_| ParseError::new("could not parse href"))? {
+                Href::PlainUrl(_) => Err(ParseError::new("href requires a fragment identifier")),
+                Href::WithFragment(f) => Ok(IRI::Resource(f)),
             }
         }
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]