@@ -7,14 +7,20 @@ use std::{
77} ;
88
99use serde:: Deserialize ;
10- use snafu:: { ResultExt as _, Snafu } ;
10+ use snafu:: { ResultExt as _, Snafu , ensure } ;
1111
1212use crate :: { IfContext , build:: docker:: BuildArguments } ;
1313
1414#[ derive( Debug , Snafu ) ]
1515pub enum ParseImageError {
1616 #[ snafu( display( "encountered invalid format, expected name[=version,...]" ) ) ]
1717 InvalidFormat ,
18+
19+ #[ snafu( display( "the path contains unsupported characters: '.' or '~'" ) ) ]
20+ UnsupportedChars ,
21+
22+ #[ snafu( display( "absolute paths are not supported" ) ) ]
23+ AbsolutePath ,
1824}
1925
2026#[ derive( Clone , Debug ) ]
@@ -29,11 +35,16 @@ impl FromStr for Image {
2935 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
3036 let parts: Vec < _ > = s. split ( '=' ) . collect ( ) ;
3137
38+ ensure ! ( !parts[ 0 ] . contains( [ '.' , '~' ] ) , UnsupportedCharsSnafu ) ;
39+ ensure ! ( !parts[ 0 ] . starts_with( '/' ) , AbsolutePathSnafu ) ;
40+
41+ let image_name = parts[ 0 ] . trim_end_matches ( '/' ) . to_owned ( ) ;
42+
3243 match parts. len ( ) {
33- 1 => Ok ( Self :: new_unversioned ( parts [ 0 ] . to_owned ( ) ) ) ,
44+ 1 => Ok ( Self :: new_unversioned ( image_name ) ) ,
3445 2 => {
3546 let versions: Vec < _ > = parts[ 1 ] . split ( ',' ) . map ( ToOwned :: to_owned) . collect ( ) ;
36- Ok ( Self :: new ( parts [ 0 ] . to_owned ( ) , versions) )
47+ Ok ( Self :: new ( image_name , versions) )
3748 }
3849 _ => InvalidFormatSnafu . fail ( ) ,
3950 }
0 commit comments