stac_io/
lib.rs

1pub mod api;
2mod error;
3mod format;
4#[cfg(feature = "geoparquet")]
5mod geoparquet;
6mod json;
7mod ndjson;
8mod read;
9mod realized_href;
10#[cfg(feature = "store")]
11pub mod store;
12mod write;
13
14#[cfg(feature = "geoparquet")]
15pub use geoparquet::{FromGeoparquetPath, IntoGeoparquetPath};
16#[cfg(feature = "store")]
17pub use store::{StacStore, parse_href, parse_href_opts};
18pub use {
19    error::Error,
20    format::Format,
21    json::{FromJsonPath, ToJsonPath},
22    ndjson::{FromNdjsonPath, ToNdjsonPath},
23    read::read,
24    realized_href::RealizedHref,
25    write::write,
26};
27
28/// Crate-specific result type.
29pub type Result<T> = std::result::Result<T, Error>;
30
31/// Composite trait for all formats readable by stac-io.
32#[cfg(feature = "geoparquet")]
33pub trait Readable: FromJsonPath + FromNdjsonPath + FromGeoparquetPath {}
34#[cfg(not(feature = "geoparquet"))]
35pub trait Readable: FromJsonPath + FromNdjsonPath {}
36
37#[cfg(feature = "geoparquet")]
38impl<T> Readable for T where T: FromJsonPath + FromNdjsonPath + FromGeoparquetPath {}
39#[cfg(not(feature = "geoparquet"))]
40impl<T> Readable for T where T: FromJsonPath + FromNdjsonPath {}
41
42/// Composite trait for all formats writeable by stac-io.
43#[cfg(feature = "geoparquet")]
44pub trait Writeable: ToJsonPath + ToNdjsonPath + IntoGeoparquetPath {}
45#[cfg(not(feature = "geoparquet"))]
46pub trait Writeable: ToJsonPath + ToNdjsonPath {}
47
48#[cfg(feature = "geoparquet")]
49impl<T> Writeable for T where T: ToJsonPath + ToNdjsonPath + IntoGeoparquetPath {}
50#[cfg(not(feature = "geoparquet"))]
51impl<T> Writeable for T where T: ToJsonPath + ToNdjsonPath {}
52
53/// Returns a string suitable for use as a HTTP user agent.
54pub fn user_agent() -> &'static str {
55    concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"))
56}
57
58#[cfg(test)]
59mod tests {
60    use stac::{Catalog, Collection, Item, ItemCollection};
61    use tempfile::TempDir;
62
63    macro_rules! read {
64        ($function:ident, $filename:expr_2021, $value:ty $(, $meta:meta)?) => {
65            #[test]
66            $(#[$meta])?
67            fn $function() {
68                use stac::SelfHref;
69
70                let value: $value = crate::read($filename).unwrap();
71                assert!(value.self_href().is_some());
72            }
73        };
74    }
75
76    read!(read_item_from_path, "examples/simple-item.json", Item);
77    read!(read_catalog_from_path, "examples/catalog.json", Catalog);
78    read!(
79        read_collection_from_path,
80        "examples/collection.json",
81        Collection
82    );
83    read!(
84        read_item_collection_from_path,
85        "data/item-collection.json",
86        ItemCollection
87    );
88
89    mod read_with_reqwest {
90        use stac::{Catalog, Collection, Item};
91
92        read!(
93            read_item_from_url,
94            "https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/simple-item.json",
95            Item
96        );
97        read!(
98            read_catalog_from_url,
99            "https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/catalog.json",
100            Catalog
101        );
102        read!(
103            read_collection_from_url,
104            "https://raw.githubusercontent.com/radiantearth/stac-spec/master/examples/collection.json",
105            Collection
106        );
107    }
108
109    #[test]
110    #[cfg(feature = "geoparquet")]
111    fn read_geoparquet() {
112        let _: ItemCollection = super::read("data/extended-item.parquet").unwrap();
113    }
114
115    #[test]
116    #[cfg(not(feature = "geoparquet"))]
117    fn read_geoparquet_without_geoparquet() {
118        let _ = super::read::<ItemCollection>("data/extended-item.parquet").unwrap_err();
119    }
120
121    #[test]
122    fn write() {
123        let tempdir = TempDir::new().unwrap();
124        let item = Item::new("an-id");
125        super::write(tempdir.path().join("item.json"), item).unwrap();
126        let item: Item = super::read(tempdir.path().join("item.json").to_string_lossy()).unwrap();
127        assert_eq!(item.id, "an-id");
128    }
129}