2222//! ## Usage
2323//!
2424//! [Item](crate::Item), [Collection](crate::Collection),
25- //! [Catalog](crate::Catalog), and [Asset](crate::Asset) all implement the
26- //! [Extensions] trait, which provides methods to get, set, and remove extension information:
25+ //! [Catalog](crate::Catalog) all implement the [Extensions] trait, which
26+ //! provides methods to get, set, and remove extension information:
2727//!
2828//! ```
29- //! use stac::{Item, Extensions, extensions::{Projection, projection::Centroid}};
29+ //! use stac::{Item, Extensions, Fields, extensions::{Projection, projection::Centroid}};
3030//! let mut item: Item = stac::read("examples/extensions-collection/proj-example/proj-example.json").unwrap();
3131//! assert!(item.has_extension::<Projection>());
3232//!
3333//! // Get extension information
34- //! let mut projection: Projection = item.extension().unwrap().unwrap() ;
34+ //! let mut projection: Projection = item.extension().unwrap();
3535//! println!("code: {}", projection.code.as_ref().unwrap());
3636//!
3737//! // Set extension information
3838//! projection.centroid = Some(Centroid { lat: 34.595302, lon: -101.344483 });
39- //! item. set_extension(projection).unwrap();
39+ //! Extensions:: set_extension(&mut item, projection).unwrap();
4040//!
4141//! // Remove an extension
42- //! item. remove_extension::<Projection>();
42+ //! Extensions:: remove_extension::<Projection>(&mut item );
4343//! assert!(!item.has_extension::<Projection>());
4444//! ```
4545
@@ -122,26 +122,6 @@ pub trait Extensions: Fields {
122122 . any ( |extension| extension. starts_with ( E :: identifier_prefix ( ) ) )
123123 }
124124
125- /// Gets an extension's data.
126- ///
127- /// Returns `Ok(None)` if the object doesn't have the given extension.
128- ///
129- /// # Examples
130- ///
131- /// ```
132- /// use stac::{Item, extensions::{Projection, Extensions}};
133- /// let item: Item = stac::read("examples/extensions-collection/proj-example/proj-example.json").unwrap();
134- /// let projection: Projection = item.extension().unwrap().unwrap();
135- /// assert_eq!(projection.code.unwrap(), "EPSG:32614");
136- /// ```
137- fn extension < E : Extension > ( & self ) -> Result < Option < E > > {
138- if self . has_extension :: < E > ( ) {
139- self . fields_with_prefix ( E :: PREFIX ) . map ( |v| Some ( v) )
140- } else {
141- Ok ( None )
142- }
143- }
144-
145125 /// Adds an extension's identifier to this object.
146126 ///
147127 /// # Examples
@@ -169,10 +149,9 @@ pub trait Extensions: Fields {
169149 /// item.set_extension(projection).unwrap();
170150 /// ```
171151 fn set_extension < E : Extension > ( & mut self , extension : E ) -> Result < ( ) > {
172- self . remove_extension :: < E > ( ) ;
173152 self . extensions_mut ( ) . push ( E :: IDENTIFIER . to_string ( ) ) ;
174153 self . extensions_mut ( ) . dedup ( ) ;
175- self . set_fields_with_prefix ( E :: PREFIX , extension)
154+ Fields :: set_extension ( self , extension)
176155 }
177156
178157 /// Removes this extension and all of its fields from this object.
@@ -187,8 +166,7 @@ pub trait Extensions: Fields {
187166 /// assert!(!item.has_extension::<Projection>());
188167 /// ```
189168 fn remove_extension < E : Extension > ( & mut self ) {
190- // TODO how do we handle removing from assets when this is done on an item?
191- self . remove_fields_with_prefix ( E :: PREFIX ) ;
169+ Fields :: remove_extension :: < E > ( self ) ;
192170 self . extensions_mut ( )
193171 . retain ( |extension| !extension. starts_with ( E :: identifier_prefix ( ) ) )
194172 }
@@ -220,13 +198,13 @@ mod tests {
220198
221199 #[ test]
222200 fn set_extension_on_asset ( ) {
201+ use crate :: Fields ;
202+
223203 let mut asset = Asset :: new ( "a/href.tif" ) ;
224- assert ! ( !asset. has_extension:: <Raster >( ) ) ;
225204 let mut band = Band :: default ( ) ;
226205 band. unit = Some ( "parsecs" . to_string ( ) ) ;
227206 let raster = Raster { bands : vec ! [ band] } ;
228207 asset. set_extension ( raster) . unwrap ( ) ;
229- assert ! ( asset. has_extension:: <Raster >( ) ) ;
230208 let mut item = Item :: new ( "an-id" ) ;
231209 let _ = item. assets . insert ( "data" . to_string ( ) , asset) ;
232210 }
0 commit comments