split Hashes into sepparate module

This commit is contained in:
Dietrich 2020-10-30 16:37:57 +01:00
parent 8dd2c53aa9
commit 4207e1dc0f
2 changed files with 41 additions and 39 deletions

37
src/userlib/hashes.rs Normal file
View File

@ -0,0 +1,37 @@
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
pub struct SourceHash {
hashvalue: String,
}
impl SourceHash {
pub fn new(src: &str) -> Self {
Self {
hashvalue: src.to_owned(),
}
}
pub fn has_changed(&self, new: &str) -> bool {
trace!(
"Old and new lengths: {}, {}",
self.hashvalue.len(),
new.len()
);
!self.hashvalue.eq(new)
}
}
pub struct Hashes {
pub passwd: SourceHash,
pub shadow: SourceHash,
pub group: SourceHash,
}
impl Hashes {
pub fn new(passwd: &str, shadow: &str, group: &str) -> Self {
Self {
passwd: SourceHash::new(passwd),
shadow: SourceHash::new(shadow),
group: SourceHash::new(group),
}
}
}

View File

@ -8,6 +8,7 @@
#![allow(clippy::non_ascii_literal)] #![allow(clippy::non_ascii_literal)]
pub mod files; pub mod files;
pub mod hashes;
use crate::api::GroupRead; use crate::api::GroupRead;
use crate::api::UserRead; use crate::api::UserRead;
@ -19,7 +20,7 @@ use std::io::{BufReader, Read};
pub struct UserDBLocal { pub struct UserDBLocal {
source_files: files::Files, source_files: files::Files,
source_hashes: Hashes, // to detect changes source_hashes: hashes::Hashes, // to detect changes
pub users: HashMap<String, crate::User>, pub users: HashMap<String, crate::User>,
pub groups: Vec<crate::Group>, pub groups: Vec<crate::Group>,
} }
@ -44,7 +45,7 @@ impl UserDBLocal {
}, },
users, users,
groups, groups,
source_hashes: Hashes::new(&passwd_content, &shadow_content, &group_content), source_hashes: hashes::Hashes::new(&passwd_content, &shadow_content, &group_content),
}; };
res res
} }
@ -71,7 +72,7 @@ impl UserDBLocal {
source_files: files, source_files: files,
users, users,
groups: string_to(&my_group_lines), groups: string_to(&my_group_lines),
source_hashes: Hashes::new(&my_passwd_lines, &my_shadow_lines, &my_group_lines), source_hashes: hashes::Hashes::new(&my_passwd_lines, &my_shadow_lines, &my_group_lines),
}) })
} }
} }
@ -254,42 +255,6 @@ impl UserDBValidation for UserDBLocal {
} }
} }
pub struct SourceHash {
hashvalue: String,
}
impl SourceHash {
pub fn new(src: &str) -> Self {
Self {
hashvalue: src.to_owned(),
}
}
pub fn has_changed(&self, new: &str) -> bool {
trace!(
"Old and new lengths: {}, {}",
self.hashvalue.len(),
new.len()
);
!self.hashvalue.eq(new)
}
}
pub struct Hashes {
pub passwd: SourceHash,
pub shadow: SourceHash,
pub group: SourceHash,
}
impl Hashes {
pub fn new(passwd: &str, shadow: &str, group: &str) -> Self {
Self {
passwd: SourceHash::new(passwd),
shadow: SourceHash::new(shadow),
group: SourceHash::new(group),
}
}
}
/// Parse a file to a string /// Parse a file to a string
fn file_to_string(file: &File) -> Result<String, crate::UserLibError> { fn file_to_string(file: &File) -> Result<String, crate::UserLibError> {
let mut reader = BufReader::new(file); let mut reader = BufReader::new(file);