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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::files::Files;
use crate::utils::is_readable_file;
use crate::{Error, FileNamed, Result, OneFileNamed};
use std::ffi::OsString;
use std::io::Read;
use std::path::PathBuf;

#[derive(Debug)]
pub struct OneFile {
    name: Box<dyn OneFileNamed>,
    directory: PathBuf,
}

impl OneFile {
    pub fn new(name: Box<dyn OneFileNamed>, directory: impl Into<PathBuf>) -> Self {
        Self {
            name,
            directory: directory.into(),
        }
    }

    pub fn name(&self) -> &dyn OneFileNamed {
        self.name.as_ref()
    }

    pub fn find(&self) -> Result<PathBuf> {
        match self.name.name_type() {
            FileNamed::Exact(name) => {
                let file = self.directory.join(name);
                if is_readable_file(&file) {
                    Ok(file)
                } else {
                    Err(Error::new(format!(
                        "File named {} does not exist in {:?}",
                        name, &self.directory
                    ))
                    .boxed())
                }
            }
            FileNamed::Any(names) => {
                let files = names
                    .iter()
                    .map(|each| self.directory.join(each))
                    .filter(|each| is_readable_file(each.as_path()))
                    .collect::<Vec<PathBuf>>();

                match files.len() {
                    0 => Err(Error::new(format!(
                        "Could not find any file out of {:?} in {:?}",
                        names, &self.directory
                    ))
                    .boxed()),
                    1 => Ok(files.first().unwrap().to_owned()),
                    len => Err(Error::new(format!(
                        "Found more than 1 ({}) files out of {:?} in {:?}",
                        len, names, &self.directory
                    ))
                    .boxed()),
                }
            }
            #[cfg(feature = "regex")]
            FileNamed::Regex(regex_pattern) => {
                let files = crate::finders::regex_finder::find_files_in_directory_matching(
                    regex_pattern,
                    &self.directory,
                )?;
                match files.len() {
                    0 => Err(Error::new(format!(
                        "Could not find any file matching {:?} in {:?}",
                        regex_pattern, &self.directory
                    ))
                    .boxed()),
                    1 => Ok(files.first().unwrap().to_owned()),
                    len => Err(Error::new(format!(
                        "Found more than 1 ({}) files matching {:?} in {:?}",
                        len, regex_pattern, &self.directory
                    ))
                    .boxed()),
                }
            }
            #[cfg(feature = "wildmatch")]
            FileNamed::Wildmatch(wildmatch_pattern) => {
                let files = crate::finders::wildmatch_finder::find_files_in_directory_matching(
                    wildmatch_pattern,
                    &self.directory,
                )?;
                match files.len() {
                    0 => Err(Error::new(format!(
                        "Could not find any file matching {:?} in {:?}",
                        wildmatch_pattern, &self.directory
                    ))
                    .boxed()),
                    1 => Ok(files.first().unwrap().to_owned()),
                    len => Err(Error::new(format!(
                        "Found more than 1 ({}) files matching {:?} in {:?}",
                        len, wildmatch_pattern, &self.directory
                    ))
                    .boxed()),
                }
            }
        }
    }

    pub fn as_path_buf(&self) -> Result<PathBuf> {
        self.find()
    }

    pub fn as_os_string(&self) -> Result<OsString> {
        let path = self.find()?;
        match path.file_name() {
            None => Err(Error::new(format!("Could not get the file name of {:?}", &path)).boxed()),
            Some(file_name) => Ok(file_name.to_os_string()),
        }
    }

    pub fn as_string(&self) -> Result<String> {
        let file_name = self.as_os_string()?;
        match file_name.to_str() {
            None => Err(Error::new(format!(
                "Could not convert the file name to Unicode String {:?}",
                &file_name
            ))
            .boxed()),
            Some(file_name) => Ok(file_name.to_owned()),
        }
    }

    pub fn as_bytes(&self) -> Result<Vec<u8>> {
        let path = self.find()?;
        let mut file = std::fs::File::open(path)?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer)?;
        Ok(buffer)
    }
}

impl Clone for OneFile {
    fn clone(&self) -> Self {
        Self::new(self.name.boxed(), self.directory.clone())
    }
}

impl Files for OneFile {
    fn all(&self) -> Result<Vec<PathBuf>> {
        self.find().map(|file| vec![file])
    }

    fn into_files(self) -> Box<dyn Files> {
        Box::new(self)
    }
}

impl From<OneFile> for PathBuf {
    fn from(filter: OneFile) -> Self {
        PathBuf::from(&filter)
    }
}

impl From<&OneFile> for PathBuf {
    fn from(filter: &OneFile) -> Self {
        filter
            .find()
            .expect("Could not find exactly one matching file")
    }
}

impl From<&OneFile> for Result<OsString> {
    fn from(filter: &OneFile) -> Self {
        filter.as_os_string()
    }
}

impl From<OneFile> for Result<OsString> {
    fn from(filter: OneFile) -> Self {
        (&filter).into()
    }
}

impl From<&OneFile> for Result<String> {
    fn from(filter: &OneFile) -> Self {
        filter.as_string()
    }
}

impl From<OneFile> for Result<String> {
    fn from(filter: OneFile) -> Self {
        (&filter).into()
    }
}

impl From<&OneFile> for Result<Vec<u8>> {
    fn from(filter: &OneFile) -> Self {
        filter.as_bytes()
    }
}

impl From<OneFile> for Result<Vec<u8>> {
    fn from(filter: OneFile) -> Self {
        (&filter).into()
    }
}