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
#[cfg(feature = "use_failure")]
#[macro_use] extern crate failure_derive;

#[cfg(feature = "use_failure")]
use std::io;
use std::{
    path::Path,
    fs::File,
    io::{
        Read,
        Write,
    }
};

pub trait FromFile : Sized {
    #[cfg(feature = "use_failure")]
    fn from_file(path: &Path) -> Result<Self, Failure> {
        let mut file_handle = match File::open(path) {
            Ok(handle) => handle,
            Err(cause) => return Err(Failure::FileOpenError { cause }),
        };

        let mut file_contents = Vec::new();

        match file_handle.read_to_end(&mut file_contents) {
            Ok(_i) => (),
            Err(cause) => return Err(Failure::FileReadError { cause }),
        }

        match Self::deserialize(&file_contents) {
            Ok(data) => Ok(data),
            Err(cause) => Err(Failure::DeserializationError { cause }),
        }
    }

    #[cfg(not(feature = "use_failure"))]
    fn from_file(path: &Path) -> Result<Self, String> {
        let mut file_handle = match File::open(path) {
            Ok(handle) => handle,
            Err(cause) => return Err(String::from(format!("failed to open file: {}", cause))),
        };

        let mut file_contents = Vec::new();

        match file_handle.read_to_end(&mut file_contents) {
            Ok(_i) => (),
            Err(cause) => return Err(String::from(format!("failed to read file: {}", cause))),
        }

        match Self::deserialize(&file_contents) {
            Ok(data) => Ok(data),
            Err(cause) => Err(String::from(format!("failed to create file: {}", cause))),
        }
    }

    #[cfg(feature = "use_failure")]
    fn deserialize(data: &Vec<u8>) -> Result<Self, failure::Error>;
    #[cfg(not(feature = "use_failure"))]
    fn deserialize(data: &Vec<u8>) -> Result<Self, String>;
}

pub trait ToFile : Sized {
    #[cfg(feature = "use_failure")]
    fn to_file(&self, path: &Path) -> Result<(), Failure> {
        let mut file_handle = match File::create(path){
            Ok(handle) => handle,
            Err(cause) => return Err(Failure::FileOpenError { cause }),
        };

        let mut file_contents = match self.serialize() {
            Ok(bytes) => bytes,
            Err(cause) => return Err(Failure::SerializationError { cause }),
        };

        match file_handle.write_all(&mut file_contents) {
            Ok(()) => (),
            Err(cause) => return Err(Failure::FileWriteError { cause }),
        }

        match file_handle.flush() {
            Ok(()) => Ok(()),
            Err(cause) => Err(Failure::FileFlushError { cause }),
        }
    }

    #[cfg(not(feature = "use_failure"))]
    fn to_file(&self, path: &Path) -> Result<(), String> {
        let mut file_handle = match File::create(path){
            Ok(handle) => handle,
            Err(cause) => return Err(String::from(format!("failed to create file: {}", cause))),
        };

        let mut file_contents = match self.serialize() {
            Ok(bytes) => bytes,
            Err(cause) => return Err(String::from(format!("failed to serialize data: {}", cause))),
        };

        match file_handle.write_all(&mut file_contents) {
            Ok(()) => (),
            Err(cause) => return Err(String::from(format!("failed to serialize data: {}", cause))),
        }

        match file_handle.flush() {
            Ok(()) => Ok(()),
            Err(cause) => Err(String::from(format!("failed to serialize data: {}", cause))),
        }
    }

    #[cfg(feature = "use_failure")]
    fn serialize(&self) -> Result<Vec<u8>, failure::Error>;
    #[cfg(not(feature = "use_failure"))]
    fn serialize(&self) -> Result<Vec<u8>, String>;
}

#[cfg(feature = "use_failure")]
#[derive(Debug, Fail)]
pub enum Failure {
    #[fail(display = "error flushing to file: {}", cause)]
    FileFlushError {
        #[cause] cause: io::Error,
    },
    #[fail(display = "error opening file: {}", cause)]
    FileOpenError {
        #[cause] cause: io::Error,
    },
    #[fail(display = "error reading from file: {}", cause)]
    FileReadError {
        #[cause] cause: io::Error,
    },
    #[fail(display = "error writing to file: {}", cause)]
    FileWriteError {
        #[cause] cause: io::Error,
    },
    #[fail(display = "error deserializing data: {}", cause)]
    DeserializationError {
        #[cause] cause: failure::Error,
    },
    #[fail(display = "error serializing data: {}", cause)]
    SerializationError {
        #[cause] cause: failure::Error,
    },
}