1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Validate STAC objects with jsonschema.
//!
//! # Examples
//!
//! Validation is provided via the [Validate] trait:
//!
//! ```
//! use stac::Item;
//! use stac_validate::Validate;
//! Item::new("an-id").validate().unwrap();
//! ```
//!
//! [stac::Collection], [stac::Catalog], and [stac::Item] all have their schemas built into the library, so they don't need to be fetched from the network.
//! Any extension schemas are fetched using [reqwest](https://docs.rs/reqwest/latest/reqwest/), and cached for later use.
//! This means that, if you're doing multiple validations, you should re-use the same [Validator]:
//!
//! ```
//! # use stac::Item;
//! use stac_validate::Validator;
//!
//! let mut items: Vec<_> = (0..10).map(|n| Item::new(format!("item-{}", n))).collect();
//! let mut validator = Validator::new();
//! for item in items {
//!     validator.validate(item).unwrap();
//! }
//! ```

#![deny(
    elided_lifetimes_in_paths,
    explicit_outlives_requirements,
    keyword_idents,
    macro_use_extern_crate,
    meta_variable_misuse,
    missing_abi,
    missing_debug_implementations,
    missing_docs,
    non_ascii_idents,
    noop_method_call,
    pointer_structural_match,
    rust_2021_incompatible_closure_captures,
    rust_2021_incompatible_or_patterns,
    rust_2021_prefixes_incompatible_syntax,
    rust_2021_prelude_collisions,
    single_use_lifetimes,
    trivial_casts,
    trivial_numeric_casts,
    unreachable_pub,
    unsafe_code,
    unsafe_op_in_unsafe_fn,
    unused_crate_dependencies,
    unused_extern_crates,
    unused_import_braces,
    unused_lifetimes,
    unused_qualifications,
    unused_results
)]

mod error;
mod validate;
mod validator;

pub use {
    error::Error,
    validate::{Validate, ValidateCore},
    validator::Validator,
};

/// Crate-specific result type.
pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use crate::Validate;
    use geojson::{Geometry, Value};
    use stac::{Catalog, Collection, Item};

    #[test]
    fn item() {
        let item = Item::new("an-id");
        item.validate().unwrap();
    }

    #[test]
    fn item_with_geometry() {
        let mut item = Item::new("an-id");
        item.set_geometry(Geometry::new(Value::Point(vec![-105.1, 40.1])))
            .unwrap();
        item.validate().unwrap();
    }

    #[test]
    fn item_with_extensions() {
        let item: Item =
            stac::read("data/extensions-collection/proj-example/proj-example.json").unwrap();
        item.validate().unwrap();
    }

    #[test]
    fn catalog() {
        let catalog = Catalog::new("an-id", "a description");
        catalog.validate().unwrap();
    }

    #[test]
    fn collection() {
        let collection = Collection::new("an-id", "a description");
        collection.validate().unwrap();
    }

    #[test]
    fn value() {
        let value: stac::Value = stac::read("data/simple-item.json").unwrap();
        value.validate().unwrap();
    }

    #[test]
    fn item_collection() {
        let item = stac::read("data/simple-item.json").unwrap();
        let item_collection = stac::ItemCollection::from(vec![item]);
        item_collection.validate().unwrap();
    }
}

// From https://github.com/rust-lang/cargo/issues/383#issuecomment-720873790,
// may they be forever blessed.
#[cfg(doctest)]
mod readme {
    macro_rules! external_doc_test {
        ($x:expr) => {
            #[doc = $x]
            extern "C" {}
        };
    }

    external_doc_test!(include_str!("../README.md"));
}