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

Easier serialization of wkt with ToWkt::wkt_string/write_wkt #89

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changes

## next release
### Changed

### Added
* impl `std::fmt::Display` for `Wkt`.
* <https://github.com/georust/wkt/pull/88>
* added `wkt_string` and `write_wkt` methods to `ToWkt` trait
* <https://github.com/georust/wkt/pull/89>

## 0.10.0 - 2022-02-24
### Changed
Expand Down
41 changes: 38 additions & 3 deletions src/towkt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,45 @@ use geo_types::CoordFloat;
/// A trait for converting values to WKT
pub trait ToWkt<T>
where
T: CoordFloat,
T: CoordFloat + std::fmt::Display,
{
/// Converts the value of `self` to an instance of WKT
/// Converts the value of `self` to an [`Wkt`] struct.
///
/// Typically you won't need to call this, but by implementing it for your own type, your type
/// gains the other methods in this trait.
fn to_wkt(&self) -> Wkt<T>;

/// Serialize as a WKT string
///
/// ```
/// use wkt::ToWkt;
/// let point: geo_types::Geometry<f64> = geo_types::point!(x: 1.0, y: 2.0).into();
/// assert_eq!("POINT(1 2)", &point.wkt_string());
/// ```
fn wkt_string(&self) -> String {
self.to_wkt().to_string()
}

/// Write a WKT string to a [`File`](std::fs::File), or anything else that implements [`Write`](std::io::Write).
///
/// ```
/// use wkt::ToWkt;
/// use std::fs::File;
/// let point: geo_types::Geometry<f64> = geo_types::point!(x: 1.2, y: 3.4).into();
///
/// // use a vec as a fake "file" for the purpose of example, but you could equally replace the
/// // following with:
/// // let mut file = File::create("myfile.wkt").unwrap();
/// let mut file = vec![] ;
///
/// point.write_wkt(&mut file).unwrap();
/// let wkt_string = String::from_utf8(file).unwrap();
///
/// 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())
}
}

fn g_point_to_w_coord<T>(g_point: &geo_types::Coordinate<T>) -> Coord<T>
Expand Down Expand Up @@ -215,7 +250,7 @@ where

impl<T> ToWkt<T> for geo_types::Geometry<T>
where
T: CoordFloat,
T: CoordFloat + std::fmt::Display,
{
fn to_wkt(&self) -> Wkt<T> {
let w_geom = g_geom_to_w_geom(self);
Expand Down