1- use crate :: { Band , DataType , Href , Statistics } ;
1+ use crate :: { Band , DataType , Href , Result , Statistics } ;
22use indexmap:: IndexMap ;
33use serde:: { Deserialize , Serialize } ;
44use serde_json:: { Map , Value } ;
@@ -112,6 +112,14 @@ pub trait Assets {
112112 /// item.assets_mut().insert("foo".to_string(), Asset::new("./asset.tif"));
113113 /// ```
114114 fn assets_mut ( & mut self ) -> & mut IndexMap < String , Asset > ;
115+
116+ /// Makes all asset hrefs absolute.
117+ fn make_assets_absolute ( & mut self , base : impl AsRef < Href > ) -> Result < ( ) > {
118+ for asset in self . assets_mut ( ) . values_mut ( ) {
119+ asset. href = asset. href . into_absolute ( & base) ?;
120+ }
121+ Ok ( ( ) )
122+ }
115123}
116124
117125impl Asset {
@@ -174,7 +182,8 @@ impl<'a> From<&'a str> for Asset {
174182
175183#[ cfg( test) ]
176184mod tests {
177- use super :: Asset ;
185+ use super :: { Asset , Assets } ;
186+ use crate :: { Href , Item } ;
178187
179188 #[ test]
180189 fn new ( ) {
@@ -195,4 +204,14 @@ mod tests {
195204 assert ! ( value. get( "type" ) . is_none( ) ) ;
196205 assert ! ( value. get( "roles" ) . is_none( ) ) ;
197206 }
207+
208+ #[ test]
209+ fn make_absolute ( ) {
210+ let asset = Asset :: new ( "an-href" ) ;
211+ let mut item = Item :: new ( "an-item" ) ;
212+ let _ = item. assets . insert ( "data" . into ( ) , asset) ;
213+ item. make_assets_absolute ( Href :: from ( "http://rustac.test" ) )
214+ . unwrap ( ) ;
215+ assert_eq ! ( item. assets[ "data" ] . href, "http://rustac.test/an-href" ) ;
216+ }
198217}
0 commit comments