stac_io/
lib.rs

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