Skip to content

Commit

Permalink
Update blanket write_wkt impl
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Nov 26, 2024
1 parent 9b8ca1a commit a719912
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
34 changes: 0 additions & 34 deletions src/geo_types_to_wkt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use geo_types::CoordNum;

use crate::to_wkt::{write_multi_polygon, write_point};
use crate::types::{
Coord, GeometryCollection, LineString, MultiLineString, MultiPoint, MultiPolygon, Point,
Polygon,
Expand Down Expand Up @@ -36,17 +35,6 @@ where
}
}

/// A wrapper around something that implements std::io::Write to be used with our writer traits,
/// which require std::fmt::Write
struct WriterWrapper<W: std::io::Write>(W);

impl<W: std::io::Write> std::fmt::Write for WriterWrapper<W> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
self.0.write(s.as_bytes()).map_err(|_| std::fmt::Error)?;
Ok(())
}
}

/// # Examples
/// ```
/// use geo_types::{point, Point};
Expand All @@ -63,17 +51,6 @@ where
fn to_wkt(&self) -> Wkt<T> {
Wkt::Point(g_point_to_w_point(self))
}

fn wkt_string(&self) -> String {
let mut s = String::new();
write_point(&mut s, self).unwrap();
s
}

fn write_wkt(&self, writer: impl std::io::Write) -> std::io::Result<()> {
write_point(&mut WriterWrapper(writer), self).unwrap();
Ok(())
}
}

/// # Examples
Expand Down Expand Up @@ -188,17 +165,6 @@ where
fn to_wkt(&self) -> Wkt<T> {
g_mpolygon_to_w_mpolygon(self).into()
}

fn wkt_string(&self) -> String {
let mut s = String::new();
write_multi_polygon(&mut s, self).unwrap();
s
}

fn write_wkt(&self, writer: impl std::io::Write) -> std::io::Result<()> {
write_multi_polygon(&mut WriterWrapper(writer), self).unwrap();
Ok(())
}
}

/// # Examples
Expand Down
21 changes: 19 additions & 2 deletions src/to_wkt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ pub use geo_trait_impl::{
write_rect, write_triangle,
};

/// A wrapper around something that implements std::io::Write to be used with our writer traits,
/// which require std::fmt::Write
struct WriterWrapper<W: std::io::Write>(W);

impl<W: std::io::Write> std::fmt::Write for WriterWrapper<W> {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
// Sadly, this will lose the content of the error when mapping to std::fmt::Error
self.0.write(s.as_bytes()).map_err(|_| std::fmt::Error)?;
Ok(())
}
}

/// A trait for converting values to WKT
pub trait ToWkt<T>
where
Expand Down Expand Up @@ -49,7 +61,12 @@ where
///
/// assert_eq!(wkt_string, "POINT(1.2 3.4)");
/// ```
fn write_wkt(&self, mut writer: impl std::io::Write) -> std::io::Result<()> {
writer.write_all(self.wkt_string().as_bytes())
///
/// ## Panics
///
/// - If
fn write_wkt(&self, writer: impl std::io::Write) -> std::io::Result<()> {
write_geometry(&mut WriterWrapper(writer), &self.to_wkt())
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err.to_string()))
}
}

0 comments on commit a719912

Please sign in to comment.