[librsvg: 6/10] CHandle::get_dimensions() - return a Result; don't panic if the handle is not loaded



commit fc575d2d7e034e9b9a0a234f65c356a495669069
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Jul 23 15:17:48 2019 -0500

    CHandle::get_dimensions() - return a Result; don't panic if the handle is not loaded
    
    The idea is to do this:
    
    1. Indicate programming errors around incorrect API
    usage (e.g. calling rsvg_handle_get_dimensions() before
    rsvg_handle_close()) by emitting a g_critical().
    
    2. Return an "empty" dimensions value instead of panicking, but do
    this as close to the public API as possible, instead of in the
    implementation functions.

 rsvg_internals/src/c_api.rs | 23 ++++++-----------------
 1 file changed, 6 insertions(+), 17 deletions(-)
---
diff --git a/rsvg_internals/src/c_api.rs b/rsvg_internals/src/c_api.rs
index f69b8e3e..5b176a3a 100644
--- a/rsvg_internals/src/c_api.rs
+++ b/rsvg_internals/src/c_api.rs
@@ -664,13 +664,10 @@ impl CHandle {
         handle.has_sub(id)
     }
 
-    fn get_dimensions(&self) -> RsvgDimensionData {
-        if let Ok(handle) = self.get_handle_ref() {
-            let size_callback = self.size_callback.borrow();
-            handle.get_dimensions_no_error(self.dpi.get(), &*size_callback, self.is_testing.get())
-        } else {
-            panic!("Handle is not loaded");
-        }
+    fn get_dimensions(&self) -> Result<RsvgDimensionData, RenderingError> {
+        let handle = self.get_handle_ref()?;
+        let size_callback = self.size_callback.borrow();
+        handle.get_dimensions(self.dpi.get(), &*size_callback, self.is_testing.get())
     }
 
     fn get_dimensions_sub(&self, id: Option<&str>) -> Result<RsvgDimensionData, RenderingError> {
@@ -985,7 +982,6 @@ pub unsafe extern "C" fn rsvg_rust_handle_has_sub(
     }
 
     let id: String = from_glib_none(id);
-    // FIXME: return a proper error code to the public API
     rhandle.has_sub(&id).unwrap_or(false).to_glib()
 }
 
@@ -1029,7 +1025,7 @@ pub unsafe extern "C" fn rsvg_rust_handle_get_dimensions(
     dimension_data: *mut RsvgDimensionData,
 ) {
     let rhandle = get_rust_handle(handle);
-    *dimension_data = rhandle.get_dimensions();
+    *dimension_data = rhandle.get_dimensions().unwrap_or_else(|_| RsvgDimensionData::empty());
 }
 
 #[no_mangle]
@@ -1049,14 +1045,7 @@ pub unsafe extern "C" fn rsvg_rust_handle_get_dimensions_sub(
         }
 
         Err(_) => {
-            let d = &mut *dimension_data;
-
-            d.width = 0;
-            d.height = 0;
-            d.em = 0.0;
-            d.ex = 0.0;
-
-            // FIXME: return a proper error code to the public API
+            *dimension_data = RsvgDimensionData::empty();
             false.to_glib()
         }
     }


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