Restructure the passwd file

This commit is contained in:
dietrich 2020-10-02 11:55:56 +02:00
parent 24db3e2579
commit 9b4956c78a
2 changed files with 58 additions and 57 deletions

View File

@ -1,7 +1,8 @@
#[macro_use]
extern crate lazy_static;
extern crate log;
pub mod passwd;
pub mod userlib_error;
pub use passwd::{Password, Username};
pub use passwd::{Gecos, Gid, HomeDir, Password, ShellPath, Uid, Username};

View File

@ -81,11 +81,41 @@ pub struct Uid {
uid: u32,
}
impl Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.uid,)
}
}
impl TryFrom<&str> for Uid {
type Error = UserLibError;
fn try_from(source: &str) -> std::result::Result<Self, Self::Error> {
Ok(Self {
uid: source.parse::<u32>().unwrap(),
})
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Gid {
gid: u32,
}
impl Display for Gid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.gid,)
}
}
impl TryFrom<&str> for Gid {
type Error = UserLibError;
fn try_from(source: &str) -> std::result::Result<Self, Self::Error> {
Ok(Self {
gid: source.parse::<u32>().unwrap(),
})
}
}
/// The gecos field of a user.
///
/// In the `/etc/passwd` file this field is a `,` sepparated list of items.
@ -235,12 +265,38 @@ pub struct HomeDir<'a> {
dir: &'a str,
}
impl Display for HomeDir<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.dir,)
}
}
impl<'a> TryFrom<&'a str> for HomeDir<'a> {
type Error = UserLibError;
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
Ok(Self { dir: source })
}
}
/// The path to the Shell binary
#[derive(Debug, PartialEq, Eq)]
pub struct ShellPath<'a> {
shell: &'a str,
}
impl Display for ShellPath<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.shell,)
}
}
impl<'a> TryFrom<&'a str> for ShellPath<'a> {
type Error = UserLibError;
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
Ok(ShellPath { shell: source })
}
}
/// A record(line) in the user database `/etc/passwd` found in most linux systems.
#[derive(Debug, PartialEq, Eq)]
pub struct Passwd<'a> {
@ -350,62 +406,6 @@ impl Display for Passwd<'_> {
}
}
impl Display for Uid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.uid,)
}
}
impl TryFrom<&str> for Uid {
type Error = UserLibError;
fn try_from(source: &str) -> std::result::Result<Self, Self::Error> {
Ok(Self {
uid: source.parse::<u32>().unwrap(),
})
}
}
impl Display for Gid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.gid,)
}
}
impl TryFrom<&str> for Gid {
type Error = UserLibError;
fn try_from(source: &str) -> std::result::Result<Self, Self::Error> {
Ok(Self {
gid: source.parse::<u32>().unwrap(),
})
}
}
impl Display for HomeDir<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.dir,)
}
}
impl<'a> TryFrom<&'a str> for HomeDir<'a> {
type Error = UserLibError;
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
Ok(Self { dir: source })
}
}
impl Display for ShellPath<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.shell,)
}
}
impl<'a> TryFrom<&'a str> for ShellPath<'a> {
type Error = UserLibError;
fn try_from(source: &'a str) -> std::result::Result<Self, Self::Error> {
Ok(ShellPath { shell: source })
}
}
// Tests ----------------------------------------------------------------------
#[test]