Add comments to the new fields module

This commit is contained in:
Benjamin Sago 2016-04-16 19:56:44 +01:00
parent efa372cb3b
commit 570fac0c18

View File

@ -1,73 +1,169 @@
//! Wrapper types for the values returned from `File`s.
//!
//! The methods of `File` that return information about the entry on the
//! filesystem -- size, modification date, block count, or Git status -- used
//! to just return these as formatted strings, but this became inflexible once
//! customisable output styles landed.
//!
//! Instead, they will return a wrapper type from this module, which tags the
//! type with what field it is while containing the actual raw value.
//!
//! The `output::details` module, among others, uses these types to render and
//! display the information as formatted strings.
// C-style `blkcnt_t` types dont follow Rusts rules!
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
/// Wrapper types for the values returned from `File` objects.
/// /// The type of a files block count.
/// The methods of `File` don't return formatted strings; neither do they
/// return raw numbers representing timestamps or user IDs. Instead, they will
/// return an object in this `fields` module. These objects are later rendered
/// into formatted strings in the `output/details` module.
pub type blkcnt_t = u64; pub type blkcnt_t = u64;
/// The type of a files group ID.
pub type gid_t = u32; pub type gid_t = u32;
/// The type of a files inode.
pub type ino_t = u64; pub type ino_t = u64;
/// The type of a files number of links.
pub type nlink_t = u64; pub type nlink_t = u64;
/// The type of a files timestamp (creation, modification, access, etc).
pub type time_t = i64; pub type time_t = i64;
/// The type of a files user ID.
pub type uid_t = u32; pub type uid_t = u32;
/// The files base type, which gets displayed in the very first column of the
/// details output.
///
/// This type is set entirely by the filesystem, rather than relying on a
/// files contents. So “link” is a type, but “image” is just a type of
/// regular file. (See the `filetype` module for those checks.)
pub enum Type { pub enum Type {
File, Directory, Pipe, Link, Special, File, Directory, Pipe, Link, Special,
} }
/// The files Unix permission bitfield, with one entry per bit.
pub struct Permissions { pub struct Permissions {
pub file_type: Type, pub file_type: Type,
pub user_read: bool, pub user_read: bool,
pub user_write: bool, pub user_write: bool,
pub user_execute: bool, pub user_execute: bool,
pub group_read: bool, pub group_read: bool,
pub group_write: bool, pub group_write: bool,
pub group_execute: bool, pub group_execute: bool,
pub other_read: bool, pub other_read: bool,
pub other_write: bool, pub other_write: bool,
pub other_execute: bool, pub other_execute: bool,
} }
/// A files number of hard links on the filesystem.
///
/// Under Unix, a file can exist on the filesystem only once but appear in
/// multiple directories. However, its rare (but occasionally useful!) for a
/// regular file to have a link count greater than 1, so we highlight the
/// block count specifically for this case.
pub struct Links { pub struct Links {
/// The actual link count.
pub count: nlink_t, pub count: nlink_t,
/// Whether this file is a regular file with more than one hard link.
pub multiple: bool, pub multiple: bool,
} }
/// A files inode. Every directory entry on a Unix filesystem has an inode,
/// including directories and links, so this is applicable to everything exa
/// can deal with.
pub struct Inode(pub ino_t); pub struct Inode(pub ino_t);
/// The number of blocks that a file takes up on the filesystem, if any.
pub enum Blocks { pub enum Blocks {
/// This file has the given number of blocks.
Some(blkcnt_t), Some(blkcnt_t),
/// This file isnt of a type that can take up blocks.
None, None,
} }
/// The ID of the user that owns a file. This will only ever be a number;
/// looking up the username is done in the `display` module.
pub struct User(pub uid_t); pub struct User(pub uid_t);
/// The ID of the group that a file belongs to.
pub struct Group(pub gid_t); pub struct Group(pub gid_t);
/// A files size, in bytes. This is usually formatted by the `number_prefix`
/// crate into something human-readable.
pub enum Size { pub enum Size {
/// This file has a defined size.
Some(u64), Some(u64),
/// This file has no size, or has a size but we arent interested in it.
///
/// Under Unix, directory entries that arent regular files will still
/// have a file size. For example, a directory will just contain a list of
/// its files as its “contents” and will be specially flagged as being a
/// directory, rather than a file. However, seeing the “file size” of this
/// data is rarely useful -- I cant think of a time when Ive seen it and
/// learnt something. So we discard it and just output “-” instead.
///
/// See this answer for more: http://unix.stackexchange.com/a/68266
None, None,
} }
/// One of a files timestamps (created, accessed, or modified).
pub struct Time(pub time_t); pub struct Time(pub time_t);
/// A files status in a Git repository. Whether a file is in a repository or
/// not is handled by the Git module, rather than having a “null” variant in
/// this enum.
pub enum GitStatus { pub enum GitStatus {
/// This file hasnt changed since the last commit.
NotModified, NotModified,
/// This file didnt exist for the last commit, and is not specified in
/// the ignored files list.
New, New,
/// A file thats been modified since the last commit.
Modified, Modified,
/// A deleted file. This cant ever be shown, but its here anyway!
Deleted, Deleted,
/// A file that Git has tracked a rename for.
Renamed, Renamed,
/// A file thats had its type (such as the file permissions) changed.
TypeChange, TypeChange,
} }
/// A files complete Git status. Its possible to make changes to a file, add
/// it to the staging area, then make *more* changes, so we need to list each
/// files status for both of these.
pub struct Git { pub struct Git {
pub staged: GitStatus, pub staged: GitStatus,
pub unstaged: GitStatus, pub unstaged: GitStatus,
} }
impl Git { impl Git {
/// Create a Git status for a file with nothing done to it.
pub fn empty() -> Git { pub fn empty() -> Git {
Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified } Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
} }