Skip to content

Commit

Permalink
refactor!: remove stac-types
Browse files Browse the repository at this point in the history
  • Loading branch information
gadomski committed Dec 6, 2024
1 parent 87ce7cd commit cf954f5
Show file tree
Hide file tree
Showing 27 changed files with 73 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- name: Test
run: cargo test -p stac -p stac-types --all-features
run: cargo test -p stac --all-features
check-features-core:
name: Check stac features
runs-on: ubuntu-latest
Expand Down
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ members = [
"crates/extensions",
"crates/pgstac",
"crates/server",
"crates/types",
]
default-members = [
"crates/api",
"crates/cli",
"crates/core",
"crates/derive",
"crates/extensions",
"crates/types",
]

[workspace.package]
Expand Down Expand Up @@ -77,7 +75,6 @@ stac-api = { version = "0.6.2", path = "crates/api" }
stac-derive = { version = "0.1.0", path = "crates/derive" }
stac-duckdb = { version = "0.0.3", path = "crates/duckdb" }
stac-server = { version = "0.3.2", path = "crates/server" }
stac-types = { version = "0.1.0", path = "crates/types" }
syn = "2.0"
tempfile = "3.13"
thiserror = "2.0"
Expand Down
2 changes: 0 additions & 2 deletions crates/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ client = [
"dep:http",
"dep:reqwest",
"dep:tokio",
"stac-types/reqwest",
]
geo = ["dep:geo", "stac/geo"]
python = ["dep:pyo3", "dep:pythonize"]
Expand All @@ -37,7 +36,6 @@ serde_json.workspace = true
serde_urlencoded.workspace = true
stac.workspace = true
stac-derive.workspace = true
stac-types.workspace = true
pyo3 = { workspace = true, optional = true }
pythonize = { workspace = true, optional = true }
thiserror.workspace = true
Expand Down
1 change: 0 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ stac = { workspace = true, features = [
stac-api = { workspace = true, features = ["client"] }
stac-duckdb = { workspace = true, optional = true }
stac-server = { workspace = true, features = ["axum"] }
stac-types.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = [
"macros",
Expand Down
4 changes: 0 additions & 4 deletions crates/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ pub enum Error {
#[error(transparent)]
StacServer(#[from] stac_server::Error),

/// [stac_types::Error]
#[error(transparent)]
StacTypes(#[from] stac_types::Error),

/// [tokio::sync::mpsc::error::SendError]
#[error(transparent)]
TokioSend(#[from] tokio::sync::mpsc::error::SendError<Value>),
Expand Down
6 changes: 3 additions & 3 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object-store-all = [
"object-store-gcp",
"object-store-http",
]
reqwest = ["dep:reqwest", "stac-types/reqwest"]
reqwest = ["dep:reqwest"]
reqwest-rustls = ["reqwest/rustls-tls"]
validate = ["dep:jsonschema", "dep:reqwest", "dep:tokio", "dep:fluent-uri"]
validate-blocking = ["validate", "tokio/rt"]
Expand All @@ -61,17 +61,17 @@ geoarrow = { workspace = true, optional = true }
geojson.workspace = true
jsonschema = { workspace = true, optional = true }
log.workspace = true
mime.workspace = true
object_store = { workspace = true, optional = true }
parquet = { workspace = true, optional = true }
reqwest = { workspace = true, features = ["json", "blocking"], optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
stac-derive.workspace = true
stac-types.workspace = true
thiserror.workspace = true
tokio = { workspace = true, optional = true }
tracing.workspace = true
url.workspace = true
url = { workspace = true, features = ["serde"] }

[dev-dependencies]
assert-json-diff.workspace = true
Expand Down
27 changes: 23 additions & 4 deletions crates/core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::Version;
use thiserror::Error;

/// Error enum for crate-specific errors.
Expand Down Expand Up @@ -46,6 +47,16 @@ pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),

/// Returned when a STAC object has the wrong type field.
#[error("incorrect type: expected={expected}, actual={actual}")]
IncorrectType {
/// The actual type field on the object.
actual: String,

/// The expected value.
expected: String,
},

/// Returned when a property name conflicts with a top-level STAC field, or
/// it's an invalid top-level field name.
#[error("invalid attribute name: {0}")]
Expand All @@ -67,6 +78,14 @@ pub enum Error {
#[error("no items")]
NoItems,

/// There is not an href, when an href is required.
#[error("no href")]
NoHref,

/// This is not a JSON object.
#[error("json value is not an object")]
NotAnObject(serde_json::Value),

/// [object_store::Error]
#[error(transparent)]
#[cfg(feature = "object-store")]
Expand Down Expand Up @@ -95,10 +114,6 @@ pub enum Error {
#[error(transparent)]
SerdeJson(#[from] serde_json::Error),

/// [stac_types::Error]
#[error(transparent)]
StacTypes(#[from] stac_types::Error),

/// [tokio::task::JoinError]
#[error(transparent)]
#[cfg(any(feature = "validate", feature = "object-store"))]
Expand All @@ -120,6 +135,10 @@ pub enum Error {
#[error("unsupported geoparquet type")]
UnsupportedGeoparquetType,

/// Unsupported migration.
#[error("unsupported migration: {0} to {1}")]
UnsupportedMigration(Version, Version),

/// [url::ParseError]
#[error(transparent)]
UrlParse(#[from] url::ParseError),
Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions crates/core/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
geoparquet::{Compression, FromGeoparquet, IntoGeoparquet},
Error, FromJson, FromNdjson, Href, Result, SelfHref, ToJson, ToNdjson,
Error, FromJson, FromNdjson, Href, RealizedHref, Result, SelfHref, ToJson, ToNdjson,
};
use bytes::Bytes;
use stac_types::RealizedHref;
use std::{fmt::Display, path::Path, str::FromStr};

/// The format of STAC data.
Expand Down
5 changes: 3 additions & 2 deletions crates/types/src/href.rs → crates/core/src/href.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Href {
String(String),
}

/// An href that has been realized to a path or a url.
#[derive(Debug)]
pub enum RealizedHref {
/// A path buf
Expand Down Expand Up @@ -225,8 +226,8 @@ impl TryFrom<Href> for Url {
}

#[cfg(feature = "reqwest")]
impl From<reqwest::Url> for Href {
fn from(value: reqwest::Url) -> Self {
impl From<Url> for Href {
fn from(value: Url) -> Self {
Href::Url(value)
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/core/src/item_collection.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Error, Href, Item, Link, Migrate, Version};
use crate::{Error, Href, Item, Link, Migrate, Result, Version};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use stac_derive::{Links, SelfHref};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Deref for ItemCollection {
}

impl Migrate for ItemCollection {
fn migrate(mut self, version: &Version) -> stac_types::Result<Self> {
fn migrate(mut self, version: &Version) -> Result<Self> {
let mut items = Vec::with_capacity(self.items.len());
for item in self.items {
items.push(item.migrate(version)?);
Expand All @@ -74,7 +74,7 @@ impl Migrate for ItemCollection {
impl TryFrom<Value> for ItemCollection {
type Error = Error;

fn try_from(value: Value) -> Result<Self, Self::Error> {
fn try_from(value: Value) -> Result<Self> {
match serde_json::from_value::<ItemCollection>(value.clone()) {
Ok(item_collection) => Ok(item_collection),
Err(err) => {
Expand Down
19 changes: 18 additions & 1 deletion crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
unused_results
)]

// Enables derive macros here and elsewhere.
// https://users.rust-lang.org/t/use-of-imported-types-in-derive-macro/94676/3
extern crate self as stac;

mod asset;
mod band;
mod bbox;
Expand All @@ -155,17 +159,22 @@ mod collection;
mod data_type;
pub mod datetime;
mod error;
mod fields;
mod format;
#[cfg(feature = "geo")]
pub mod geo;
#[cfg(feature = "geoarrow")]
pub mod geoarrow;
pub mod geoparquet;
mod href;
pub mod io;
pub mod item;
mod item_asset;
mod item_collection;
mod json;
pub mod link;
mod migrate;
pub mod mime;
mod ndjson;
mod node;
#[cfg(feature = "object-store")]
Expand All @@ -174,12 +183,12 @@ mod statistics;
#[cfg(feature = "validate")]
mod validate;
mod value;
mod version;

use std::fmt::Display;

#[cfg(feature = "object-store")]
pub use resolver::Resolver;
pub use stac_types::{mime, Fields, Href, Link, Links, Migrate, SelfHref, Version, STAC_VERSION};
#[cfg(feature = "validate-blocking")]
pub use validate::ValidateBlocking;
#[cfg(feature = "validate")]
Expand All @@ -192,19 +201,27 @@ pub use {
collection::{Collection, Extent, Provider, SpatialExtent, TemporalExtent},
data_type::DataType,
error::Error,
fields::Fields,
format::Format,
geoparquet::{FromGeoparquet, IntoGeoparquet},
href::{Href, RealizedHref, SelfHref},
io::{read, write},
item::{FlatItem, Item, Properties},
item_asset::ItemAsset,
item_collection::ItemCollection,
json::{FromJson, ToJson},
link::{Link, Links},
migrate::Migrate,
ndjson::{FromNdjson, ToNdjson},
node::{Container, Node},
statistics::Statistics,
value::Value,
version::Version,
};

/// The default STAC version of this library.
pub const STAC_VERSION: Version = Version::v1_1_0;

/// Custom [Result](std::result::Result) type for this crate.
pub type Result<T> = std::result::Result<T, Error>;

Expand Down
2 changes: 1 addition & 1 deletion crates/types/src/link.rs → crates/core/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ mod tests {
}

mod links {
use stac::{Catalog, Item, Link, Links};
use crate::{Catalog, Item, Link, Links};

#[test]
fn link() {
Expand Down
10 changes: 5 additions & 5 deletions crates/types/src/migrate.rs → crates/core/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ fn migrate_license(object: &mut Map<String, Value>) {

#[cfg(test)]
mod tests {
use crate::{Collection, DataType, Item, Link, Links, Migrate, Version};
use assert_json_diff::assert_json_eq;
use serde_json::Value;
use stac::{Collection, DataType, Item, Link, Links, Migrate, Version};

#[test]
fn migrate_v1_0_0_to_v1_1_0() {
let item: Item = stac::read("data/bands-v1.0.0.json").unwrap();
let item: Item = crate::read("data/bands-v1.0.0.json").unwrap();
let item = item.migrate(&Version::v1_1_0).unwrap();
let asset = &item.assets["example"];
assert_eq!(asset.data_type.as_ref().unwrap(), &DataType::UInt16);
Expand All @@ -241,7 +241,7 @@ mod tests {
assert_eq!(asset.bands[3].name.as_ref().unwrap(), "nir");

let expected: Value =
serde_json::to_value(stac::read::<Item>("data/bands-v1.1.0.json").unwrap()).unwrap();
serde_json::to_value(crate::read::<Item>("data/bands-v1.1.0.json").unwrap()).unwrap();
assert_json_eq!(expected, serde_json::to_value(item).unwrap());

let mut collection = Collection::new("an-id", "a description");
Expand All @@ -259,15 +259,15 @@ mod tests {
#[test]
fn remove_empty_bands() {
// https://github.com/stac-utils/stac-rs/issues/350
let item: Item = stac::read("data/20201211_223832_CS2.json").unwrap();
let item: Item = crate::read("data/20201211_223832_CS2.json").unwrap();
let item = item.migrate(&Version::v1_1_0).unwrap();
let asset = &item.assets["data"];
assert!(asset.bands.is_empty());
}

#[test]
fn migrate_v1_1_0_to_v1_1_0() {
let item: Item = stac::read("../../spec-examples/v1.1.0/simple-item.json").unwrap();
let item: Item = crate::read("../../spec-examples/v1.1.0/simple-item.json").unwrap();
let _ = item.migrate(&Version::v1_1_0).unwrap();
}
}
File renamed without changes.
3 changes: 1 addition & 2 deletions crates/core/src/ndjson.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{Error, FromJson, Item, ItemCollection, Result, Value};
use crate::{Error, FromJson, Item, ItemCollection, Result, SelfHref, Value};
use bytes::Bytes;
use serde::Serialize;
use stac_types::SelfHref;
use std::{
fs::File,
io::{BufRead, BufReader, BufWriter, Write},
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl TryFrom<Value> for Container {
match value {
Value::Catalog(c) => Ok(c.into()),
Value::Collection(c) => Ok(c.into()),
_ => Err(stac_types::Error::IncorrectType {
_ => Err(Error::IncorrectType {
actual: value.type_name().to_string(),
expected: "Catalog or Collection".to_string(),
}
Expand Down
10 changes: 5 additions & 5 deletions crates/core/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
Catalog, Collection, Error, Href, Item, ItemCollection, Link, Links, Migrate, Result, Version,
Catalog, Collection, Error, Href, Item, ItemCollection, Link, Links, Migrate, Result, SelfHref,
Version,
};
use serde::{Deserialize, Serialize};
use serde_json::Map;
use stac_types::SelfHref;
use std::convert::TryFrom;

/// An enum that can hold any STAC object type.
Expand Down Expand Up @@ -260,7 +260,7 @@ macro_rules! impl_try_from {
if let Value::$object(o) = value {
Ok(o)
} else {
Err(stac_types::Error::IncorrectType {
Err(Error::IncorrectType {
actual: value.type_name().to_string(),
expected: $name.to_string(),
}
Expand All @@ -284,7 +284,7 @@ impl TryFrom<Value> for ItemCollection {
match value {
Value::Item(item) => Ok(ItemCollection::from(vec![item])),
Value::ItemCollection(item_collection) => Ok(item_collection),
Value::Catalog(_) | Value::Collection(_) => Err(stac_types::Error::IncorrectType {
Value::Catalog(_) | Value::Collection(_) => Err(Error::IncorrectType {
actual: value.type_name().to_string(),
expected: "ItemCollection".to_string(),
}
Expand All @@ -294,7 +294,7 @@ impl TryFrom<Value> for ItemCollection {
}

impl Migrate for Value {
fn migrate(self, version: &Version) -> stac_types::Result<Value> {
fn migrate(self, version: &Version) -> Result<Value> {
match self {
Value::Item(item) => item.migrate(version).map(Value::Item),
Value::Catalog(catalog) => catalog.migrate(version).map(Value::Catalog),
Expand Down
File renamed without changes.
Loading

0 comments on commit cf954f5

Please sign in to comment.