55
66use std:: { fmt, str:: FromStr } ;
77
8- use crate :: { get_kdl_entry, kdl_value_to_string} ;
8+ use crate :: { get_kdl_entry, kdl_value_to_integer , kdl_value_to_string} ;
99
1010use super :: FromKdlProperty ;
1111
1212/// The filesystem information for a partition
1313/// 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.
1614#[ 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 ,
2626}
2727
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 {
3530 F2fs ,
36-
37- /// EXT4 filesystem
3831 Ext4 ,
39-
40- /// XFS filesystem
4132 Xfs ,
42-
43- /// Swap partition
4433 Swap ,
45-
46- /// Any filesystem
47- #[ default]
48- Any ,
4934}
5035
51- impl fmt:: Display for FilesystemType {
36+ impl fmt:: Display for StandardFilesystemType {
5237 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5338 match self {
54- Self :: Fat32 => f. write_str ( "fat32" ) ,
5539 Self :: Ext4 => f. write_str ( "ext4" ) ,
5640 Self :: F2fs => f. write_str ( "f2fs" ) ,
5741 Self :: Xfs => f. write_str ( "xfs" ) ,
5842 Self :: Swap => f. write_str ( "swap" ) ,
59- Self :: Any => f. write_str ( "any" ) ,
6043 }
6144 }
6245}
6346
64- impl FromStr for FilesystemType {
47+ impl FromStr for StandardFilesystemType {
6548 type Err = crate :: Error ;
6649
67- /// Attempt to convert a string to a filesystem type
6850 fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
6951 match value {
70- "fat32" => Ok ( Self :: Fat32 ) ,
7152 "ext4" => Ok ( Self :: Ext4 ) ,
7253 "f2fs" => Ok ( Self :: F2fs ) ,
7354 "xfs" => Ok ( Self :: Xfs ) ,
7455 "swap" => Ok ( Self :: Swap ) ,
75- "any" => Ok ( Self :: Any ) ,
7656 _ => Err ( crate :: Error :: UnknownVariant ) ,
7757 }
7858 }
7959}
8060
81- impl FromKdlProperty < ' _ > for FilesystemType {
61+ impl FromKdlProperty < ' _ > for StandardFilesystemType {
8262 fn from_kdl_property ( entry : & kdl:: KdlEntry ) -> Result < Self , crate :: Error > {
8363 let value = kdl_value_to_string ( entry) ?;
8464 let v = value. parse ( ) . map_err ( |_| crate :: UnsupportedValue {
@@ -91,15 +71,17 @@ impl FromKdlProperty<'_> for FilesystemType {
9171
9272impl Filesystem {
9373 pub fn from_kdl_node ( node : & kdl:: KdlNode ) -> Result < Self , crate :: Error > {
94- let mut filesystem_type = None ;
74+ let mut fs_type = None ;
9575 let mut label = None ;
9676 let mut uuid = None ;
77+ let mut volume_id = None ;
9778
9879 for entry in node. iter_children ( ) {
9980 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 ) ?) ?) ,
10182 "label" => label = Some ( kdl_value_to_string ( get_kdl_entry ( entry, & 0 ) ?) ?) ,
10283 "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 ) ,
10385 _ => {
10486 return Err ( crate :: UnsupportedNode {
10587 at : entry. span ( ) ,
@@ -110,13 +92,37 @@ impl Filesystem {
11092 }
11193 }
11294
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+ }
121127 }
122128}
0 commit comments