1
1
use std:: {
2
+ ffi:: OsStr ,
2
3
fs:: read_to_string,
3
4
path:: { Path , PathBuf } ,
4
5
} ;
@@ -23,35 +24,42 @@ pub struct Meta {
23
24
}
24
25
25
26
impl CargoEspFlashMeta {
26
- pub fn load < P : AsRef < Path > > ( path : P ) -> Result < CargoEspFlashMeta > {
27
- let path = path. as_ref ( ) ;
28
- if !path. exists ( ) {
27
+ pub fn load < P > ( manifest : P ) -> Result < CargoEspFlashMeta >
28
+ where
29
+ P : AsRef < Path > ,
30
+ {
31
+ let manifest = manifest. as_ref ( ) ;
32
+ if !manifest. exists ( ) {
29
33
return Err ( Error :: NoProject . into ( ) ) ;
30
34
}
31
- let toml = read_to_string ( path)
35
+
36
+ let toml = read_to_string ( manifest)
32
37
. into_diagnostic ( )
33
38
. wrap_err ( "Failed to read Cargo.toml" ) ?;
39
+
34
40
let manifest = Manifest :: < Meta > :: from_slice_with_metadata ( toml. as_bytes ( ) )
35
41
. map_err ( move |e| TomlError :: new ( e, toml) )
36
42
. wrap_err ( "Failed to parse Cargo.toml" ) ?;
43
+
37
44
let meta = manifest
38
45
. package
39
46
. and_then ( |pkg| pkg. metadata )
40
47
. unwrap_or_default ( )
41
48
. espflash
42
49
. unwrap_or_default ( ) ;
43
- match meta. partition_table {
44
- Some ( table) if !table. ends_with ( ".csv" ) => {
45
- return Err ( Error :: InvalidPartitionTablePath . into ( ) )
50
+
51
+ if let Some ( table) = & meta. partition_table {
52
+ if table. extension ( ) != Some ( OsStr :: new ( "csv" ) ) {
53
+ return Err ( Error :: InvalidPartitionTablePath . into ( ) ) ;
46
54
}
47
- _ => { }
48
55
}
49
- match meta. bootloader {
50
- Some ( table) if !table. ends_with ( ".bin" ) => {
51
- return Err ( Error :: InvalidBootloaderPath . into ( ) )
56
+
57
+ if let Some ( bootloader) = & meta. bootloader {
58
+ if bootloader. extension ( ) != Some ( OsStr :: new ( "bin" ) ) {
59
+ return Err ( Error :: InvalidBootloaderPath . into ( ) ) ;
52
60
}
53
- _ => { }
54
61
}
62
+
55
63
Ok ( meta)
56
64
}
57
65
}
0 commit comments