@@ -7,14 +7,20 @@ use std::{
7
7
} ;
8
8
9
9
use serde:: Deserialize ;
10
- use snafu:: { ResultExt as _, Snafu } ;
10
+ use snafu:: { ResultExt as _, Snafu , ensure } ;
11
11
12
12
use crate :: { IfContext , build:: docker:: BuildArguments } ;
13
13
14
14
#[ derive( Debug , Snafu ) ]
15
15
pub enum ParseImageError {
16
16
#[ snafu( display( "encountered invalid format, expected name[=version,...]" ) ) ]
17
17
InvalidFormat ,
18
+
19
+ #[ snafu( display( "the path contains unsupported characters: '.' or '~'" ) ) ]
20
+ UnsupportedChars ,
21
+
22
+ #[ snafu( display( "absolute paths are not supported" ) ) ]
23
+ AbsolutePath ,
18
24
}
19
25
20
26
#[ derive( Clone , Debug ) ]
@@ -29,11 +35,16 @@ impl FromStr for Image {
29
35
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
30
36
let parts: Vec < _ > = s. split ( '=' ) . collect ( ) ;
31
37
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
+
32
43
match parts. len ( ) {
33
- 1 => Ok ( Self :: new_unversioned ( parts [ 0 ] . to_owned ( ) ) ) ,
44
+ 1 => Ok ( Self :: new_unversioned ( image_name ) ) ,
34
45
2 => {
35
46
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) )
37
48
}
38
49
_ => InvalidFormatSnafu . fail ( ) ,
39
50
}
0 commit comments