[librsvg/wip/rust-api: 2/11] Provide a minimal loading API in librsvg_crate



commit a0feaec630c727ba968496627a1c99e5c3a02b94
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed Feb 13 14:27:26 2019 -0600

    Provide a minimal loading API in librsvg_crate

 Cargo.lock                   |  1 +
 librsvg_crate/Cargo.toml     |  1 +
 librsvg_crate/src/lib.rs     | 50 ++++++++++++++++++++++++++++++++++++++++++--
 rsvg_internals/src/handle.rs |  8 ++++++-
 rsvg_internals/src/lib.rs    |  5 +++++
 5 files changed, 62 insertions(+), 3 deletions(-)
---
diff --git a/Cargo.lock b/Cargo.lock
index 641f61d9..4f5f0d7b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -590,6 +590,7 @@ version = "0.0.1"
 dependencies = [
  "cairo-rs 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "gio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "glib 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "rsvg_internals 0.0.1",
  "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
diff --git a/librsvg_crate/Cargo.toml b/librsvg_crate/Cargo.toml
index 0fda9988..041f270b 100644
--- a/librsvg_crate/Cargo.toml
+++ b/librsvg_crate/Cargo.toml
@@ -9,6 +9,7 @@ name = "librsvg"
 
 [dependencies]
 cairo-rs = "0.5.0"
+glib = "0.6.0"
 gio = { version="0.5.1", features=["v2_48"] } # per configure.ac
 rsvg_internals = { path = "../rsvg_internals" }
 url = "1.7.2"
diff --git a/librsvg_crate/src/lib.rs b/librsvg_crate/src/lib.rs
index abb21df7..35ab5800 100644
--- a/librsvg_crate/src/lib.rs
+++ b/librsvg_crate/src/lib.rs
@@ -1,11 +1,40 @@
 #![warn(unused)]
+extern crate gio;
+extern crate glib;
 extern crate rsvg_internals;
 extern crate url;
 
+use std::path::Path;
+
+use gio::FileExt;
+use glib::object::Cast;
+
+use rsvg_internals::{Handle, LoadFlags};
 use url::Url;
 
+pub use rsvg_internals::{LoadingError, RenderingError};
+
 /// Full configuration for loading an [`SvgHandle`][SvgHandle]
 ///
+/// This struct implements a builder pattern for configuring an
+/// [`SvgHandle`][SvgHandle]'s options, and then loading the SVG data.
+/// You can call the methods of `LoadOptions` in sequence to configure
+/// how SVG data should be loaded, and finally use one of the loading
+/// functions to load an [`SvgHandle`][SvgHandle].
+///
+/// # Example:
+///
+/// ```ignore
+/// extern crate librsvg;
+///
+/// use librsvg::LoadOptions;
+///
+/// let svg_handle = LoadOptions::new()
+///     .unlimited_size()
+///     .read_path("example.svg")
+///     .unwrap();
+/// ```
+///
 /// [SvgHandle]: struct.SvgHandle.html
 pub struct LoadOptions {
     unlimited_size: bool,
@@ -36,7 +65,24 @@ impl LoadOptions {
         self.keep_image_data = keep;
         self
     }
-}
 
-pub struct SvgHandle {
+    fn load_flags(&self) -> LoadFlags {
+        LoadFlags {
+            unlimited_size: self.unlimited_size,
+            keep_image_data: self.keep_image_data,
+        }
+    }
+
+    pub fn read_path<P: AsRef<Path>>(self, path: P) -> Result<SvgHandle, LoadingError> {
+        let file = gio::File::new_for_path(path);
+
+        let stream = file.read(None)?;
+
+        let mut handle = Handle::new_with_flags(self.load_flags());
+        handle.construct_read_stream_sync(&stream.upcast(), Some(&file), None)?;
+
+        Ok(SvgHandle(handle))
+    }
 }
+
+pub struct SvgHandle(Handle);
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index b179cf56..e020b5ba 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -172,6 +172,12 @@ impl Handle {
         }
     }
 
+    pub fn new_with_flags(load_flags: LoadFlags) -> Handle {
+        let handle = Handle::new();
+        handle.load_flags.set(load_flags);
+        handle
+    }
+
     fn set_base_url(&self, url: &str) {
         if self.load_state.get() != LoadState::Start {
             rsvg_g_warning(
@@ -523,7 +529,7 @@ impl Handle {
         self.construct_read_stream_sync(&stream.upcast(), Some(file), cancellable)
     }
 
-    fn construct_read_stream_sync(
+    pub fn construct_read_stream_sync(
         &mut self,
         stream: &gio::InputStream,
         base_file: Option<&gio::File>,
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 2d5d05be..ffc1bf33 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -44,6 +44,8 @@ pub use color::{rsvg_css_parse_color, ColorKind, ColorSpec};
 
 pub use dpi::rsvg_rust_set_default_dpi_x_y;
 
+pub use error::{LoadingError, RenderingError};
+
 pub use handle::{
     rsvg_handle_rust_close,
     rsvg_handle_rust_free,
@@ -74,6 +76,9 @@ pub use handle::{
     rsvg_handle_rust_set_size_callback,
     rsvg_handle_rust_set_testing,
     rsvg_handle_rust_write,
+
+    Handle,
+    LoadFlags,
 };
 
 pub use pixbuf_utils::{


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