11//! Links.
22
3- use crate :: { mime:: APPLICATION_GEOJSON , Error , Result } ;
3+ use crate :: { mime:: APPLICATION_GEOJSON , Error , Href , Result } ;
44use mime:: APPLICATION_JSON ;
55use serde:: { Deserialize , Serialize } ;
66use serde_json:: { Map , Value } ;
@@ -88,7 +88,7 @@ pub struct Link {
8888}
8989
9090/// Implemented by any object that has links.
91- pub trait Links {
91+ pub trait Links : Href {
9292 /// Returns a reference to this object's links.
9393 ///
9494 /// # Examples
@@ -217,7 +217,7 @@ pub trait Links {
217217 Box :: new ( self . links ( ) . iter ( ) . filter ( |link| link. is_item ( ) ) )
218218 }
219219
220- /// Makes all relative links absolute with respect to an href.
220+ /// Makes all relative links absolute with respect to this object's href.
221221 ///
222222 /// # Examples
223223 ///
@@ -226,15 +226,19 @@ pub trait Links {
226226 ///
227227 /// let mut catalog: stac::Catalog = stac::read("examples/catalog.json").unwrap();
228228 /// assert!(!catalog.root_link().unwrap().is_absolute());
229- /// catalog.make_relative_links_absolute("examples/catalog.json" ).unwrap();
229+ /// catalog.make_relative_links_absolute().unwrap();
230230 /// assert!(catalog.root_link().unwrap().is_absolute());
231231 /// ```
232- fn make_relative_links_absolute ( & mut self , href : impl ToString ) -> Result < ( ) > {
233- let href = make_absolute ( href. to_string ( ) , None ) ?;
234- for link in self . links_mut ( ) {
235- link. href = make_absolute ( std:: mem:: take ( & mut link. href ) , Some ( & href) ) ?;
232+ fn make_relative_links_absolute ( & mut self ) -> Result < ( ) > {
233+ if let Some ( href) = self . href ( ) {
234+ let href = make_absolute ( href. to_string ( ) , None ) ?;
235+ for link in self . links_mut ( ) {
236+ link. href = make_absolute ( std:: mem:: take ( & mut link. href ) , Some ( & href) ) ?;
237+ }
238+ Ok ( ( ) )
239+ } else {
240+ Err ( Error :: NoHref )
236241 }
237- Ok ( ( ) )
238242 }
239243
240244 /// Makes all absolute links relative with respect to an href.
@@ -248,7 +252,7 @@ pub trait Links {
248252 ///
249253 /// let mut catalog: stac::Catalog = stac::read("examples/catalog.json").unwrap();
250254 /// assert!(!catalog.root_link().unwrap().is_absolute());
251- /// catalog.make_relative_links_absolute("examples/catalog.json" ).unwrap();
255+ /// catalog.make_relative_links_absolute().unwrap();
252256 /// assert!(catalog.root_link().unwrap().is_absolute());
253257 /// catalog.make_absolute_links_relative("examples/catalog.json").unwrap();
254258 /// assert!(catalog.root_link().unwrap().is_relative());
@@ -817,7 +821,7 @@ mod tests {
817821 }
818822
819823 mod links {
820- use crate :: { Catalog , Item , Link , Links } ;
824+ use crate :: { Catalog , Href , Item , Link , Links } ;
821825
822826 #[ test]
823827 fn link ( ) {
@@ -846,9 +850,7 @@ mod tests {
846850 #[ test]
847851 fn make_relative_links_absolute_path ( ) {
848852 let mut catalog: Catalog = crate :: read ( "examples/catalog.json" ) . unwrap ( ) ;
849- catalog
850- . make_relative_links_absolute ( "examples/catalog.json" )
851- . unwrap ( ) ;
853+ catalog. make_relative_links_absolute ( ) . unwrap ( ) ;
852854 for link in catalog. links ( ) {
853855 assert ! ( link. is_absolute( ) ) ;
854856 }
@@ -857,9 +859,8 @@ mod tests {
857859 #[ test]
858860 fn make_relative_links_absolute_url ( ) {
859861 let mut catalog: Catalog = crate :: read ( "examples/catalog.json" ) . unwrap ( ) ;
860- catalog
861- . make_relative_links_absolute ( "http://stac-rs.test/catalog.json" )
862- . unwrap ( ) ;
862+ catalog. set_href ( "http://stac-rs.test/catalog.json" ) ;
863+ catalog. make_relative_links_absolute ( ) . unwrap ( ) ;
863864 for link in catalog. links ( ) {
864865 assert ! ( link. is_absolute( ) ) ;
865866 }
@@ -872,9 +873,7 @@ mod tests {
872873 #[ test]
873874 fn make_absolute_links_relative_path ( ) {
874875 let mut catalog: Catalog = crate :: read ( "examples/catalog.json" ) . unwrap ( ) ;
875- catalog
876- . make_relative_links_absolute ( "examples/catalog.json" )
877- . unwrap ( ) ;
876+ catalog. make_relative_links_absolute ( ) . unwrap ( ) ;
878877 catalog. make_absolute_links_relative ( "examples/" ) . unwrap ( ) ;
879878 for link in catalog. links ( ) {
880879 if !link. is_self ( ) {
@@ -886,9 +885,8 @@ mod tests {
886885 #[ test]
887886 fn make_absolute_links_relative_url ( ) {
888887 let mut catalog: Catalog = crate :: read ( "examples/catalog.json" ) . unwrap ( ) ;
889- catalog
890- . make_relative_links_absolute ( "http://stac-rs.test/catalog.json" )
891- . unwrap ( ) ;
888+ catalog. set_href ( "http://stac-rs.test/catalog.json" ) ;
889+ catalog. make_relative_links_absolute ( ) . unwrap ( ) ;
892890 catalog
893891 . make_absolute_links_relative ( "http://stac-rs.test/" )
894892 . unwrap ( ) ;
0 commit comments