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
use serde;
use serde_json;

use std::fmt;
use std::io;
use std::error::Error;
use std::fs::File;

#[derive(Debug)]
pub enum SerDesError {
    Io(io::Error),
    Json(serde_json::Error),    
}

impl fmt::Display for SerDesError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            SerDesError::Io(ref err) => err.fmt(f),
            SerDesError::Json(ref err) => err.fmt(f),    
        }    
    }    
}

impl Error for SerDesError {
    fn description(&self) -> &str {
        match *self {
            SerDesError::Io(ref err) => err.description(),
            SerDesError::Json(ref err) => err.description(),    
        }    
    }
    fn cause(&self) -> Option<&Error> {
        match *self {
            SerDesError::Io(ref err) => Some(err),
            SerDesError::Json(ref err) => Some(err),    
        }    
    } 
}

impl From<io::Error> for SerDesError {
    fn from(err: io::Error) -> SerDesError {
        SerDesError::Io(err)    
    } 
}

impl From<serde_json::Error> for SerDesError {
    fn from(err: serde_json::Error) -> SerDesError {
        SerDesError::Json(err)    
    }    
}

pub fn serialize<T: serde::Serialize>(obj: &T, path: &str) -> Result<(), SerDesError> {
    let mut file = File::create(path)?;
    Ok(serde_json::to_writer(&mut file, &obj)?)
}

pub fn deserialize<T>(path: &str) -> Result<T, SerDesError> 
    where for<'de> T: serde::Deserialize<'de>
{
    let file = File::open(path)?;
    Ok(serde_json::from_reader(&file)?)    
}