Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[photos] Prepare optional Rust location Wasm binding

AswinAsok7ea0f601fd5a
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 3e9d4e7195..214a14cf11 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -3032,6 +3032,7 @@ dependencies = [
"ente-contacts",
"ente-core",
"ente-legacy",
+ "ente-location",
"ente-locker",
"js-sys",
"log",
diff --git a/rust/bindings/wasm/lib/Cargo.toml b/rust/bindings/wasm/lib/Cargo.toml
index 0aa0139cc6..12896f45f6 100644
--- a/rust/bindings/wasm/lib/Cargo.toml
+++ b/rust/bindings/wasm/lib/Cargo.toml
@@ -16,6 +16,7 @@ crypto-blob = ["crypto"]
crypto-file = ["crypto-blob", "dep:md-5"]
crypto-seal = ["crypto"]
legacy = ["session", "dep:ente-legacy", "dep:wasm-bindgen-futures"]
+location = ["dep:ente-location", "dep:serde", "dep:serde-wasm-bindgen", "dep:tsify"]
locker = ["session", "dep:ente-locker"]
prelogin = ["accounts", "crypto"]
session = [
@@ -32,6 +33,7 @@ ente-collections = { workspace = true, optional = true }
ente-contacts = { workspace = true, optional = true }
ente-core.workspace = true
ente-legacy = { workspace = true, optional = true }
+ente-location = { workspace = true, optional = true }
ente-locker = { workspace = true, optional = true }
js-sys.workspace = true
log.workspace = true
diff --git a/rust/bindings/wasm/lib/src/lib.rs b/rust/bindings/wasm/lib/src/lib.rs
index a4dee15ea5..fef175c64f 100644
--- a/rust/bindings/wasm/lib/src/lib.rs
+++ b/rust/bindings/wasm/lib/src/lib.rs
@@ -10,6 +10,8 @@ pub mod contacts;
pub mod crypto;
#[cfg(feature = "legacy")]
pub mod legacy;
+#[cfg(feature = "location")]
+pub mod location;
#[cfg(feature = "locker")]
pub mod locker;
mod logging;
diff --git a/rust/bindings/wasm/lib/src/location.rs b/rust/bindings/wasm/lib/src/location.rs
new file mode 100644
index 0000000000..2028f41ec0
--- /dev/null
+++ b/rust/bindings/wasm/lib/src/location.rs
@@ -0,0 +1,95 @@
+use ente_location::{CityIndex, Coordinate, UrbanCenterIndex};
+use serde::{Deserialize, Serialize};
+use serde_wasm_bindgen as swb;
+use tsify::Tsify;
+use wasm_bindgen::prelude::*;
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error(transparent)]
+ Location(#[from] ente_location::Error),
+ #[error(transparent)]
+ Serde(#[from] swb::Error),
+}
+
+impl From<Error> for JsValue {
+ fn from(error: Error) -> Self {
+ crate::js_error(&error, None)
+ }
+}
+
+#[derive(Deserialize, Tsify)]
+pub struct LocationCoordinate {
+ latitude: f64,
+ longitude: f64,
+}
+
+#[derive(Deserialize, Tsify)]
+pub struct LocationQuery {
+ coordinates: Vec<LocationCoordinate>,
+}
+
+#[derive(Serialize, Tsify)]
+pub struct LocationCity {
+ name: String,
+ country: String,
+ latitude: f64,
+ longitude: f64,
+}
+
+#[derive(Serialize, Tsify)]
+#[serde(rename_all = "camelCase")]
+pub struct LocationCityGroup {
+ city: LocationCity,
+ coordinate_indices: Vec<u32>,
+}
+
+#[derive(Serialize, Tsify)]
+pub struct LocationCityGroups {
+ groups: Vec<LocationCityGroup>,
+}
+
+#[wasm_bindgen]
+pub struct LocationIndex {
+ cities: CityIndex,
+ urban_centers: UrbanCenterIndex,
+}
+
+#[wasm_bindgen]
+impl LocationIndex {
+ #[wasm_bindgen(constructor)]
+ pub fn new(cities: &[u8], urban_centers: &[u8]) -> Result<LocationIndex, Error> {
+ Ok(Self {
+ cities: CityIndex::from_bytes(cities)?,
+ urban_centers: UrbanCenterIndex::from_bytes(urban_centers)?,
+ })
+ }
+
+ #[wasm_bindgen(js_name = groupCities)]
+ pub fn group_cities(
+ &self,
+ input: <LocationQuery as Tsify>::JsType,
+ ) -> Result<<LocationCityGroups as Tsify>::JsType, Error> {
+ let input = LocationQuery::from_js(input)?;
+ let coordinates: Vec<_> = input
+ .coordinates
+ .into_iter()
+ .map(|coordinate| Coordinate::new(coordinate.latitude, coordinate.longitude))
+ .collect();
+ let groups = self
+ .urban_centers
+ .match_coordinates_with_cities(&self.cities, &coordinates, "")
+ .into_iter()
+ .map(|group| LocationCityGroup {
+ city: LocationCity {
+ name: group.city.name,
+ country: group.city.country_name,
+ latitude: group.city.latitude,
+ longitude: group.city.longitude,
+ },
+ coordinate_indices: group.coordinate_indices,
+ })
+ .collect();
+ LocationCityGroups { groups }.into_js().map_err(Into::into)
+ }
+}