From 9b4956c78a929eee2dd25c496dc4a4caaf8e5604 Mon Sep 17 00:00:00 2001 From: dietrich Date: Fri, 2 Oct 2020 11:55:56 +0200 Subject: [PATCH] Restructure the passwd file --- src/lib.rs | 3 +- src/passwd.rs | 112 +++++++++++++++++++++++++------------------------- 2 files changed, 58 insertions(+), 57 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3ed8e52..4dd27f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; diff --git a/src/passwd.rs b/src/passwd.rs index 540188d..4dd412e 100644 --- a/src/passwd.rs +++ b/src/passwd.rs @@ -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 { + Ok(Self { + uid: source.parse::().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 { + Ok(Self { + gid: source.parse::().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 { + 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 { + 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 { - Ok(Self { - uid: source.parse::().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 { - Ok(Self { - gid: source.parse::().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 { - 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 { - Ok(ShellPath { shell: source }) - } -} - // Tests ---------------------------------------------------------------------- #[test]