Просмотр исходного кода

Make DeviceIDs its own type

This is so we can define a render method on it.
Benjamin Sago 8 лет назад
Родитель
Сommit
ddd34f3b1f
3 измененных файлов с 29 добавлено и 24 удалено
  1. 11 8
      src/fs/fields.rs
  2. 2 2
      src/fs/file.rs
  3. 16 14
      src/output/render/size.rs

+ 11 - 8
src/fs/fields.rs

@@ -135,14 +135,17 @@ pub enum Size {
     ///
     /// This is what ls does as well. Without it, the devices will just have
     /// file sizes of zero.
-    ///
-    /// You can see what these device numbers mean:
-    /// - http://www.lanana.org/docs/device-list/
-    /// - http://www.lanana.org/docs/device-list/devices-2.6+.txt
-    DeviceIDs {
-        major: u8,
-        minor: u8,
-    }
+    DeviceIDs(DeviceIDs),
+}
+
+/// The major and minor device IDs that gets displayed for device files.
+///
+/// You can see what these device numbers mean:
+/// - http://www.lanana.org/docs/device-list/
+/// - http://www.lanana.org/docs/device-list/devices-2.6+.txt
+pub struct DeviceIDs {
+    pub major: u8,
+    pub minor: u8,
 }
 
 

+ 2 - 2
src/fs/file.rs

@@ -273,10 +273,10 @@ impl<'dir> File<'dir> {
         }
         else if self.is_char_device() || self.is_block_device() {
             let dev = self.metadata.rdev();
-            f::Size::DeviceIDs {
+            f::Size::DeviceIDs(f::DeviceIDs {
                 major: (dev / 256) as u8,
                 minor: (dev % 256) as u8,
-            }
+            })
         }
         else {
             f::Size::Some(self.metadata.len())

+ 16 - 14
src/output/render/size.rs

@@ -11,9 +11,9 @@ impl f::Size {
         use number_prefix::{Prefixed, Standalone, PrefixNames};
 
         let size = match *self {
-            f::Size::Some(s)                     => s,
-            f::Size::None                        => return TextCell::blank(colours.punctuation),
-            f::Size::DeviceIDs { major, minor }  => return render_device_ids(colours, major, minor),
+            f::Size::Some(s)             => s,
+            f::Size::None                => return TextCell::blank(colours.punctuation),
+            f::Size::DeviceIDs(ref ids)  => return ids.render(colours),
         };
 
         let result = match size_format {
@@ -48,16 +48,18 @@ impl f::Size {
     }
 }
 
-fn render_device_ids(colours: &Colours, major: u8, minor: u8) -> TextCell {
-    let major = major.to_string();
-    let minor = minor.to_string();
-
-    TextCell {
-        width: DisplayWidth::from(major.len() + 1 + minor.len()),
-        contents: vec![
-            colours.size.major.paint(major),
-            colours.punctuation.paint(","),
-            colours.size.minor.paint(minor),
-        ].into(),
+impl f::DeviceIDs {
+    fn render(&self, colours: &Colours) -> TextCell {
+        let major = self.major.to_string();
+        let minor = self.minor.to_string();
+
+        TextCell {
+            width: DisplayWidth::from(major.len() + 1 + minor.len()),
+            contents: vec![
+                colours.size.major.paint(major),
+                colours.punctuation.paint(","),
+                colours.size.minor.paint(minor),
+            ].into(),
+        }
     }
 }