use std::path::Path;
use std::num::ParseIntError;
use libc::gid_t;
use entries::{Entries,Entry};
#[derive(Debug, PartialEq, PartialOrd)]
pub struct GroupEntry {
pub name: String,
pub passwd: String,
pub gid: gid_t,
pub members: Vec<String>,
}
impl Entry for GroupEntry {
fn from_line(line: &str) -> Result<GroupEntry, ParseIntError> {
let parts: Vec<&str> = line.split(":").map(|part| part.trim()).collect();
let members: Vec<String> = match parts[3].len() {
0 => Vec::new(),
_ => parts[3]
.split(",")
.map(|member| member.trim())
.map(|member| member.to_string())
.collect()
};
Ok(GroupEntry {
name: parts[0].to_string(),
passwd: parts[1].to_string(),
gid: try!(parts[2].parse()),
members: members,
})
}
}
pub fn get_entry_by_gid_from_path(path: &Path, gid: gid_t) -> Option<GroupEntry> {
Entries::<GroupEntry>::new(path).find(|x| x.gid == gid)
}
pub fn get_entry_by_gid(gid: gid_t) -> Option<GroupEntry> {
get_entry_by_gid_from_path(&Path::new("/etc/group"), gid)
}
pub fn get_entry_by_name_from_path(path: &Path, name: &str) -> Option<GroupEntry> {
Entries::<GroupEntry>::new(path).find(|x| x.name == name)
}
pub fn get_entry_by_name(name: &str) -> Option<GroupEntry> {
get_entry_by_name_from_path(&Path::new("/etc/group"), name)
}
pub fn get_all_entries_from_path(path: &Path) -> Vec<GroupEntry> {
Entries::new(path).collect()
}
pub fn get_all_entries() -> Vec<GroupEntry> {
get_all_entries_from_path(&Path::new("/etc/group"))
}