make the structs clonable, add pos and source to the fields

This commit is contained in:
Dietrich 2020-10-23 19:49:21 +02:00
parent d81bac5146
commit 83255390e3
5 changed files with 18 additions and 10 deletions

View File

@ -51,6 +51,8 @@ pub(crate) fn is_groupname_valid(name: &str) -> bool {
/// A record(line) in the user database `/etc/shadow` found in most linux systems.
#[derive(Debug, PartialEq, Eq)]
pub struct Group {
pos: u32,
source: String,
groupname: Groupname, /* Username. */
pub(crate) password: crate::Password, /* Usually not used (disabled with x) */
gid: crate::Gid, /* Group ID. */
@ -118,6 +120,8 @@ impl NewFromString for Group {
let elements: Vec<String> = line.split(':').map(ToString::to_string).collect();
if elements.len() == 4 {
Ok(Self {
pos: position,
source: line.clone(),
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())?,

View File

@ -8,7 +8,7 @@ use std::fmt::{self, Display};
/// The first 4 values are more or less standardised to be full name, room, phone at work and phone at home. After that there can be some extra fields often containing the emailadress and even additional information.
///
/// This enum represents the first 4 values by name and adds the other values to a list of strings [`Gecos::Detail`]. If only one field is found and no `,` at all this value is used as a human readable comment [`Gecos::Simple`].
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Gecos {
Detail {
full_name: String,

View File

@ -7,7 +7,7 @@ use std::convert::TryFrom;
use std::fmt::{self, Display};
/// A record(line) in the user database `/etc/passwd` found in most linux systems.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct User {
source: String,
pos: u32,

View File

@ -20,7 +20,7 @@ use std::fmt::{self, Display};
/// When done the validity will automatically be checked in the `trait TryFrom`.
///
/// In the future some extra fields might be added.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Username {
/// The username value
pub(crate) username: String,
@ -57,7 +57,7 @@ pub(crate) fn is_username_valid(name: &str) -> bool {
USERVALIDATION.is_match(name)
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Password {
Encrypted(crate::EncryptedPassword),
Shadow(crate::Shadow),
@ -74,7 +74,7 @@ impl Display for Password {
}
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct EncryptedPassword {
pub(in crate::user) password: String,
}
@ -97,7 +97,7 @@ impl TryFrom<String> for EncryptedPassword {
}
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Uid {
pub(in crate::user) uid: u32,
}
@ -125,7 +125,7 @@ impl Uid {
}
}
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Gid {
pub(in crate::user) gid: u32,
}
@ -158,7 +158,7 @@ impl Gid {
}
/// The home directory of a user
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct HomeDir {
pub(in crate::user) dir: String,
}
@ -177,7 +177,7 @@ impl TryFrom<String> for HomeDir {
}
/// The path to the Shell binary
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ShellPath {
pub(in crate::user) shell: String,
}

View File

@ -16,8 +16,10 @@ use std::convert::TryFrom;
use std::fmt::{self, Debug, Display};
/// A record(line) in the user database `/etc/shadow` found in most linux systems.
#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Shadow {
pos: u32,
source: String,
username: crate::Username, /* Username. */
pub(crate) password: crate::EncryptedPassword, /* Hashed passphrase */
last_change: Option<chrono::NaiveDateTime>, /* User ID. */
@ -98,6 +100,8 @@ impl NewFromString for Shadow {
if elements.len() == 9 {
let extra = elements.get(8).unwrap();
Ok(Self {
pos: position,
source: line.clone(),
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()),