From aad626d3fc32902022dbdbabd88ca080c702975e Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Tue, 28 Mar 2023 23:45:23 -0400 Subject: [PATCH] Do not duplicate structs in xpin-wasm --- Cargo.lock | 31 ++++++++++++- spatial-coordinate-systems/Cargo.toml | 1 + spatial-coordinate-systems/src/latlon.rs | 2 +- spatial-coordinate-systems/src/lib.rs | 22 ++++----- spatial-coordinate-systems/src/urls.rs | 2 +- xpin-wasm/src/lib.rs | 58 +----------------------- 6 files changed, 46 insertions(+), 70 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 645fc2a..cd80b03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -504,6 +504,12 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -1373,6 +1379,7 @@ dependencies = [ "nom", "pluscodes", "serde", + "strum", "thiserror", "url", "utm", @@ -1426,13 +1433,35 @@ version = "0.4.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0" dependencies = [ - "heck", + "heck 0.3.3", "proc-macro-error", "proc-macro2", "quote", "syn 1.0.109", ] +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" +dependencies = [ + "strum_macros", +] + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.1", + "proc-macro2", + "quote", + "rustversion", + "syn 1.0.109", +] + [[package]] name = "syn" version = "1.0.109" diff --git a/spatial-coordinate-systems/Cargo.toml b/spatial-coordinate-systems/Cargo.toml index c4dce3e..bc9df76 100644 --- a/spatial-coordinate-systems/Cargo.toml +++ b/spatial-coordinate-systems/Cargo.toml @@ -30,3 +30,4 @@ url = "2.3.1" wasm-bindgen={version="0.2",optional=true} serde={version="1",optional=true,features=["derive"]} +strum={version="0.24",features=["derive"]} diff --git a/spatial-coordinate-systems/src/latlon.rs b/spatial-coordinate-systems/src/latlon.rs index 363140f..e5049f9 100644 --- a/spatial-coordinate-systems/src/latlon.rs +++ b/spatial-coordinate-systems/src/latlon.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use std::fmt; #[cfg(feature = "wasm_bindgen")] -use wasm_bindgen::prelude::*; +use wasm_bindgen::prelude::wasm_bindgen; use nom::{ character::complete::space0, diff --git a/spatial-coordinate-systems/src/lib.rs b/spatial-coordinate-systems/src/lib.rs index e53dbbc..267199e 100644 --- a/spatial-coordinate-systems/src/lib.rs +++ b/spatial-coordinate-systems/src/lib.rs @@ -9,8 +9,11 @@ pub mod plus; pub mod urls; pub mod utm; pub use error::Error; +use strum::EnumDiscriminants; // pub mod xpin; mod error; +#[cfg(feature = "wasm_bindgen")] +use wasm_bindgen::prelude::wasm_bindgen; use nom::{ branch::alt, @@ -21,6 +24,7 @@ use nom::{ }; pub use latlon::LatLon; +use strum::EnumString; #[derive(Debug, Clone, Copy, PartialEq)] pub enum Direction { @@ -52,7 +56,13 @@ impl Direction { } } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, EnumDiscriminants)] +#[cfg_attr( + feature = "wasm_bindgen", + strum_discriminants(wasm_bindgen(getter_with_clone)) +)] +#[strum_discriminants(derive(EnumString))] +#[strum_discriminants(name(CoordinateType))] pub enum Coordinate { DD(dd::Coordinate), DMS(dms::Coordinate), @@ -62,16 +72,6 @@ pub enum Coordinate { Plus(plus::Coordinate), } -#[derive(Debug, Copy, Clone)] -pub enum CoordinateType { - DD, - DMS, - DMM, - UTM, - // Xpin, - Plus, -} - impl Coordinate { pub fn parse(i: &str) -> IResult<&str, Self> { map( diff --git a/spatial-coordinate-systems/src/urls.rs b/spatial-coordinate-systems/src/urls.rs index cef66a6..df132c2 100644 --- a/spatial-coordinate-systems/src/urls.rs +++ b/spatial-coordinate-systems/src/urls.rs @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize}; use std::str::FromStr; use url::Url; #[cfg(feature = "wasm_bindgen")] -use wasm_bindgen::prelude::*; +use wasm_bindgen::prelude::wasm_bindgen; // TODO: Set a reasonable OSM zoom level const OPENSTREETMAP_ZOOM_LEVEL: u8 = 19; diff --git a/xpin-wasm/src/lib.rs b/xpin-wasm/src/lib.rs index 4305bcd..aeffd3a 100644 --- a/xpin-wasm/src/lib.rs +++ b/xpin-wasm/src/lib.rs @@ -1,46 +1,9 @@ use std::str::FromStr; -use spatial_coordinate_systems::{urls::CoordinateUrls, Coordinate, LatLon}; +use spatial_coordinate_systems::{urls::CoordinateUrls, Coordinate, CoordinateType, LatLon}; use wasm_bindgen::prelude::*; use xpin::Address; -#[wasm_bindgen] -#[derive(Debug, Copy, Clone)] -pub enum CoordinateType { - DD, - DMS, - DMM, - UTM, - // Xpin, - Plus, -} - -impl From for spatial_coordinate_systems::CoordinateType { - fn from(val: CoordinateType) -> Self { - match val { - CoordinateType::DD => Self::DD, - CoordinateType::DMS => Self::DMS, - CoordinateType::DMM => Self::DMM, - CoordinateType::UTM => Self::UTM, - // CoordinateType::Xpin => Self::Xpin, - CoordinateType::Plus => Self::Plus, - } - } -} - -impl From for CoordinateType { - fn from(value: spatial_coordinate_systems::CoordinateType) -> CoordinateType { - match value { - spatial_coordinate_systems::CoordinateType::DD => Self::DD, - spatial_coordinate_systems::CoordinateType::DMS => Self::DMS, - spatial_coordinate_systems::CoordinateType::DMM => Self::DMM, - spatial_coordinate_systems::CoordinateType::UTM => Self::UTM, - // spatial_coordinate_systems::CoordinateType::Xpin => Self::Xpin, - spatial_coordinate_systems::CoordinateType::Plus => Self::Plus, - } - } -} - #[wasm_bindgen(getter_with_clone)] #[derive(Debug)] pub struct EncodedAddress { @@ -186,26 +149,9 @@ impl TryFrom<&'_ Address<'_>> for EncodedAddress { } } -// #[wasm_bindgen(getter_with_clone)] -// #[derive(Debug, Clone)] -// pub struct CoordinateUrls { -// // TODO: These should be getters only -// pub google_maps: String, -// pub openstreetmap: String, -// } - -// impl From for CoordinateUrls { -// fn from(value: spatial_coordinate_systems::urls::CoordinateUrls) -> Self { -// Self { -// google_maps: value.google_maps, -// openstreetmap: value.openstreetmap, -// } -// } -// } - #[cfg(test)] mod tests { - use super::*; + // use super::*; #[test] fn test_general() {