aboutsummaryrefslogtreecommitdiff
path: root/lockchain-files/src/userstore.rs
blob: 4c4ff437752effcc9fe4cc9cad3095a559dae803 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! Implements serialization, desrialization for UserStore

use lcc::{errors::DataError, traits::AutoEncoder, users::UserStore};

pub trait DiskMirror {
    fn to_disk(&self) -> Vec<u8>;
    fn from_disk(Vec<u8>) -> Result<Box<Self>, DataError>;
}

impl DiskMirror for UserStore {
    fn to_disk(&self) -> Vec<u8> {
        self.encode().unwrap().into_bytes()
    }

    fn from_disk(vec: Vec<u8>) -> Result<Box<Self>, DataError> {
        Self::decode(::std::str::from_utf8(vec.as_slice()).map_err(|_| DataError::FailedDecode)?)
            .map(|s| Box::new(s))
            .map_err(|_| DataError::FailedDecode)
    }
}