[librsvg: 22/32] Use an internal IoError for the io module, instead of LoadingError
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 22/32] Use an internal IoError for the io module, instead of LoadingError
- Date: Fri, 4 Dec 2020 21:11:31 +0000 (UTC)
commit 59de2c6e81adcc839151df53574ed2e32d5ee9f7
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Nov 26 17:39:03 2020 -0600
Use an internal IoError for the io module, instead of LoadingError
This lets us remove LoadingError::BadDataUrl, which was just used
while decoding data: URLs.
src/css.rs | 1 +
src/document.rs | 1 +
src/error.rs | 14 ++++++++++----
src/io.rs | 38 ++++++++++++++++++++++++++++----------
src/xml/mod.rs | 6 ++----
5 files changed, 42 insertions(+), 18 deletions(-)
---
diff --git a/src/css.rs b/src/css.rs
index 9c901354..2e508035 100644
--- a/src/css.rs
+++ b/src/css.rs
@@ -692,6 +692,7 @@ impl Stylesheet {
.map_err(|_| LoadingError::BadUrl)?;
io::acquire_data(&aurl, None)
+ .map_err(LoadingError::from)
.and_then(|data| {
let BinaryData {
data: bytes,
diff --git a/src/document.rs b/src/document.rs
index 2abe1e5c..1e84a695 100644
--- a/src/document.rs
+++ b/src/document.rs
@@ -156,6 +156,7 @@ impl Resources {
let aurl = e.key();
// FIXME: pass a cancellable to these
let doc = io::acquire_stream(aurl, None)
+ .map_err(LoadingError::from)
.and_then(|stream| {
Document::load_from_stream(
&load_options.copy_with_base_url(aurl),
diff --git a/src/error.rs b/src/error.rs
index d541086b..5d4ce770 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -6,6 +6,7 @@ use std::fmt;
use cssparser::{BasicParseError, BasicParseErrorKind, ParseErrorKind, ToCss};
use markup5ever::QualName;
+use crate::io::IoError;
use crate::node::Node;
use crate::url_resolver::Fragment;
@@ -340,9 +341,6 @@ pub enum LoadingError {
/// A malformed or disallowed URL was used.
BadUrl,
- /// A `data:` URL could not be decoded.
- BadDataUrl,
-
/// An invalid stylesheet was used.
BadCss,
@@ -367,7 +365,6 @@ impl fmt::Display for LoadingError {
LoadingError::XmlParseError(ref s) => write!(f, "XML parse error: {}", s),
LoadingError::OutOfMemory(ref s) => write!(f, "out of memory: {}", s),
LoadingError::BadUrl => write!(f, "invalid URL"),
- LoadingError::BadDataUrl => write!(f, "invalid data: URL"),
LoadingError::BadCss => write!(f, "invalid CSS"),
LoadingError::NoSvgRoot => write!(f, "XML does not have <svg> root"),
LoadingError::Glib(ref e) => e.fmt(f),
@@ -382,3 +379,12 @@ impl From<glib::Error> for LoadingError {
LoadingError::Glib(e)
}
}
+
+impl From<IoError> for LoadingError {
+ fn from(e: IoError) -> LoadingError {
+ match e {
+ IoError::BadDataUrl => LoadingError::BadUrl,
+ IoError::Glib(e) => LoadingError::Glib(e),
+ }
+ }
+}
diff --git a/src/io.rs b/src/io.rs
index 2753fc7b..fff43cbf 100644
--- a/src/io.rs
+++ b/src/io.rs
@@ -1,24 +1,42 @@
//! Utilities to acquire streams and data from from URLs.
use gio::{Cancellable, File as GFile, FileExt, InputStream, MemoryInputStream};
-use glib::{Bytes as GBytes, Cast};
+use glib::{self, Bytes as GBytes, Cast};
+use std::fmt;
-use crate::error::LoadingError;
use crate::url_resolver::AllowedUrl;
+pub enum IoError {
+ BadDataUrl,
+ Glib(glib::Error),
+}
+
+impl From<glib::Error> for IoError {
+ fn from(e: glib::Error) -> IoError {
+ IoError::Glib(e)
+ }
+}
+
+impl fmt::Display for IoError {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match *self {
+ IoError::BadDataUrl => write!(f, "invalid data: URL"),
+ IoError::Glib(ref e) => e.fmt(f),
+ }
+ }
+}
+
pub struct BinaryData {
pub data: Vec<u8>,
pub content_type: Option<String>,
}
-fn decode_data_uri(uri: &str) -> Result<BinaryData, LoadingError> {
- let data_url = data_url::DataUrl::process(uri).map_err(|_| LoadingError::BadDataUrl)?;
+fn decode_data_uri(uri: &str) -> Result<BinaryData, IoError> {
+ let data_url = data_url::DataUrl::process(uri).map_err(|_| IoError::BadDataUrl)?;
let mime_type = data_url.mime_type().to_string();
- let (bytes, fragment_id) = data_url
- .decode_to_vec()
- .map_err(|_| LoadingError::BadDataUrl)?;
+ let (bytes, fragment_id) = data_url.decode_to_vec().map_err(|_| IoError::BadDataUrl)?;
// See issue #377 - per the data: URL spec
// (https://fetch.spec.whatwg.org/#data-urls), those URLs cannot
@@ -26,7 +44,7 @@ fn decode_data_uri(uri: &str) -> Result<BinaryData, LoadingError> {
// one. This probably indicates mis-quoted SVG data inside the
// data: URL.
if fragment_id.is_some() {
- return Err(LoadingError::BadDataUrl);
+ return Err(IoError::BadDataUrl);
}
Ok(BinaryData {
@@ -39,7 +57,7 @@ fn decode_data_uri(uri: &str) -> Result<BinaryData, LoadingError> {
pub fn acquire_stream(
aurl: &AllowedUrl,
cancellable: Option<&Cancellable>,
-) -> Result<InputStream, LoadingError> {
+) -> Result<InputStream, IoError> {
let uri = aurl.as_str();
if uri.starts_with("data:") {
@@ -67,7 +85,7 @@ pub fn acquire_stream(
pub fn acquire_data(
aurl: &AllowedUrl,
cancellable: Option<&Cancellable>,
-) -> Result<BinaryData, LoadingError> {
+) -> Result<BinaryData, IoError> {
let uri = aurl.as_str();
if uri.starts_with("data:") {
diff --git a/src/xml/mod.rs b/src/xml/mod.rs
index b819db7f..89f4e899 100644
--- a/src/xml/mod.rs
+++ b/src/xml/mod.rs
@@ -22,7 +22,7 @@ use xml5ever::tokenizer::{TagKind, Token, TokenSink, XmlTokenizer, XmlTokenizerO
use crate::attributes::Attributes;
use crate::document::{Document, DocumentBuilder};
use crate::error::LoadingError;
-use crate::io;
+use crate::io::{self, IoError};
use crate::limits::MAX_LOADED_ELEMENTS;
use crate::node::{Node, NodeBorrow};
use crate::style::StyleType;
@@ -555,9 +555,7 @@ impl XmlState {
// FIXME: distinguish between "file not found" and "invalid XML"
let stream = io::acquire_stream(aurl, None).map_err(|e| match e {
- LoadingError::BadDataUrl => {
- AcquireError::FatalError(String::from("malformed data: URL"))
- }
+ IoError::BadDataUrl => AcquireError::FatalError(String::from("malformed data: URL")),
_ => AcquireError::ResourceError,
})?;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]