2016-10-30 14:43:33 +00:00
|
|
|
|
use glob;
|
2016-04-17 19:38:37 +00:00
|
|
|
|
|
2017-06-27 00:13:50 +00:00
|
|
|
|
use fs::DotFilter;
|
2017-07-24 07:34:50 +00:00
|
|
|
|
use fs::filter::{FileFilter, SortField, SortCase, IgnorePatterns};
|
2017-07-26 16:48:18 +00:00
|
|
|
|
|
|
|
|
|
use options::{flags, Misfire};
|
|
|
|
|
use options::parser::Matches;
|
2016-04-17 19:38:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl FileFilter {
|
|
|
|
|
|
|
|
|
|
/// Determines the set of file filter options to use, based on the user’s
|
|
|
|
|
/// command-line arguments.
|
2017-07-26 16:48:18 +00:00
|
|
|
|
pub fn deduce(matches: &Matches) -> Result<FileFilter, Misfire> {
|
2016-04-17 19:38:37 +00:00
|
|
|
|
Ok(FileFilter {
|
2017-07-26 16:48:18 +00:00
|
|
|
|
list_dirs_first: matches.has(&flags::DIRS_FIRST),
|
|
|
|
|
reverse: matches.has(&flags::REVERSE),
|
2017-03-26 16:35:50 +00:00
|
|
|
|
sort_field: SortField::deduce(matches)?,
|
2017-06-29 11:07:46 +00:00
|
|
|
|
dot_filter: DotFilter::deduce(matches)?,
|
2017-03-26 16:35:50 +00:00
|
|
|
|
ignore_patterns: IgnorePatterns::deduce(matches)?,
|
2016-04-17 19:38:37 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for SortField {
|
|
|
|
|
fn default() -> SortField {
|
|
|
|
|
SortField::Name(SortCase::Sensitive)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SortField {
|
|
|
|
|
|
|
|
|
|
/// Determine the sort field to use, based on the presence of a “sort”
|
|
|
|
|
/// argument. This will return `Err` if the option is there, but does not
|
|
|
|
|
/// correspond to a valid field.
|
2017-07-26 16:48:18 +00:00
|
|
|
|
fn deduce(matches: &Matches) -> Result<SortField, Misfire> {
|
2017-06-23 21:58:07 +00:00
|
|
|
|
|
|
|
|
|
const SORTS: &[&str] = &[ "name", "Name", "size", "extension",
|
|
|
|
|
"Extension", "modified", "accessed",
|
2017-06-29 13:50:39 +00:00
|
|
|
|
"created", "inode", "type", "none" ];
|
2017-06-23 21:58:07 +00:00
|
|
|
|
|
2017-07-26 16:48:18 +00:00
|
|
|
|
let word = match matches.get(&flags::SORT) {
|
|
|
|
|
Some(w) => w,
|
|
|
|
|
None => return Ok(SortField::default()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if word == "name" || word == "filename" {
|
|
|
|
|
Ok(SortField::Name(SortCase::Sensitive))
|
|
|
|
|
}
|
|
|
|
|
else if word == "Name" || word == "Filename" {
|
|
|
|
|
Ok(SortField::Name(SortCase::Insensitive))
|
|
|
|
|
}
|
|
|
|
|
else if word == "size" || word == "filesize" {
|
|
|
|
|
Ok(SortField::Size)
|
|
|
|
|
}
|
|
|
|
|
else if word == "ext" || word == "extension" {
|
|
|
|
|
Ok(SortField::Extension(SortCase::Sensitive))
|
|
|
|
|
}
|
|
|
|
|
else if word == "Ext" || word == "Extension" {
|
|
|
|
|
Ok(SortField::Extension(SortCase::Insensitive))
|
|
|
|
|
}
|
|
|
|
|
else if word == "mod" || word == "modified" {
|
|
|
|
|
Ok(SortField::ModifiedDate)
|
|
|
|
|
}
|
|
|
|
|
else if word == "acc" || word == "accessed" {
|
|
|
|
|
Ok(SortField::AccessedDate)
|
|
|
|
|
}
|
|
|
|
|
else if word == "cr" || word == "created" {
|
|
|
|
|
Ok(SortField::CreatedDate)
|
|
|
|
|
}
|
|
|
|
|
else if word == "inode" {
|
|
|
|
|
Ok(SortField::FileInode)
|
|
|
|
|
}
|
|
|
|
|
else if word == "type" {
|
|
|
|
|
Ok(SortField::FileType)
|
|
|
|
|
}
|
|
|
|
|
else if word == "none" {
|
|
|
|
|
Ok(SortField::Unsorted)
|
2016-04-17 19:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2017-07-26 16:48:18 +00:00
|
|
|
|
Err(Misfire::bad_argument(&flags::SORT, word, SORTS))
|
2016-04-17 19:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-30 14:43:33 +00:00
|
|
|
|
|
|
|
|
|
|
2017-06-26 22:28:10 +00:00
|
|
|
|
impl DotFilter {
|
2017-07-26 16:48:18 +00:00
|
|
|
|
pub fn deduce(matches: &Matches) -> Result<DotFilter, Misfire> {
|
|
|
|
|
let dots = match matches.count(&flags::ALL) {
|
2017-06-29 11:07:46 +00:00
|
|
|
|
0 => return Ok(DotFilter::JustFiles),
|
2017-06-27 00:13:50 +00:00
|
|
|
|
1 => DotFilter::Dotfiles,
|
|
|
|
|
_ => DotFilter::DotfilesAndDots,
|
2017-06-29 11:07:46 +00:00
|
|
|
|
};
|
|
|
|
|
|
2017-07-26 16:48:18 +00:00
|
|
|
|
if matches.has(&flags::TREE) {
|
|
|
|
|
Err(Misfire::TreeAllAll)
|
2017-06-29 11:07:46 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Ok(dots)
|
2017-06-26 22:48:55 +00:00
|
|
|
|
}
|
2017-06-26 22:28:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-10-30 14:43:33 +00:00
|
|
|
|
impl IgnorePatterns {
|
2017-07-24 07:34:50 +00:00
|
|
|
|
|
2016-10-30 14:43:33 +00:00
|
|
|
|
/// Determines the set of file filter options to use, based on the user’s
|
|
|
|
|
/// command-line arguments.
|
2017-07-26 16:48:18 +00:00
|
|
|
|
pub fn deduce(matches: &Matches) -> Result<IgnorePatterns, Misfire> {
|
|
|
|
|
let patterns = match matches.get(&flags::IGNORE_GLOB) {
|
2016-10-30 14:43:33 +00:00
|
|
|
|
None => Ok(Vec::new()),
|
2017-07-26 16:48:18 +00:00
|
|
|
|
Some(is) => is.to_string_lossy().split('|').map(|a| glob::Pattern::new(a)).collect(),
|
|
|
|
|
}?;
|
2016-10-30 14:43:33 +00:00
|
|
|
|
|
2017-07-26 16:48:18 +00:00
|
|
|
|
// TODO: is to_string_lossy really the best way to handle
|
|
|
|
|
// invalid UTF-8 there?
|
|
|
|
|
|
|
|
|
|
Ok(IgnorePatterns { patterns })
|
2016-10-30 14:43:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|