From 727822308e6452685ba636c9cf3160b3a839e8d4 Mon Sep 17 00:00:00 2001 From: Ben S Date: Fri, 27 Jun 2014 19:51:04 +0100 Subject: [PATCH] Refactor user/group code to be less indented --- file.rs | 12 +++++++---- unix.rs | 67 ++++++++++++++++++++++++++++++--------------------------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/file.rs b/file.rs index a98b7fe..0add070 100644 --- a/file.rs +++ b/file.rs @@ -116,13 +116,17 @@ impl<'a> File<'a> { // Display the ID if the user/group doesn't exist, which // usually means it was deleted but its files weren't. User => { - let style = if unix.uid == self.stat.unstable.uid as u32 { Yellow.bold() } else { Plain }; - let string = unix.get_user_name(self.stat.unstable.uid as u32).unwrap_or(self.stat.unstable.uid.to_str()); + let uid = self.stat.unstable.uid as u32; + unix.load_user(uid); + let style = if unix.uid == uid { Yellow.bold() } else { Plain }; + let string = unix.get_user_name(uid).unwrap_or(uid.to_str()); style.paint(string.as_slice()) }, Group => { - let name = unix.get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str()); - let style = if unix.is_group_member(self.stat.unstable.gid as u32) { Yellow.normal() } else { Plain }; + let gid = self.stat.unstable.gid as u32; + unix.load_group(gid); + let name = unix.get_group_name(gid).unwrap_or(gid.to_str()); + let style = if unix.is_group_member(gid) { Yellow.normal() } else { Plain }; style.paint(name.as_slice()) }, } diff --git a/unix.rs b/unix.rs index 785adbd..5dd1535 100644 --- a/unix.rs +++ b/unix.rs @@ -73,20 +73,27 @@ impl Unix { } } + pub fn get_user_name(&self, uid: u32) -> Option { + self.user_names.get(&uid).clone() + } + + pub fn get_group_name(&self, gid: u32) -> Option { + self.group_names.get(&gid).clone() + } + pub fn is_group_member(&self, gid: u32) -> bool { *self.groups.get(&gid) } - pub fn get_user_name(&mut self, uid: u32) -> Option { - self.user_names.find_or_insert_with(uid, |&u| { - let pw = unsafe { c::getpwuid(u as i32) }; - if pw.is_not_null() { - return unsafe { Some(from_c_str(read(pw).pw_name)) }; - } - else { - return None; - } - }).clone() + pub fn load_user(&mut self, uid: u32) { + let pw = unsafe { c::getpwuid(uid as i32) }; + if pw.is_not_null() { + let username = unsafe { Some(from_c_str(read(pw).pw_name)) }; + self.user_names.insert(uid, username); + } + else { + self.user_names.insert(uid, None); + } } fn group_membership(group: **i8, uname: &String) -> bool { @@ -113,32 +120,28 @@ impl Unix { } } - pub fn get_group_name(&mut self, gid: u32) -> Option { - match self.group_names.find_copy(&gid) { - Some(name) => name, + pub fn load_group(&mut self, gid: u32) { + match unsafe { c::getgrgid(gid).to_option() } { None => { - match unsafe { c::getgrgid(gid).to_option() } { - None => { - self.group_names.insert(gid, None); - return None; - }, - Some(r) => { - let group_name = unsafe { Some(from_c_str(r.gr_name)) }; - self.group_names.insert(gid, group_name.clone()); - - // Calculate whether we are a member of the - // group. Now's as good a time as any as we've - // just retrieved the group details. - - if !self.groups.contains_key(&gid) { - self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username)); - } - - return group_name; - } + self.group_names.insert(gid, None); + self.groups.insert(gid, false); + }, + Some(r) => { + let group_name = unsafe { Some(from_c_str(r.gr_name)) }; + self.group_names.insert(gid, group_name.clone()); + + // Calculate whether we are a member of the + // group. Now's as good a time as any as we've + // just retrieved the group details. + + if !self.groups.contains_key(&gid) { + self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username)); } + + self.group_names.insert(gid, group_name); } } + } } -- GitLab