fsort/criterion/
error.rs

1use std::fmt::{Display, Formatter, Result as FmtResult};
2use std::error::Error as ErrorTrait;
3use std::path::Path;
4use std::fs::Metadata;
5
6#[derive(Debug,Clone,Copy)]
7/// An error occurring during the creation of a criterion.
8pub enum Error {
9    /// It was not possible to access file behind the path.
10    NoAccess,
11    /// The path is not a file but a directory, device or other unsupported handle.
12    InvalidPath,
13    /// The requested search criterion is not supported on this OS.
14    CriterionUnsupported
15}
16
17impl Display for Error {
18    fn fmt(&self, f: &mut Formatter) -> FmtResult {
19        write!(f, "{}", self.description())
20    }
21}
22
23impl ErrorTrait for Error {
24    fn description(&self) -> &str {
25        match *self {
26            Error::NoAccess => "Missing access privileges",
27            Error::InvalidPath => "Path is not a file",
28            Error::CriterionUnsupported => "Sort criterion unsupported by this OS"
29        }
30    }
31}
32
33/// Reads metadata from a file.
34pub fn try_metadata<P : AsRef<Path>>(path : P) -> Result<Metadata, Error> {
35    if let Ok(meta) = path.as_ref().metadata() {
36        if !meta.is_file() {
37            Err(Error::InvalidPath)
38        } else {
39            Ok(meta)
40        }
41    }
42        else {
43        Err(Error::NoAccess)
44    }
45}
46