Przeglądaj źródła

Refactor user/group code to be less indented

Ben S 11 lat temu
rodzic
commit
727822308e
2 zmienionych plików z 43 dodań i 36 usunięć
  1. 8 4
      file.rs
  2. 35 32
      unix.rs

+ 8 - 4
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())
             },
         }

+ 35 - 32
unix.rs

@@ -73,20 +73,27 @@ impl Unix {
         }
     }
 
+    pub fn get_user_name(&self, uid: u32) -> Option<String> {
+        self.user_names.get(&uid).clone()
+    }
+
+    pub fn get_group_name(&self, gid: u32) -> Option<String> {
+        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<String> {
-        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<String> {
-        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);
             }
         }
+        
     }
 }