[librsvg: 21/32] Move get_input_stream_for_loading to the xml module




commit 12598f5d2ee675d9c1855a2ef7bbb7dcc28e6e50
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Nov 26 16:49:54 2020 -0600

    Move get_input_stream_for_loading to the xml module
    
    This is just used for svgz files, after all.

 src/io.rs      | 35 +----------------------------------
 src/xml/mod.rs | 37 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 37 insertions(+), 35 deletions(-)
---
diff --git a/src/io.rs b/src/io.rs
index 06d5b37b..2753fc7b 100644
--- a/src/io.rs
+++ b/src/io.rs
@@ -1,9 +1,6 @@
 //! Utilities to acquire streams and data from from URLs.
 
-use gio::{
-    BufferedInputStream, BufferedInputStreamExt, Cancellable, ConverterInputStream, File as GFile,
-    FileExt, InputStream, MemoryInputStream, ZlibCompressorFormat, ZlibDecompressor,
-};
+use gio::{Cancellable, File as GFile, FileExt, InputStream, MemoryInputStream};
 use glib::{Bytes as GBytes, Cast};
 
 use crate::error::LoadingError;
@@ -38,36 +35,6 @@ fn decode_data_uri(uri: &str) -> Result<BinaryData, LoadingError> {
     })
 }
 
-// Header of a gzip data stream
-const GZ_MAGIC_0: u8 = 0x1f;
-const GZ_MAGIC_1: u8 = 0x8b;
-
-pub fn get_input_stream_for_loading(
-    stream: &InputStream,
-    cancellable: Option<&Cancellable>,
-) -> Result<InputStream, LoadingError> {
-    // detect gzipped streams (svgz)
-
-    let buffered = BufferedInputStream::new(stream);
-    let num_read = buffered.fill(2, cancellable)?;
-    if num_read < 2 {
-        // FIXME: this string was localized in the original; localize it
-        return Err(LoadingError::XmlParseError(String::from(
-            "Input file is too short",
-        )));
-    }
-
-    let buf = buffered.peek_buffer();
-    assert!(buf.len() >= 2);
-    if buf[0..2] == [GZ_MAGIC_0, GZ_MAGIC_1] {
-        let decomp = ZlibDecompressor::new(ZlibCompressorFormat::Gzip);
-        let converter = ConverterInputStream::new(&buffered, &decomp);
-        Ok(converter.upcast::<InputStream>())
-    } else {
-        Ok(buffered.upcast::<InputStream>())
-    }
-}
-
 /// Returns an input stream.  The url can be a data: URL or a plain URI
 pub fn acquire_stream(
     aurl: &AllowedUrl,
diff --git a/src/xml/mod.rs b/src/xml/mod.rs
index 23275b98..b819db7f 100644
--- a/src/xml/mod.rs
+++ b/src/xml/mod.rs
@@ -2,6 +2,11 @@
 
 use encoding::label::encoding_from_whatwg_label;
 use encoding::DecoderTrap;
+use gio::{
+    BufferedInputStream, BufferedInputStreamExt, Cancellable, ConverterInputStream, InputStream,
+    ZlibCompressorFormat, ZlibDecompressor,
+};
+use glib::Cast;
 use markup5ever::{
     buffer_queue::BufferQueue, expanded_name, local_name, namespace_url, ns, ExpandedName,
     LocalName, Namespace, QualName,
@@ -17,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::{self, get_input_stream_for_loading};
+use crate::io;
 use crate::limits::MAX_LOADED_ELEMENTS;
 use crate::node::{Node, NodeBorrow};
 use crate::style::StyleType;
@@ -696,6 +701,36 @@ pub fn xml_load_from_possibly_compressed_stream(
     state.build_document(&stream, cancellable)
 }
 
+// Header of a gzip data stream
+const GZ_MAGIC_0: u8 = 0x1f;
+const GZ_MAGIC_1: u8 = 0x8b;
+
+fn get_input_stream_for_loading(
+    stream: &InputStream,
+    cancellable: Option<&Cancellable>,
+) -> Result<InputStream, LoadingError> {
+    // detect gzipped streams (svgz)
+
+    let buffered = BufferedInputStream::new(stream);
+    let num_read = buffered.fill(2, cancellable)?;
+    if num_read < 2 {
+        // FIXME: this string was localized in the original; localize it
+        return Err(LoadingError::XmlParseError(String::from(
+            "Input file is too short",
+        )));
+    }
+
+    let buf = buffered.peek_buffer();
+    assert!(buf.len() >= 2);
+    if buf[0..2] == [GZ_MAGIC_0, GZ_MAGIC_1] {
+        let decomp = ZlibDecompressor::new(ZlibCompressorFormat::Gzip);
+        let converter = ConverterInputStream::new(&buffered, &decomp);
+        Ok(converter.upcast::<InputStream>())
+    } else {
+        Ok(buffered.upcast::<InputStream>())
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;


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