fix some warnings

This commit is contained in:
dietrich 2020-10-09 11:59:23 +02:00
parent b6eb9972d2
commit f14d4b34ee
4 changed files with 25 additions and 19 deletions

View File

@ -102,11 +102,11 @@ impl NewFromString for Group {
println!("{}", &line); println!("{}", &line);
let elements: Vec<String> = line.split(':').map(ToString::to_string).collect(); let elements: Vec<String> = line.split(':').map(ToString::to_string).collect();
if elements.len() == 4 { if elements.len() == 4 {
Ok(Group { Ok(Self {
groupname: Groupname::try_from(elements.get(0).unwrap().to_string())?, groupname: Groupname::try_from(elements.get(0).unwrap().to_string())?,
password: crate::Password::Disabled, password: crate::Password::Disabled,
gid: crate::Gid::try_from(elements.get(2).unwrap().to_string())?, 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 { } else {
Err(UserLibError::Message(format!( Err(UserLibError::Message(format!(
@ -118,13 +118,15 @@ impl NewFromString for Group {
} }
} }
fn parse_members_list(source: String) -> Vec<crate::Username> { fn parse_members_list(source: &str) -> Vec<crate::Username> {
let mut res = vec![]; let mut res = vec![];
for mem in source for mem in source.split(',').filter_map(|x| {
.split(',') if x.is_empty() {
.filter(|x| !x.is_empty()) None
.map(ToString::to_string) } else {
{ Some(x.to_string())
}
}) {
res.push(crate::Username::try_from(mem).expect("failed to parse username")); res.push(crate::Username::try_from(mem).expect("failed to parse username"));
} }
res res

View File

@ -63,9 +63,9 @@ pub enum Password {
impl Display for Password { impl Display for Password {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Password::Encrypted(EncryptedPassword { password }) => write!(f, "{}", password,), Self::Encrypted(EncryptedPassword { password }) => write!(f, "{}", password,),
Password::Shadow(_) => write!(f, "x"), Self::Shadow(_) => write!(f, "x"),
Password::Disabled => write!(f, "x"), Self::Disabled => write!(f, "x"),
} }
} }
} }
@ -183,7 +183,7 @@ impl Display for ShellPath {
impl TryFrom<String> for ShellPath { impl TryFrom<String> for ShellPath {
type Error = UserLibError; type Error = UserLibError;
fn try_from(source: String) -> std::result::Result<Self, Self::Error> { fn try_from(source: String) -> std::result::Result<Self, Self::Error> {
Ok(ShellPath { shell: source }) Ok(Self { shell: source })
} }
} }

View File

@ -97,7 +97,7 @@ impl NewFromString for Shadow {
let elements: Vec<String> = line.split(':').map(ToString::to_string).collect(); let elements: Vec<String> = line.split(':').map(ToString::to_string).collect();
if elements.len() == 9 { if elements.len() == 9 {
let extra = elements.get(8).unwrap(); let extra = elements.get(8).unwrap();
Ok(Shadow { Ok(Self {
username: crate::Username::try_from(elements.get(0).unwrap().to_string())?, username: crate::Username::try_from(elements.get(0).unwrap().to_string())?,
password: crate::EncryptedPassword::try_from(elements.get(1).unwrap().to_string())?, password: crate::EncryptedPassword::try_from(elements.get(1).unwrap().to_string())?,
last_change: date_since_epoch(elements.get(2).unwrap()), last_change: date_since_epoch(elements.get(2).unwrap()),

View File

@ -38,11 +38,11 @@ impl Default for Files {
impl UserDBLocal { impl UserDBLocal {
#[must_use] #[must_use]
pub fn import_from_strings( pub fn import_from_strings(
passwd_content: String, passwd_content: &str,
shadow_content: String, shadow_content: &str,
group_content: String, group_content: &str,
) -> Self { ) -> Self {
let res = UserDBLocal { let res = Self {
source_files: Files { source_files: Files {
passwd: None, passwd: None,
group: 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 { pub trait NewFromString {
fn new_from_string(line: String) -> Result<Self, crate::UserLibError> fn new_from_string(line: String) -> Result<Self, crate::UserLibError>
where where
@ -153,7 +157,7 @@ where
#[test] #[test]
fn test_creator_user_db_local() { 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!( assert_eq!(
data.passwd_entries.get(0).unwrap().get_username(), data.passwd_entries.get(0).unwrap().get_username(),
"testuser" "testuser"
@ -172,6 +176,6 @@ fn test_parsing_local_database() {
let mut group_reader = BufReader::new(group_file); let mut group_reader = BufReader::new(group_file);
let mut my_group_lines = "".to_string(); let mut my_group_lines = "".to_string();
group_reader.read_to_string(&mut my_group_lines).unwrap(); 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"); assert_eq!(data.group_entries.get(0).unwrap().get_groupname(), "root");
} }