Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept– combining *begin and *end processor methods #49

Open
frewsxcv opened this issue Sep 7, 2022 · 0 comments
Open

Proof of concept– combining *begin and *end processor methods #49

frewsxcv opened this issue Sep 7, 2022 · 0 comments

Comments

@frewsxcv
Copy link
Member

frewsxcv commented Sep 7, 2022

Just sharing in case it's something we want to consider

diff --git a/geozero/src/feature_processor.rs b/geozero/src/feature_processor.rs
index 2b8554a..6b4c8d2 100644
--- a/geozero/src/feature_processor.rs
+++ b/geozero/src/feature_processor.rs
@@ -4,7 +4,7 @@ use crate::property_processor::PropertyProcessor;
 
 /// Feature processing trait
 #[allow(unused_variables)]
-pub trait FeatureProcessor: GeomProcessor + PropertyProcessor {
+pub trait FeatureProcessor: GeomProcessor + PropertyProcessor + Sized {
     /// Begin of dataset processing
     fn dataset_begin(&mut self, name: Option<&str>) -> Result<()> {
         Ok(())
@@ -22,10 +22,16 @@ pub trait FeatureProcessor: GeomProcessor + PropertyProcessor {
         Ok(())
     }
     /// Begin of feature property processing
+    fn write_properties(&mut self, inner: impl FnOnce(&mut Self) -> Result<()>) -> Result<()> {
+        Ok(())
+    }
+    /// Begin of feature property processing
+    #[deprecated]
     fn properties_begin(&mut self) -> Result<()> {
         Ok(())
     }
     /// End of feature property processing
+    #[deprecated]
     fn properties_end(&mut self) -> Result<()> {
         Ok(())
     }
diff --git a/geozero/src/geojson/geojson_reader.rs b/geozero/src/geojson/geojson_reader.rs
index 9d107a4..29fc714 100644
--- a/geozero/src/geojson/geojson_reader.rs
+++ b/geozero/src/geojson/geojson_reader.rs
@@ -121,9 +121,9 @@ fn process_geojson_feature<P: FeatureProcessor>(
     if feature.geometry.is_some() || feature.properties.is_some() {
         processor.feature_begin(idx as u64)?;
         if let Some(ref properties) = feature.properties {
-            processor.properties_begin()?;
-            process_properties(properties, processor)?;
-            processor.properties_end()?;
+            processor.write_properties(|processor| {
+                process_properties(properties, processor)
+            })?;
         }
         if let Some(ref geometry) = feature.geometry {
             processor.geometry_begin()?;
diff --git a/geozero/src/geojson/geojson_writer.rs b/geozero/src/geojson/geojson_writer.rs
index ac7f371..97214b4 100644
--- a/geozero/src/geojson/geojson_writer.rs
+++ b/geozero/src/geojson/geojson_writer.rs
@@ -54,6 +54,12 @@ impl<W: Write> FeatureProcessor for GeoJsonWriter<'_, W> {
         self.out.write_all(b"}")?;
         Ok(())
     }
+    fn write_properties(&mut self, inner: impl FnOnce(&mut Self) -> Result<()>) -> Result<()> {
+        self.out.write_all(br#", "properties": {"#)?;
+        inner(self)?;
+        self.out.write_all(b"}")?;
+        Ok(())
+    }
     fn properties_begin(&mut self) -> Result<()> {
         self.out.write_all(br#", "properties": {"#)?;
         Ok(())
@frewsxcv frewsxcv changed the title Proof of concept– combining *start and *end processor methods Proof of concept– combining *begin and *end processor methods Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant