[librsvg: 1/10] errors: move AllowedUrlError together with the other errors




commit d001ce723bcc4927f08ea30ce851baf83ec99d70
Author: Paolo Borelli <pborelli gnome org>
Date:   Sat Dec 12 09:48:21 2020 +0100

    errors: move AllowedUrlError together with the other errors

 src/document.rs     |  4 ++--
 src/error.rs        | 46 ++++++++++++++++++++++++++++++++++++
 src/url_resolver.rs | 67 +++++++++--------------------------------------------
 3 files changed, 59 insertions(+), 58 deletions(-)
---
diff --git a/src/document.rs b/src/document.rs
index 19d01bf5..fff43d3d 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -11,13 +11,13 @@ use std::rc::Rc;
 
 use crate::attributes::Attributes;
 use crate::css::{self, Origin, Stylesheet};
-use crate::error::{AcquireError, LoadingError};
+use crate::error::{AcquireError, AllowedUrlError, LoadingError};
 use crate::handle::LoadOptions;
 use crate::io::{self, BinaryData};
 use crate::limits;
 use crate::node::{Node, NodeBorrow, NodeData};
 use crate::surface_utils::shared_surface::SharedImageSurface;
-use crate::url_resolver::{AllowedUrl, AllowedUrlError, Fragment, UrlResolver};
+use crate::url_resolver::{AllowedUrl, Fragment, UrlResolver};
 use crate::xml::xml_load_from_possibly_compressed_stream;
 
 static UA_STYLESHEETS: Lazy<Vec<Stylesheet>> = Lazy::new(|| {
diff --git a/src/error.rs b/src/error.rs
index 4fdcc7b9..e2a53c50 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -278,6 +278,52 @@ impl<'i, O> AttributeResultExt<O> for Result<O, ParseError<'i>> {
     }
 }
 
+/// Errors returned when resolving an URL
+#[derive(Debug, Clone)]
+pub enum AllowedUrlError {
+    /// parsing error from `Url::parse()`
+    HrefParseError(url::ParseError),
+
+    /// A base file/uri was not set
+    BaseRequired,
+
+    /// Cannot reference a file with a different URI scheme from the base file
+    DifferentURISchemes,
+
+    /// Some scheme we don't allow loading
+    DisallowedScheme,
+
+    /// The requested file is not in the same directory as the base file,
+    /// or in one directory below the base file.
+    NotSiblingOrChildOfBaseFile,
+
+    /// Error when obtaining the file path or the base file path
+    InvalidPath,
+
+    /// The base file cannot be the root of the file system
+    BaseIsRoot,
+
+    /// Error when canonicalizing either the file path or the base file path
+    CanonicalizationError,
+}
+
+impl fmt::Display for AllowedUrlError {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        match *self {
+            AllowedUrlError::HrefParseError(e) => write!(f, "href parse error: {}", e),
+            AllowedUrlError::BaseRequired => write!(f, "base required"),
+            AllowedUrlError::DifferentURISchemes => write!(f, "different URI schemes"),
+            AllowedUrlError::DisallowedScheme => write!(f, "disallowed scheme"),
+            AllowedUrlError::NotSiblingOrChildOfBaseFile => {
+                write!(f, "not sibling or child of base file")
+            }
+            AllowedUrlError::InvalidPath => write!(f, "invalid path"),
+            AllowedUrlError::BaseIsRoot => write!(f, "base is root"),
+            AllowedUrlError::CanonicalizationError => write!(f, "canonicalization error"),
+        }
+    }
+}
+
 /// Errors returned when creating an `Href` out of a string
 #[derive(Debug, Clone)]
 pub enum HrefError {
diff --git a/src/url_resolver.rs b/src/url_resolver.rs
index 928135d8..52eae3f0 100644
--- a/src/url_resolver.rs
+++ b/src/url_resolver.rs
@@ -6,7 +6,7 @@ use std::ops::Deref;
 use std::path::{Path, PathBuf};
 use url::Url;
 
-use crate::error::HrefError;
+use crate::error::{AllowedUrlError, HrefError};
 
 /// Currently only contains the base URL.
 ///
@@ -102,34 +102,6 @@ impl UrlResolver {
 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
 pub struct AllowedUrl(Url);
 
-#[derive(Debug, PartialEq)]
-pub enum AllowedUrlError {
-    /// parsing error from `Url::parse()`
-    HrefParseError(url::ParseError),
-
-    /// A base file/uri was not set
-    BaseRequired,
-
-    /// Cannot reference a file with a different URI scheme from the base file
-    DifferentURISchemes,
-
-    /// Some scheme we don't allow loading
-    DisallowedScheme,
-
-    /// The requested file is not in the same directory as the base file,
-    /// or in one directory below the base file.
-    NotSiblingOrChildOfBaseFile,
-
-    /// Error when obtaining the file path or the base file path
-    InvalidPath,
-
-    /// The base file cannot be the root of the file system
-    BaseIsRoot,
-
-    /// Error when canonicalizing either the file path or the base file path
-    CanonicalizationError,
-}
-
 impl Deref for AllowedUrl {
     type Target = Url;
 
@@ -144,23 +116,6 @@ impl fmt::Display for AllowedUrl {
     }
 }
 
-impl fmt::Display for AllowedUrlError {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        match *self {
-            AllowedUrlError::HrefParseError(e) => write!(f, "href parse error: {}", e),
-            AllowedUrlError::BaseRequired => write!(f, "base required"),
-            AllowedUrlError::DifferentURISchemes => write!(f, "different URI schemes"),
-            AllowedUrlError::DisallowedScheme => write!(f, "disallowed scheme"),
-            AllowedUrlError::NotSiblingOrChildOfBaseFile => {
-                write!(f, "not sibling or child of base file")
-            }
-            AllowedUrlError::InvalidPath => write!(f, "invalid path"),
-            AllowedUrlError::BaseIsRoot => write!(f, "base is root"),
-            AllowedUrlError::CanonicalizationError => write!(f, "canonicalization error"),
-        }
-    }
-}
-
 // For tests, we don't want to touch the filesystem.  In that case,
 // assume that we are being passed canonical file names.
 #[cfg(not(test))]
@@ -254,12 +209,12 @@ mod tests {
     #[test]
     fn disallows_relative_file_with_no_base_file() {
         let url_resolver = UrlResolver::new(None);
-        assert_eq!(
+        assert!(matches!(
             url_resolver.resolve_href("foo.svg"),
             Err(AllowedUrlError::HrefParseError(
                 url::ParseError::RelativeUrlWithoutBase
             ))
-        );
+        ));
     }
 
     #[test]
@@ -267,28 +222,28 @@ mod tests {
         let url_resolver = UrlResolver::new(Some(
             Url::parse("http://example.com/malicious.svg";).unwrap(),
         ));
-        assert_eq!(
+        assert!(matches!(
             url_resolver.resolve_href("file:///etc/passwd"),
             Err(AllowedUrlError::DifferentURISchemes)
-        );
+        ));
     }
 
     #[test]
     fn disallows_base_is_root() {
         let url_resolver = UrlResolver::new(Some(Url::parse("file:///").unwrap()));
-        assert_eq!(
+        assert!(matches!(
             url_resolver.resolve_href("foo.svg"),
             Err(AllowedUrlError::BaseIsRoot)
-        );
+        ));
     }
 
     #[test]
     fn disallows_non_file_scheme() {
         let url_resolver = UrlResolver::new(Some(Url::parse("http://foo.bar/baz.svg";).unwrap()));
-        assert_eq!(
+        assert!(matches!(
             url_resolver.resolve_href("foo.svg"),
             Err(AllowedUrlError::DisallowedScheme)
-        );
+        ));
     }
 
     #[test]
@@ -339,10 +294,10 @@ mod tests {
     #[test]
     fn disallows_non_sibling() {
         let url_resolver = UrlResolver::new(Some(Url::parse("file:///example/bar.svg").unwrap()));
-        assert_eq!(
+        assert!(matches!(
             url_resolver.resolve_href("file:///etc/passwd"),
             Err(AllowedUrlError::NotSiblingOrChildOfBaseFile)
-        );
+        ));
     }
 
     #[test]


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