Source file rearrangements

This commit moves file, dir, and the feature modules into one parent 'fs' module. Now there are three main 'areas' of the code: main and options, the filesystem-touching code, and the output-displaying code.

It should be the case that nothing in 'output' touches 'std::fs'.
This commit is contained in:
Benjamin Sago 2016-04-16 18:59:25 +01:00
parent b65043d6d9
commit efa372cb3b
16 changed files with 123 additions and 126 deletions

View File

@ -3,8 +3,8 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::slice::Iter as SliceIter;
use feature::Git;
use file::{File, fields};
use fs::feature::Git;
use fs::{File, fields};
/// A **Dir** provides a cached list of the file paths in a directory that's

View File

@ -2,7 +2,7 @@ use std::path::{Path, PathBuf};
use git2;
use file::fields;
use fs::fields as f;
/// Container of Git statuses for all the files in this folder's Git repository.
@ -29,48 +29,48 @@ impl Git {
}
/// Get the status for the file at the given path, if present.
pub fn status(&self, path: &Path) -> fields::Git {
pub fn status(&self, path: &Path) -> f::Git {
let status = self.statuses.iter()
.find(|p| p.0.as_path() == path);
match status {
Some(&(_, s)) => fields::Git { staged: index_status(s), unstaged: working_tree_status(s) },
None => fields::Git { staged: fields::GitStatus::NotModified, unstaged: fields::GitStatus::NotModified }
Some(&(_, s)) => f::Git { staged: index_status(s), unstaged: working_tree_status(s) },
None => f::Git { staged: f::GitStatus::NotModified, unstaged: f::GitStatus::NotModified }
}
}
/// Get the combined status for all the files whose paths begin with the
/// path that gets passed in. This is used for getting the status of
/// directories, which don't really have an 'official' status.
pub fn dir_status(&self, dir: &Path) -> fields::Git {
pub fn dir_status(&self, dir: &Path) -> f::Git {
let s = self.statuses.iter()
.filter(|p| p.0.starts_with(dir))
.fold(git2::Status::empty(), |a, b| a | b.1);
fields::Git { staged: index_status(s), unstaged: working_tree_status(s) }
f::Git { staged: index_status(s), unstaged: working_tree_status(s) }
}
}
/// The character to display if the file has been modified, but not staged.
fn working_tree_status(status: git2::Status) -> fields::GitStatus {
fn working_tree_status(status: git2::Status) -> f::GitStatus {
match status {
s if s.contains(git2::STATUS_WT_NEW) => fields::GitStatus::New,
s if s.contains(git2::STATUS_WT_MODIFIED) => fields::GitStatus::Modified,
s if s.contains(git2::STATUS_WT_DELETED) => fields::GitStatus::Deleted,
s if s.contains(git2::STATUS_WT_RENAMED) => fields::GitStatus::Renamed,
s if s.contains(git2::STATUS_WT_TYPECHANGE) => fields::GitStatus::TypeChange,
_ => fields::GitStatus::NotModified,
s if s.contains(git2::STATUS_WT_NEW) => f::GitStatus::New,
s if s.contains(git2::STATUS_WT_MODIFIED) => f::GitStatus::Modified,
s if s.contains(git2::STATUS_WT_DELETED) => f::GitStatus::Deleted,
s if s.contains(git2::STATUS_WT_RENAMED) => f::GitStatus::Renamed,
s if s.contains(git2::STATUS_WT_TYPECHANGE) => f::GitStatus::TypeChange,
_ => f::GitStatus::NotModified,
}
}
/// The character to display if the file has been modified, and the change
/// has been staged.
fn index_status(status: git2::Status) -> fields::GitStatus {
fn index_status(status: git2::Status) -> f::GitStatus {
match status {
s if s.contains(git2::STATUS_INDEX_NEW) => fields::GitStatus::New,
s if s.contains(git2::STATUS_INDEX_MODIFIED) => fields::GitStatus::Modified,
s if s.contains(git2::STATUS_INDEX_DELETED) => fields::GitStatus::Deleted,
s if s.contains(git2::STATUS_INDEX_RENAMED) => fields::GitStatus::Renamed,
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => fields::GitStatus::TypeChange,
_ => fields::GitStatus::NotModified,
s if s.contains(git2::STATUS_INDEX_NEW) => f::GitStatus::New,
s if s.contains(git2::STATUS_INDEX_MODIFIED) => f::GitStatus::Modified,
s if s.contains(git2::STATUS_INDEX_DELETED) => f::GitStatus::Deleted,
s if s.contains(git2::STATUS_INDEX_RENAMED) => f::GitStatus::Renamed,
s if s.contains(git2::STATUS_INDEX_TYPECHANGE) => f::GitStatus::TypeChange,
_ => f::GitStatus::NotModified,
}
}

74
src/fs/fields.rs Normal file
View File

@ -0,0 +1,74 @@
#![allow(non_camel_case_types)]
/// Wrapper types for the values returned from `File` objects.
///
/// 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 gid_t = u32;
pub type ino_t = u64;
pub type nlink_t = u64;
pub type time_t = i64;
pub type uid_t = u32;
pub enum Type {
File, Directory, Pipe, Link, Special,
}
pub struct Permissions {
pub file_type: Type,
pub user_read: bool,
pub user_write: bool,
pub user_execute: bool,
pub group_read: bool,
pub group_write: bool,
pub group_execute: bool,
pub other_read: bool,
pub other_write: bool,
pub other_execute: bool,
}
pub struct Links {
pub count: nlink_t,
pub multiple: bool,
}
pub struct Inode(pub ino_t);
pub enum Blocks {
Some(blkcnt_t),
None,
}
pub struct User(pub uid_t);
pub struct Group(pub gid_t);
pub enum Size {
Some(u64),
None,
}
pub struct Time(pub time_t);
pub enum GitStatus {
NotModified,
New,
Modified,
Deleted,
Renamed,
TypeChange,
}
pub struct Git {
pub staged: GitStatus,
pub unstaged: GitStatus,
}
impl Git {
pub fn empty() -> Git {
Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
}
}

View File

@ -7,9 +7,8 @@ use std::io::Result as IOResult;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};
use dir::Dir;
use self::fields as f;
use fs::dir::Dir;
use fs::fields as f;
/// Constant table copied from https://doc.rust-lang.org/src/std/sys/unix/ext/fs.rs.html#11-259
@ -409,83 +408,6 @@ fn ext(path: &Path) -> Option<String> {
}
/// Wrapper types for the values returned from `File` objects.
///
/// 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.
#[allow(non_camel_case_types)]
pub mod fields {
pub type blkcnt_t = u64;
pub type gid_t = u32;
pub type ino_t = u64;
pub type nlink_t = u64;
pub type time_t = i64;
pub type uid_t = u32;
pub enum Type {
File, Directory, Pipe, Link, Special,
}
pub struct Permissions {
pub file_type: Type,
pub user_read: bool,
pub user_write: bool,
pub user_execute: bool,
pub group_read: bool,
pub group_write: bool,
pub group_execute: bool,
pub other_read: bool,
pub other_write: bool,
pub other_execute: bool,
}
pub struct Links {
pub count: nlink_t,
pub multiple: bool,
}
pub struct Inode(pub ino_t);
pub enum Blocks {
Some(blkcnt_t),
None,
}
pub struct User(pub uid_t);
pub struct Group(pub gid_t);
pub enum Size {
Some(u64),
None,
}
pub struct Time(pub time_t);
pub enum GitStatus {
NotModified,
New,
Modified,
Deleted,
Renamed,
TypeChange,
}
pub struct Git {
pub staged: GitStatus,
pub unstaged: GitStatus,
}
impl Git {
pub fn empty() -> Git {
Git { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
}
}
}
#[cfg(test)]
mod test {
use super::ext;

View File

@ -1,4 +1,4 @@
use file::File;
use fs::File;
impl<'_> File<'_> {

9
src/fs/mod.rs Normal file
View File

@ -0,0 +1,9 @@
mod dir;
pub use self::dir::Dir;
mod file;
pub mod filetype;
pub use self::file::File;
pub mod feature;
pub mod fields;

View File

@ -23,14 +23,10 @@ use std::env;
use std::path::{Component, Path};
use std::process;
use dir::Dir;
use file::File;
use fs::{Dir, File};
use options::{Options, View};
mod dir;
mod feature;
mod file;
mod filetype;
mod fs;
mod options;
mod output;
mod term;

View File

@ -7,8 +7,8 @@ use std::os::unix::fs::MetadataExt;
use getopts;
use natord;
use feature::xattr;
use file::File;
use fs::feature::xattr;
use fs::File;
use output::{Grid, Details, GridDetails, Lines};
use output::Colours;
use output::column::{Columns, TimeTypes, SizeFormat};
@ -759,7 +759,7 @@ static EXTENDED_HELP: &'static str = r##" -@, --extended display extended a
#[cfg(test)]
mod test {
use super::{Options, Misfire, SortField, SortCase};
use feature::xattr;
use fs::feature::xattr;
fn is_helpful<T>(misfire: Result<T, Misfire>) -> bool {
match misfire {

View File

@ -1,4 +1,4 @@
use dir::Dir;
use fs::Dir;
#[derive(PartialEq, Debug, Copy, Clone)]

View File

@ -88,10 +88,8 @@ use locale;
use users::{Users, Groups, UsersCache};
use dir::Dir;
use feature::xattr::{Attribute, FileAttributes};
use file::fields as f;
use file::File;
use fs::{Dir, File, fields as f};
use fs::feature::xattr::{Attribute, FileAttributes};
use options::{FileFilter, RecurseOptions};
use output::colours::Colours;
use output::column::{Alignment, Column, Columns, SizeFormat};
@ -224,7 +222,7 @@ impl Details {
use num_cpus;
use scoped_threadpool::Pool;
use std::sync::{Arc, Mutex};
use feature::xattr;
use fs::feature::xattr;
let mut pool = Pool::new(num_cpus::get() as u32);
let mut file_eggs = Vec::new();
@ -761,8 +759,7 @@ pub mod test {
pub use super::{Table, Environment, Details};
pub use std::sync::Mutex;
pub use file::File;
pub use file::fields as f;
pub use fs::{File, fields as f};
pub use output::column::{Column, Columns};
pub use output::cell::TextCell;

View File

@ -1,6 +1,6 @@
use term_grid as grid;
use file::File;
use fs::File;
use output::DisplayWidth;
use output::colours::Colours;
use super::filename;

View File

@ -4,9 +4,8 @@ use ansi_term::ANSIStrings;
use users::UsersCache;
use term_grid as grid;
use dir::Dir;
use feature::xattr::FileAttributes;
use file::File;
use fs::{Dir, File};
use fs::feature::xattr::FileAttributes;
use output::cell::TextCell;
use output::column::Column;

View File

@ -1,6 +1,6 @@
use ansi_term::ANSIStrings;
use file::File;
use fs::File;
use super::filename;
use super::colours::Colours;

View File

@ -1,6 +1,6 @@
use ansi_term::Style;
use file::File;
use fs::File;
pub use self::cell::{TextCell, TextCellContents, DisplayWidth};
pub use self::colours::Colours;