fields.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //! Wrapper types for the values returned from `File`s.
  2. //!
  3. //! The methods of `File` that return information about the entry on the
  4. //! filesystem -- size, modification date, block count, or Git status -- used
  5. //! to just return these as formatted strings, but this became inflexible once
  6. //! customisable output styles landed.
  7. //!
  8. //! Instead, they will return a wrapper type from this module, which tags the
  9. //! type with what field it is while containing the actual raw value.
  10. //!
  11. //! The `output::details` module, among others, uses these types to render and
  12. //! display the information as formatted strings.
  13. // C-style `blkcnt_t` types don’t follow Rust’s rules!
  14. #![allow(non_camel_case_types)]
  15. /// The type of a file’s block count.
  16. pub type blkcnt_t = u64;
  17. /// The type of a file’s group ID.
  18. pub type gid_t = u32;
  19. /// The type of a file’s inode.
  20. pub type ino_t = u64;
  21. /// The type of a file’s number of links.
  22. pub type nlink_t = u64;
  23. /// The type of a file’s timestamp (creation, modification, access, etc).
  24. pub type time_t = i64;
  25. /// The type of a file’s user ID.
  26. pub type uid_t = u32;
  27. /// The file’s base type, which gets displayed in the very first column of the
  28. /// details output.
  29. ///
  30. /// This type is set entirely by the filesystem, rather than relying on a
  31. /// file’s contents. So “link” is a type, but “image” is just a type of
  32. /// regular file. (See the `filetype` module for those checks.)
  33. pub enum Type {
  34. File, Directory, Pipe, Link, Socket, CharDevice, BlockDevice, Special,
  35. }
  36. impl Type {
  37. pub fn is_regular_file(&self) -> bool {
  38. match *self {
  39. Type::File => true,
  40. _ => false,
  41. }
  42. }
  43. }
  44. /// The file’s Unix permission bitfield, with one entry per bit.
  45. pub struct Permissions {
  46. pub user_read: bool,
  47. pub user_write: bool,
  48. pub user_execute: bool,
  49. pub group_read: bool,
  50. pub group_write: bool,
  51. pub group_execute: bool,
  52. pub other_read: bool,
  53. pub other_write: bool,
  54. pub other_execute: bool,
  55. }
  56. /// A file’s number of hard links on the filesystem.
  57. ///
  58. /// Under Unix, a file can exist on the filesystem only once but appear in
  59. /// multiple directories. However, it’s rare (but occasionally useful!) for a
  60. /// regular file to have a link count greater than 1, so we highlight the
  61. /// block count specifically for this case.
  62. pub struct Links {
  63. /// The actual link count.
  64. pub count: nlink_t,
  65. /// Whether this file is a regular file with more than one hard link.
  66. pub multiple: bool,
  67. }
  68. /// A file’s inode. Every directory entry on a Unix filesystem has an inode,
  69. /// including directories and links, so this is applicable to everything exa
  70. /// can deal with.
  71. pub struct Inode(pub ino_t);
  72. /// The number of blocks that a file takes up on the filesystem, if any.
  73. pub enum Blocks {
  74. /// This file has the given number of blocks.
  75. Some(blkcnt_t),
  76. /// This file isn’t of a type that can take up blocks.
  77. None,
  78. }
  79. /// The ID of the user that owns a file. This will only ever be a number;
  80. /// looking up the username is done in the `display` module.
  81. pub struct User(pub uid_t);
  82. /// The ID of the group that a file belongs to.
  83. pub struct Group(pub gid_t);
  84. /// A file’s size, in bytes. This is usually formatted by the `number_prefix`
  85. /// crate into something human-readable.
  86. pub enum Size {
  87. /// This file has a defined size.
  88. Some(u64),
  89. /// This file has no size, or has a size but we aren’t interested in it.
  90. ///
  91. /// Under Unix, directory entries that aren’t regular files will still
  92. /// have a file size. For example, a directory will just contain a list of
  93. /// its files as its “contents” and will be specially flagged as being a
  94. /// directory, rather than a file. However, seeing the “file size” of this
  95. /// data is rarely useful -- I can’t think of a time when I’ve seen it and
  96. /// learnt something. So we discard it and just output “-” instead.
  97. ///
  98. /// See this answer for more: http://unix.stackexchange.com/a/68266
  99. None,
  100. /// This file is a block or character device, so instead of a size, print
  101. /// out the file’s major and minor device IDs.
  102. ///
  103. /// This is what ls does as well. Without it, the devices will just have
  104. /// file sizes of zero.
  105. DeviceIDs {
  106. major: u8,
  107. minor: u8,
  108. }
  109. }
  110. /// One of a file’s timestamps (created, accessed, or modified).
  111. pub struct Time(pub time_t);
  112. /// A file’s status in a Git repository. Whether a file is in a repository or
  113. /// not is handled by the Git module, rather than having a “null” variant in
  114. /// this enum.
  115. pub enum GitStatus {
  116. /// This file hasn’t changed since the last commit.
  117. NotModified,
  118. /// This file didn’t exist for the last commit, and is not specified in
  119. /// the ignored files list.
  120. New,
  121. /// A file that’s been modified since the last commit.
  122. Modified,
  123. /// A deleted file. This can’t ever be shown, but it’s here anyway!
  124. Deleted,
  125. /// A file that Git has tracked a rename for.
  126. Renamed,
  127. /// A file that’s had its type (such as the file permissions) changed.
  128. TypeChange,
  129. }
  130. /// A file’s complete Git status. It’s possible to make changes to a file, add
  131. /// it to the staging area, then make *more* changes, so we need to list each
  132. /// file’s status for both of these.
  133. pub struct Git {
  134. pub staged: GitStatus,
  135. pub unstaged: GitStatus,
  136. }
  137. impl Git {
  138. /// Create a Git status for a file with nothing done to it.
  139. pub fn empty() -> Git {
  140. Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
  141. }
  142. }