5
5
6
6
use std:: { fmt, str:: FromStr } ;
7
7
8
- use crate :: { get_kdl_entry, kdl_value_to_string} ;
8
+ use crate :: { get_kdl_entry, kdl_value_to_integer , kdl_value_to_string} ;
9
9
10
10
use super :: FromKdlProperty ;
11
11
12
12
/// The filesystem information for a partition
13
13
/// This is used to format the partition with a filesystem
14
- ///
15
- /// The "any" filesystem type is used to indicate that any filesystem is acceptable.
16
14
#[ derive( Debug , Clone , PartialEq ) ]
17
- pub struct Filesystem {
18
- /// The filesystem type
19
- pub filesystem_type : FilesystemType ,
20
-
21
- /// The label of the filesystem
22
- pub label : Option < String > ,
23
-
24
- /// The UUID of the filesystem
25
- pub uuid : Option < String > ,
15
+ pub enum Filesystem {
16
+ Fat32 {
17
+ label : Option < String > ,
18
+ volume_id : Option < u32 > ,
19
+ } ,
20
+ Standard {
21
+ filesystem_type : StandardFilesystemType ,
22
+ label : Option < String > ,
23
+ uuid : Option < String > ,
24
+ } ,
25
+ Any ,
26
26
}
27
27
28
- /// The filesystem type for a partition
29
- #[ derive( Debug , Clone , PartialEq , Default ) ]
30
- pub enum FilesystemType {
31
- /// FAT32 filesystem
32
- Fat32 ,
33
-
34
- /// F2FS filesystem
28
+ #[ derive( Debug , Clone , PartialEq ) ]
29
+ pub enum StandardFilesystemType {
35
30
F2fs ,
36
-
37
- /// EXT4 filesystem
38
31
Ext4 ,
39
-
40
- /// XFS filesystem
41
32
Xfs ,
42
-
43
- /// Swap partition
44
33
Swap ,
45
-
46
- /// Any filesystem
47
- #[ default]
48
- Any ,
49
34
}
50
35
51
- impl fmt:: Display for FilesystemType {
36
+ impl fmt:: Display for StandardFilesystemType {
52
37
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
53
38
match self {
54
- Self :: Fat32 => f. write_str ( "fat32" ) ,
55
39
Self :: Ext4 => f. write_str ( "ext4" ) ,
56
40
Self :: F2fs => f. write_str ( "f2fs" ) ,
57
41
Self :: Xfs => f. write_str ( "xfs" ) ,
58
42
Self :: Swap => f. write_str ( "swap" ) ,
59
- Self :: Any => f. write_str ( "any" ) ,
60
43
}
61
44
}
62
45
}
63
46
64
- impl FromStr for FilesystemType {
47
+ impl FromStr for StandardFilesystemType {
65
48
type Err = crate :: Error ;
66
49
67
- /// Attempt to convert a string to a filesystem type
68
50
fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
69
51
match value {
70
- "fat32" => Ok ( Self :: Fat32 ) ,
71
52
"ext4" => Ok ( Self :: Ext4 ) ,
72
53
"f2fs" => Ok ( Self :: F2fs ) ,
73
54
"xfs" => Ok ( Self :: Xfs ) ,
74
55
"swap" => Ok ( Self :: Swap ) ,
75
- "any" => Ok ( Self :: Any ) ,
76
56
_ => Err ( crate :: Error :: UnknownVariant ) ,
77
57
}
78
58
}
79
59
}
80
60
81
- impl FromKdlProperty < ' _ > for FilesystemType {
61
+ impl FromKdlProperty < ' _ > for StandardFilesystemType {
82
62
fn from_kdl_property ( entry : & kdl:: KdlEntry ) -> Result < Self , crate :: Error > {
83
63
let value = kdl_value_to_string ( entry) ?;
84
64
let v = value. parse ( ) . map_err ( |_| crate :: UnsupportedValue {
@@ -91,15 +71,17 @@ impl FromKdlProperty<'_> for FilesystemType {
91
71
92
72
impl Filesystem {
93
73
pub fn from_kdl_node ( node : & kdl:: KdlNode ) -> Result < Self , crate :: Error > {
94
- let mut filesystem_type = None ;
74
+ let mut fs_type = None ;
95
75
let mut label = None ;
96
76
let mut uuid = None ;
77
+ let mut volume_id = None ;
97
78
98
79
for entry in node. iter_children ( ) {
99
80
match entry. name ( ) . value ( ) {
100
- "type" => filesystem_type = Some ( FilesystemType :: from_kdl_property ( get_kdl_entry ( entry, & 0 ) ?) ?) ,
81
+ "type" => fs_type = Some ( kdl_value_to_string ( get_kdl_entry ( entry, & 0 ) ?) ?) ,
101
82
"label" => label = Some ( kdl_value_to_string ( get_kdl_entry ( entry, & 0 ) ?) ?) ,
102
83
"uuid" => uuid = Some ( kdl_value_to_string ( get_kdl_entry ( entry, & 0 ) ?) ?) ,
84
+ "volume_id" => volume_id = Some ( kdl_value_to_integer ( get_kdl_entry ( entry, & 0 ) ?) ? as u32 ) ,
103
85
_ => {
104
86
return Err ( crate :: UnsupportedNode {
105
87
at : entry. span ( ) ,
@@ -110,13 +92,37 @@ impl Filesystem {
110
92
}
111
93
}
112
94
113
- Ok ( Self {
114
- filesystem_type : filesystem_type. ok_or ( crate :: UnsupportedNode {
115
- at : node. span ( ) ,
116
- name : "type" . into ( ) ,
117
- } ) ?,
118
- label,
119
- uuid,
120
- } )
95
+ let fs_type = fs_type. ok_or ( crate :: UnsupportedNode {
96
+ at : node. span ( ) ,
97
+ name : "type" . into ( ) ,
98
+ } ) ?;
99
+
100
+ match fs_type. as_str ( ) {
101
+ "fat32" => {
102
+ if uuid. is_some ( ) {
103
+ return Err ( crate :: InvalidArguments {
104
+ at : node. span ( ) ,
105
+ advice : Some ( "FAT32 does not support UUID" . into ( ) ) ,
106
+ }
107
+ . into ( ) ) ;
108
+ }
109
+ Ok ( Filesystem :: Fat32 { label, volume_id } )
110
+ }
111
+ "any" => Ok ( Filesystem :: Any ) ,
112
+ fs_type => {
113
+ if volume_id. is_some ( ) {
114
+ return Err ( crate :: InvalidArguments {
115
+ at : node. span ( ) ,
116
+ advice : Some ( format ! ( "volume_id is only supported for FAT32, not {}" , fs_type) ) ,
117
+ }
118
+ . into ( ) ) ;
119
+ }
120
+ Ok ( Filesystem :: Standard {
121
+ filesystem_type : fs_type. parse ( ) ?,
122
+ label,
123
+ uuid,
124
+ } )
125
+ }
126
+ }
121
127
}
122
128
}
0 commit comments