diff --git a/Cargo.lock b/Cargo.lock index 5b3c533..5c8948f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -904,7 +904,7 @@ dependencies = [ [[package]] name = "pgstac" version = "0.2.2" -source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +source = "git+https://github.com/stac-utils/stac-rs#87ce7cd4cfea052c841e2a2b329098b797228202" dependencies = [ "serde", "serde_json", @@ -925,12 +925,10 @@ dependencies = [ "pyo3", "pyo3-async-runtimes", "pythonize", - "serde", "serde_json", "stac", "stac-api", "thiserror 2.0.4", - "tokio", "tokio-postgres", ] @@ -1326,7 +1324,7 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "stac" version = "0.11.0" -source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +source = "git+https://github.com/stac-utils/stac-rs#87ce7cd4cfea052c841e2a2b329098b797228202" dependencies = [ "bytes", "chrono", @@ -1344,11 +1342,13 @@ dependencies = [ [[package]] name = "stac-api" version = "0.6.2" -source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +source = "git+https://github.com/stac-utils/stac-rs#87ce7cd4cfea052c841e2a2b329098b797228202" dependencies = [ "chrono", "cql2", "geojson", + "pyo3", + "pythonize", "serde", "serde_json", "serde_urlencoded", @@ -1363,7 +1363,7 @@ dependencies = [ [[package]] name = "stac-derive" version = "0.1.0" -source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +source = "git+https://github.com/stac-utils/stac-rs#87ce7cd4cfea052c841e2a2b329098b797228202" dependencies = [ "quote", "syn", @@ -1372,7 +1372,7 @@ dependencies = [ [[package]] name = "stac-types" version = "0.1.0" -source = "git+https://github.com/stac-utils/stac-rs#ab577151ebe5c45bf0059d90b6c370bd54818ad7" +source = "git+https://github.com/stac-utils/stac-rs#87ce7cd4cfea052c841e2a2b329098b797228202" dependencies = [ "mime", "serde", diff --git a/Cargo.toml b/Cargo.toml index b0cf23f..9f214d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,10 @@ pyo3-async-runtimes = { version = "0.23.0", features = [ "tokio-runtime", ] } pythonize = "0.23.0" -serde = "1.0.215" serde_json = "1.0.133" -stac-api = { version = "0.6.2", git = "https://github.com/stac-utils/stac-rs" } +stac-api = { version = "0.6.2", features = [ + "python", +], git = "https://github.com/stac-utils/stac-rs" } stac = { version = "0.11.0", git = "https://github.com/stac-utils/stac-rs" } thiserror = "2.0.4" -tokio = "1.41.1" tokio-postgres = { version = "0.7.12", features = ["with-serde_json-1"] } diff --git a/src/lib.rs b/src/lib.rs index 710d2aa..fd5ac29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ +#![deny(unused_crate_dependencies)] + use bb8::{Pool, RunError}; use bb8_postgres::PostgresConnectionManager; -use geojson::Geometry; use pgstac::Pgstac; use pyo3::{ create_exception, @@ -9,8 +10,7 @@ use pyo3::{ types::{PyDict, PyList, PyType}, }; use serde_json::Value; -use stac::Bbox; -use stac_api::{Fields, Filter, Items, Search, Sortby}; +use stac_api::python::{StringOrDict, StringOrList}; use std::{future::Future, str::FromStr}; use thiserror::Error; use tokio_postgres::{Config, NoTls}; @@ -20,18 +20,6 @@ create_exception!(pgstacrs, StacError, PyException); type PgstacPool = Pool>; -#[derive(FromPyObject)] -pub enum StringOrDict { - String(String), - Dict(Py), -} - -#[derive(FromPyObject)] -pub enum StringOrList { - String(String), - List(Vec), -} - #[derive(Debug, Error)] enum Error { #[error(transparent)] @@ -290,71 +278,19 @@ impl Client { query: Option>, limit: Option, ) -> PyResult> { - // TODO refactor to use https://github.com/gadomski/stacrs/blob/1528d7e1b7185a86efe9fc7c42b0620093c5e9c6/src/search.rs#L128-L162 - let mut fields = Fields::default(); - if let Some(include) = include { - fields.include = include.into(); - } - if let Some(exclude) = exclude { - fields.exclude = exclude.into(); - } - let fields = if fields.include.is_empty() && fields.exclude.is_empty() { - None - } else { - Some(fields) - }; - let query = query - .map(|query| pythonize::depythonize(&query)) - .transpose()?; - let bbox = bbox - .map(|bbox| Bbox::try_from(bbox)) - .transpose() - .map_err(Error::from)?; - let sortby = sortby.map(|sortby| { - Vec::::from(sortby) - .into_iter() - .map(|s| s.parse::().unwrap()) // the parse is infallible - .collect::>() - }); - let filter = filter - .map(|filter| match filter { - StringOrDict::Dict(cql_json) => { - pythonize::depythonize(&cql_json.bind_borrowed(py)).map(Filter::Cql2Json) - } - StringOrDict::String(cql2_text) => Ok(Filter::Cql2Text(cql2_text)), - }) - .transpose()?; - let filter = filter - .map(|filter| filter.into_cql2_json()) - .transpose() - .map_err(Error::from)?; - let items = Items { + let search = stac_api::python::search( + intersects, + ids, + collections, limit, bbox, datetime, - query, - fields, + include, + exclude, sortby, filter, - ..Default::default() - }; - - let intersects = intersects - .map(|intersects| match intersects { - StringOrDict::Dict(json) => pythonize::depythonize(&json.bind_borrowed(py)) - .map_err(Error::from) - .and_then(|json| Geometry::from_json_object(json).map_err(Error::from)), - StringOrDict::String(s) => s.parse().map_err(Error::from), - }) - .transpose()?; - let ids = ids.map(|ids| ids.into()); - let collections = collections.map(|ids| ids.into()); - let search = Search { - items, - intersects, - ids, - collections, - }; + query, + )?; self.run(py, |pool| async move { let connection = pool.get().await?; let page = connection.search(search).await?; @@ -406,15 +342,6 @@ impl From for PyErr { } } -impl From for Vec { - fn from(value: StringOrList) -> Vec { - match value { - StringOrList::List(list) => list, - StringOrList::String(s) => vec![s], - } - } -} - #[pymodule] fn pgstacrs(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?;