@@ -51,6 +51,18 @@ impl Config {
5151
5252 Some ( self . clone ( ) )
5353 }
54+
55+ pub fn is_empty ( & self ) -> bool {
56+ if self . root . as_ref ( ) . is_some_and ( |r| !r. is_empty ( ) ) {
57+ return false ;
58+ }
59+
60+ if self . first_user . as_ref ( ) . is_some_and ( |u| !u. is_empty ( ) ) {
61+ return false ;
62+ }
63+
64+ true
65+ }
5466}
5567
5668/// First user settings
@@ -73,9 +85,15 @@ pub struct FirstUserConfig {
7385}
7486
7587impl FirstUserConfig {
76- /// Whether it is a valid user.
88+ /// Whether it is an empty user.
89+ pub fn is_empty ( & self ) -> bool {
90+ self . user_name . is_none ( )
91+ }
92+
7793 pub fn is_valid ( & self ) -> bool {
78- self . user_name . is_some ( )
94+ self . user_name . as_ref ( ) . is_some_and ( |n| !n. is_empty ( ) )
95+ && self . full_name . as_ref ( ) . is_some_and ( |n| !n. is_empty ( ) )
96+ && self . password . as_ref ( ) . is_some_and ( |p| !p. is_empty ( ) )
7997 }
8098}
8199
@@ -94,6 +112,12 @@ pub struct UserPassword {
94112 pub hashed_password : bool ,
95113}
96114
115+ impl UserPassword {
116+ pub fn is_empty ( & self ) -> bool {
117+ self . password . is_empty ( )
118+ }
119+ }
120+
97121fn overwrite_if_not_empty ( old : & mut String , new : String ) {
98122 if !new. is_empty ( ) {
99123 * old = new;
@@ -119,13 +143,25 @@ pub struct RootUserConfig {
119143
120144impl RootUserConfig {
121145 pub fn is_empty ( & self ) -> bool {
122- self . password . is_none ( ) && self . ssh_public_key . is_none ( )
146+ if self
147+ . password
148+ . as_ref ( )
149+ . is_some_and ( |p| !p. password . is_empty ( ) )
150+ {
151+ return false ;
152+ }
153+
154+ if self . ssh_public_key . as_ref ( ) . is_some_and ( |p| !p. is_empty ( ) ) {
155+ return false ;
156+ }
157+
158+ return true ;
123159 }
124160}
125161
126162#[ cfg( test) ]
127163mod test {
128- use super :: { FirstUserConfig , RootUserConfig , UserPassword } ;
164+ use super :: { Config , FirstUserConfig , RootUserConfig , UserPassword } ;
129165
130166 #[ test]
131167 fn test_parse_user_password ( ) {
@@ -139,4 +175,116 @@ mod test {
139175 assert_eq ! ( & password. password, "$a$b123" ) ;
140176 assert_eq ! ( password. hashed_password, false ) ;
141177 }
178+
179+ #[ test]
180+ fn test_is_empty ( ) {
181+ assert_eq ! ( Config :: default ( ) . is_empty( ) , true ) ;
182+
183+ let empty_user_config = Config {
184+ first_user : Some ( FirstUserConfig :: default ( ) ) ,
185+ ..Default :: default ( )
186+ } ;
187+ assert_eq ! ( empty_user_config. is_empty( ) , true ) ;
188+
189+ let empty_root_config = Config {
190+ root : Some ( RootUserConfig :: default ( ) ) ,
191+ ..Default :: default ( )
192+ } ;
193+ assert_eq ! ( empty_root_config. is_empty( ) , true ) ;
194+
195+ let password = UserPassword {
196+ password : "secret" . to_string ( ) ,
197+ hashed_password : false ,
198+ } ;
199+ let empty_password = UserPassword {
200+ password : "" . to_string ( ) ,
201+ hashed_password : false ,
202+ } ;
203+
204+ let user_with_password = FirstUserConfig {
205+ user_name : Some ( "jane" . to_string ( ) ) ,
206+ password : Some ( password. clone ( ) ) ,
207+ ..Default :: default ( )
208+ } ;
209+ let user_with_password_config = Config {
210+ first_user : Some ( user_with_password) ,
211+ ..Default :: default ( )
212+ } ;
213+ assert_eq ! ( user_with_password_config. is_empty( ) , false ) ;
214+
215+ let root_with_password = RootUserConfig {
216+ password : Some ( password. clone ( ) ) ,
217+ ..Default :: default ( )
218+ } ;
219+ let root_with_password_config = Config {
220+ root : Some ( root_with_password) ,
221+ ..Default :: default ( )
222+ } ;
223+ assert_eq ! ( root_with_password_config. is_empty( ) , false ) ;
224+
225+ let root_with_empty_password = RootUserConfig {
226+ password : Some ( empty_password. clone ( ) ) ,
227+ ..Default :: default ( )
228+ } ;
229+ let root_with_empty_password_config = Config {
230+ root : Some ( root_with_empty_password) ,
231+ ..Default :: default ( )
232+ } ;
233+ assert_eq ! ( root_with_empty_password_config. is_empty( ) , true ) ;
234+
235+ let root_with_ssh_key = RootUserConfig {
236+ ssh_public_key : Some ( "12345678" . to_string ( ) ) ,
237+ ..Default :: default ( )
238+ } ;
239+ let root_with_ssh_key_config = Config {
240+ root : Some ( root_with_ssh_key) ,
241+ ..Default :: default ( )
242+ } ;
243+ assert_eq ! ( root_with_ssh_key_config. is_empty( ) , false ) ;
244+ }
245+
246+ #[ test]
247+ fn test_user_is_valid ( ) {
248+ assert_eq ! ( FirstUserConfig :: default ( ) . is_valid( ) , false ) ;
249+
250+ let valid_user = FirstUserConfig {
251+ user_name : Some ( "firstuser" . to_string ( ) ) ,
252+ full_name : Some ( "First User" . to_string ( ) ) ,
253+ password : Some ( UserPassword {
254+ password : "12345678" . to_string ( ) ,
255+ hashed_password : false ,
256+ } ) ,
257+ } ;
258+ assert_eq ! ( valid_user. is_valid( ) , true ) ;
259+
260+ let empty_user_name = FirstUserConfig {
261+ user_name : Some ( "" . to_string ( ) ) ,
262+ full_name : Some ( "First User" . to_string ( ) ) ,
263+ password : Some ( UserPassword {
264+ password : "12345678" . to_string ( ) ,
265+ hashed_password : false ,
266+ } ) ,
267+ } ;
268+ assert_eq ! ( empty_user_name. is_valid( ) , false ) ;
269+
270+ let empty_full_name = FirstUserConfig {
271+ user_name : Some ( "firstuser" . to_string ( ) ) ,
272+ full_name : Some ( "" . to_string ( ) ) ,
273+ password : Some ( UserPassword {
274+ password : "12345678" . to_string ( ) ,
275+ hashed_password : false ,
276+ } ) ,
277+ } ;
278+ assert_eq ! ( empty_full_name. is_valid( ) , false ) ;
279+
280+ let empty_password = FirstUserConfig {
281+ user_name : Some ( "firstuser" . to_string ( ) ) ,
282+ full_name : Some ( "First User" . to_string ( ) ) ,
283+ password : Some ( UserPassword {
284+ password : "" . to_string ( ) ,
285+ hashed_password : false ,
286+ } ) ,
287+ } ;
288+ assert_eq ! ( empty_password. is_valid( ) , false ) ;
289+ }
142290}
0 commit comments