~jpastuszek/file-owner

94fdd509e6ef826ca9d97edef8925ca15d0eda08 — Jakub Pastuszek 1 year, 5 months ago aec5f1e
Display impl for Owner and Group
1 files changed, 29 insertions(+), 0 deletions(-)

M src/lib.rs
M src/lib.rs => src/lib.rs +29 -0
@@ 91,6 91,16 @@ impl<'s> TryFrom<&'s str> for Owner {
    }
}

impl Display for Owner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(name) = self.name().ok().flatten() {
            write!(f, "{}", name)
        } else {
            write!(f, "{}", self.id())
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct Group(Gid);



@@ 126,6 136,16 @@ impl<'s> TryFrom<&'s str> for Group {
    }
}

impl Display for Group {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(name) = self.name().ok().flatten() {
            write!(f, "{}", name)
        } else {
            write!(f, "{}", self.id())
        }
    }
}

pub fn set_owner<E: Into<FileOwnerError>>(path: impl AsRef<Path>, owner: impl TryInto<Owner, Error = E>) -> Result<(), FileOwnerError> {
    Ok(chown(path.as_ref().into(), Some(owner.try_into().map_err(Into::into)?.0), None)?)
}


@@ 188,4 208,13 @@ mod tests {
        assert_eq!(o.id(), 99);
        assert_eq!(g.id(), 99);
    }

    #[test]
    fn test_display() {
        assert_eq!(&Owner::from_uid(99).to_string(), "nobody");
        assert_eq!(&Group::from_gid(99).to_string(), "nogroup");

        assert_eq!(&Owner::from_uid(321321).to_string(), "321321");
        assert_eq!(&Group::from_gid(321321).to_string(), "321321");
    }
}