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
use std::fmt;
use std::io;
use std::path::Path;

#[derive(Debug)]
pub enum TextFileOutputError<'file_handling> {
    CannotCreate(io::Error),
    CannotWrite(io::Error),
    CannotFlush(io::Error),
    AlreadyExists(&'file_handling str),
}

impl<'file_handling> fmt::Display for TextFileOutputError<'file_handling> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            TextFileOutputError::CannotCreate(ref error) => write!(f, "Cannot create error: {}", error),
            TextFileOutputError::CannotWrite(ref error) => write!(f, "Cannot write error: {}", error),
            TextFileOutputError::CannotFlush(ref error) => write!(f, "Cannot flush error: {}", error),
            TextFileOutputError::AlreadyExists(path) => {
                write!(f, "Already exists error: File {} is already exists", path)
            }
        }
    }
}

#[derive(Debug)]
pub enum FileProcessError<'file_process> {
    InvalidPath(InvalidPathError<'file_process>),
    SaveError(TextFileOutputError<'file_process>),
    LoadError(Box<dyn std::error::Error>),
}

impl<'file_process> fmt::Display for FileProcessError<'file_process> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "File process failed")
    }
}

#[derive(Debug)]
pub enum DirectoryProcessError<'file_process> {
    InvalidPath(InvalidPathError<'file_process>),
    SaveError(TextFileOutputError<'file_process>),
    LoadError(Box<dyn std::error::Error>),
    Other,
}

impl<'file_process> fmt::Display for DirectoryProcessError<'file_process> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "File process failed")
    }
}

#[derive(Debug)]
pub enum InvalidPathError<'file_handling> {
    FileNotFound(&'file_handling Path),
}

impl<'file_handling> fmt::Display for InvalidPathError<'file_handling> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            InvalidPathError::FileNotFound(path) => write!(f, "Not exists error: File {} is not found", path.display()),
        }
    }
}