@@ -209,6 +209,90 @@ pub const STAC_VERSION: Version = Version::v1_0_0;
209209/// Custom [Result](std::result::Result) type for this crate.
210210pub type Result < T > = std:: result:: Result < T , Error > ;
211211
212+ /// Enum for the four "types" of STAC values.
213+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
214+ pub enum Type {
215+ /// An item.
216+ Item ,
217+
218+ /// A collection.
219+ Collection ,
220+
221+ /// A catalog.
222+ Catalog ,
223+
224+ /// An item collection.
225+ ///
226+ /// While not technically part of the STAC specification, it's used all over the place.
227+ ItemCollection ,
228+ }
229+
230+ impl Type {
231+ /// Returns this type as a str.
232+ ///
233+ /// # Examples
234+ ///
235+ /// ```
236+ /// use stac::Type;
237+ ///
238+ /// assert_eq!(Type::Item.as_str(), "Feature");
239+ /// ```
240+ pub fn as_str ( & self ) -> & ' static str {
241+ match self {
242+ Type :: Item => "Feature" ,
243+ Type :: Catalog => "Catalog" ,
244+ Type :: Collection => "Collection" ,
245+ Type :: ItemCollection => "FeatureCollection" ,
246+ }
247+ }
248+
249+ /// Returns the schema path for this type.
250+ ///
251+ /// # Examples
252+ ///
253+ /// ```
254+ /// use stac::{Type, Version};
255+ ///
256+ /// assert_eq!(Type::Item.spec_path(&Version::v1_0_0).unwrap(), "/v1.0.0/item-spec/json-schema/item.json");
257+ /// ```
258+ pub fn spec_path ( & self , version : & Version ) -> Option < String > {
259+ match self {
260+ Type :: Item => Some ( format ! ( "/v{}/item-spec/json-schema/item.json" , version) ) ,
261+ Type :: Catalog => Some ( format ! (
262+ "/v{}/catalog-spec/json-schema/catalog.json" ,
263+ version
264+ ) ) ,
265+ Type :: Collection => Some ( format ! (
266+ "/v{}/collection-spec/json-schema/collection.json" ,
267+ version
268+ ) ) ,
269+ Type :: ItemCollection => None ,
270+ }
271+ }
272+ }
273+
274+ impl std:: str:: FromStr for Type {
275+ type Err = Error ;
276+ fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
277+ match s {
278+ "Feature" => Ok ( Type :: Item ) ,
279+ "Catalog" => Ok ( Type :: Catalog ) ,
280+ "Collection" => Ok ( Type :: Collection ) ,
281+ "FeatureCollection" => Ok ( Type :: ItemCollection ) ,
282+ _ => Err ( Error :: UnknownType ( s. to_string ( ) ) ) ,
283+ }
284+ }
285+ }
286+
287+ impl < T > PartialEq < T > for Type
288+ where
289+ T : AsRef < str > ,
290+ {
291+ fn eq ( & self , other : & T ) -> bool {
292+ self . as_str ( ) == other. as_ref ( )
293+ }
294+ }
295+
212296/// Utility function to deserialize the type field on an object.
213297///
214298/// Use this, via a wrapper function, for `#[serde(deserialize_with)]`.
0 commit comments