diff --git a/src/group/mod.rs b/src/group/mod.rs index 7aaae21..90e6725 100644 --- a/src/group/mod.rs +++ b/src/group/mod.rs @@ -102,11 +102,11 @@ impl NewFromString for Group { println!("{}", &line); let elements: Vec = line.split(':').map(ToString::to_string).collect(); if elements.len() == 4 { - Ok(Group { + Ok(Self { groupname: Groupname::try_from(elements.get(0).unwrap().to_string())?, password: crate::Password::Disabled, gid: crate::Gid::try_from(elements.get(2).unwrap().to_string())?, - members: parse_members_list(elements.get(3).unwrap().to_string()), + members: parse_members_list(elements.get(3).unwrap()), }) } else { Err(UserLibError::Message(format!( @@ -118,13 +118,15 @@ impl NewFromString for Group { } } -fn parse_members_list(source: String) -> Vec { +fn parse_members_list(source: &str) -> Vec { let mut res = vec![]; - for mem in source - .split(',') - .filter(|x| !x.is_empty()) - .map(ToString::to_string) - { + for mem in source.split(',').filter_map(|x| { + if x.is_empty() { + None + } else { + Some(x.to_string()) + } + }) { res.push(crate::Username::try_from(mem).expect("failed to parse username")); } res diff --git a/src/user/passwd_fields.rs b/src/user/passwd_fields.rs index 811b8cf..3e67e8c 100644 --- a/src/user/passwd_fields.rs +++ b/src/user/passwd_fields.rs @@ -63,9 +63,9 @@ pub enum Password { impl Display for Password { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Password::Encrypted(EncryptedPassword { password }) => write!(f, "{}", password,), - Password::Shadow(_) => write!(f, "x"), - Password::Disabled => write!(f, "x"), + Self::Encrypted(EncryptedPassword { password }) => write!(f, "{}", password,), + Self::Shadow(_) => write!(f, "x"), + Self::Disabled => write!(f, "x"), } } } @@ -183,7 +183,7 @@ impl Display for ShellPath { impl TryFrom for ShellPath { type Error = UserLibError; fn try_from(source: String) -> std::result::Result { - Ok(ShellPath { shell: source }) + Ok(Self { shell: source }) } } diff --git a/src/user/shadow_fields.rs b/src/user/shadow_fields.rs index 3e0494f..423af98 100644 --- a/src/user/shadow_fields.rs +++ b/src/user/shadow_fields.rs @@ -97,7 +97,7 @@ impl NewFromString for Shadow { let elements: Vec = line.split(':').map(ToString::to_string).collect(); if elements.len() == 9 { let extra = elements.get(8).unwrap(); - Ok(Shadow { + Ok(Self { username: crate::Username::try_from(elements.get(0).unwrap().to_string())?, password: crate::EncryptedPassword::try_from(elements.get(1).unwrap().to_string())?, last_change: date_since_epoch(elements.get(2).unwrap()), diff --git a/src/userlib.rs b/src/userlib.rs index 93be679..82c3d24 100644 --- a/src/userlib.rs +++ b/src/userlib.rs @@ -38,11 +38,11 @@ impl Default for Files { impl UserDBLocal { #[must_use] pub fn import_from_strings( - passwd_content: String, - shadow_content: String, - group_content: String, + passwd_content: &str, + shadow_content: &str, + group_content: &str, ) -> Self { - let res = UserDBLocal { + let res = Self { source_files: Files { passwd: None, group: None, @@ -128,6 +128,10 @@ impl UserDBLocal { } } +/// Try to parse a String into some Object +/// +/// # Errors +/// if the parsing failed a [`UserLibError::Message`] is returned containing a more detailed error message. pub trait NewFromString { fn new_from_string(line: String) -> Result where @@ -153,7 +157,7 @@ where #[test] fn test_creator_user_db_local() { - let data = UserDBLocal::import_from_strings("testuser:x:1001:1001:full Name,004,000342,001-2312,myemail@test.com:/home/test:/bin/test".to_string(), "test:!!$6$/RotIe4VZzzAun4W$7YUONvru1rDnllN5TvrnOMsWUD5wSDUPAD6t6/Xwsr/0QOuWF3HcfAhypRkGa8G1B9qqWV5kZSnCb8GKMN9N61:18260:0:99999:7:::".to_string(), "teste:x:1002:test,teste".to_string()); + let data = UserDBLocal::import_from_strings("testuser:x:1001:1001:full Name,004,000342,001-2312,myemail@test.com:/home/test:/bin/test", "test:!!$6$/RotIe4VZzzAun4W$7YUONvru1rDnllN5TvrnOMsWUD5wSDUPAD6t6/Xwsr/0QOuWF3HcfAhypRkGa8G1B9qqWV5kZSnCb8GKMN9N61:18260:0:99999:7:::", "teste:x:1002:test,teste"); assert_eq!( data.passwd_entries.get(0).unwrap().get_username(), "testuser" @@ -172,6 +176,6 @@ fn test_parsing_local_database() { let mut group_reader = BufReader::new(group_file); let mut my_group_lines = "".to_string(); group_reader.read_to_string(&mut my_group_lines).unwrap(); - let data = UserDBLocal::import_from_strings(my_passwd_lines, "".to_string(), my_group_lines); + let data = UserDBLocal::import_from_strings(&my_passwd_lines, "", &my_group_lines); assert_eq!(data.group_entries.get(0).unwrap().get_groupname(), "root"); }