1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#[cfg(feature = "regex")]
extern crate regex;

#[cfg(feature = "wildmatch")]
extern crate wildmatch;

mod actions;
mod error;
mod files;
pub(crate) mod finders;
mod utils;
mod alias;

use std::path::PathBuf;
use std::fmt::Debug;

pub use actions::*;

pub use error::{BoxError, Error, Result};
pub use files::{Files, MultipleFiles, OneFile};
pub use alias::FileAlias;

/// Defines a file with various name types. It is required that exactly one file with a given name description exists.
#[derive(Debug, Clone)]
pub enum FileNamed {
    Exact(String),
    Any(Vec<String>),
    #[cfg(feature = "regex")]
    Regex(String),
    #[cfg(feature = "wildmatch")]
    Wildmatch(String),
}

#[derive(Debug, Clone)]
pub enum FilesNamed {
    Exact(String),
    Any(Vec<String>),
    #[cfg(feature = "regex")]
    Regex(String),
    #[cfg(feature = "wildmatch")]
    Wildmatch(String),
}

pub trait OneFileNamed: Debug {
    fn within_path_buf(&self, directory: PathBuf) -> OneFile;
    fn name_type(&self) -> &FileNamed;
    fn name_alias(&self) -> Option<&str>;
    fn boxed(&self) -> Box<dyn OneFileNamed>;
}

impl OneFileNamed for FileNamed {
    fn within_path_buf(&self, directory: PathBuf) -> OneFile {
        OneFile::new(self.boxed(), directory)
    }

    fn name_type(&self) -> &FileNamed {
        self
    }

    fn name_alias(&self) -> Option<&str> {
        None
    }

    fn boxed(&self) -> Box<dyn OneFileNamed> {
        Box::new(self.clone())
    }
}

impl FileNamed {
    pub fn exact(name: impl Into<String>) -> Self {
        Self::Exact(name.into())
    }

    pub fn any(names: Vec<impl Into<String>>) -> Self {
        Self::Any(names.into_iter().map(|name| name.into()).collect())
    }

    #[cfg(feature = "regex")]
    pub fn regex(pattern: impl Into<String>) -> Self {
        Self::Regex(pattern.into())
    }

    #[cfg(feature = "wildmatch")]
    pub fn wildmatch(pattern: impl Into<String>) -> Self {
        Self::Wildmatch(pattern.into())
    }

    pub fn within(&self, directory: impl Into<PathBuf>) -> OneFile {
        self.within_path_buf(directory.into())
    }

    pub fn alias(&self, name: impl Into<String>) -> FileAlias {
        FileAlias::new(self.clone(), name)
    }
}

impl FilesNamed {
    pub fn within(&self, directory: impl Into<PathBuf>) -> MultipleFiles {
        MultipleFiles::new(self.clone(), directory)
    }

    pub fn exact(name: impl Into<String>) -> Self {
        Self::Exact(name.into())
    }

    pub fn any(names: Vec<impl Into<String>>) -> Self {
        Self::Any(names.into_iter().map(|name| name.into()).collect())
    }

    #[cfg(feature = "regex")]
    pub fn regex(pattern: impl Into<String>) -> Self {
        Self::Regex(pattern.into())
    }

    #[cfg(feature = "wildmatch")]
    pub fn wildmatch(pattern: impl Into<String>) -> Self {
        Self::Wildmatch(pattern.into())
    }
}