add getters to User

This commit is contained in:
Dietrich 2020-10-23 19:49:47 +02:00
parent 83255390e3
commit 67af4a39cd

View File

@ -1,4 +1,5 @@
pub mod gecos_fields;
pub mod passwd_fields;
pub mod shadow_fields;
@ -20,6 +21,35 @@ pub struct User {
shell_path: crate::ShellPath, /* Shell program. */
}
impl User {
pub fn username(&mut self, name: String) -> &mut Self {
self.username = crate::Username {
username: name.into(),
};
self
}
pub fn disable_password(&mut self) -> &mut Self {
self.password = crate::Password::Disabled;
self
}
pub fn uid(&mut self, uid: u32) -> &mut Self {
self.uid = crate::Uid { uid };
self
}
pub fn gid(&mut self, gid: u32) -> &mut Self {
self.gid = crate::Gid { gid };
self
}
pub fn home_dir(&mut self, path: String) -> &mut Self {
self.home_dir = crate::HomeDir { dir: path };
self
}
pub fn shell_path(&mut self, path: String) -> &mut Self {
self.shell_path = crate::ShellPath { shell: path };
self
}
}
impl NewFromString for User {
/// Parse a line formatted like one in `/etc/passwd` and construct a matching [`User`] instance
///
@ -132,21 +162,19 @@ impl Default for User {
source: "".to_owned(),
pos: u32::MAX,
username: crate::Username {
username: "defaultuser".to_owned(),
username: "defaultusername".to_owned(),
},
password: crate::Password::Encrypted(crate::EncryptedPassword {
password: "notencrypted".to_owned(),
}),
password: crate::Password::Disabled,
uid: crate::Uid { uid: 1001 },
gid: crate::Gid { gid: 1001 },
gecos: crate::Gecos::Simple {
comment: "gecos default comment".to_string(),
comment: "".to_string(),
},
home_dir: crate::HomeDir {
dir: "/home/default".to_owned(),
dir: "/".to_owned(),
},
shell_path: crate::ShellPath {
shell: "/bin/bash".to_owned(),
shell: "/bin/nologin".to_owned(),
},
}
}
@ -171,10 +199,12 @@ impl Display for User {
#[test]
fn test_default_user() {
// Check if a user can be created.
let pwd = User::default();
let mut pwd = User::default();
assert_eq!(pwd.username.username, "defaultuser");
assert_eq!(pwd.home_dir.dir, "/home/default");
assert_eq!(pwd.uid.uid, 1001);
let npw = pwd.username("test".to_owned()).clone();
assert_eq!(npw.username.username, "test");
}
#[test]