Skip to main content

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