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
use super::{Client, CoreError, FutureExt};
use futures::{self, Future};
use routing::{ImmutableData, XOR_NAME_LEN, XorName};
use self_encryption::{Storage, StorageError};
use std::error::Error;
use std::fmt::{self, Display, Formatter};
pub struct SelfEncryptionStorage<T> {
client: Client<T>,
}
impl<T> SelfEncryptionStorage<T> {
pub fn new(client: Client<T>) -> Self {
SelfEncryptionStorage { client: client }
}
}
impl<T: 'static> Storage for SelfEncryptionStorage<T> {
type Error = SelfEncryptionStorageError;
fn get(&self, name: &[u8]) -> Box<Future<Item = Vec<u8>, Error = Self::Error>> {
trace!("Self encrypt invoked GetIData.");
if name.len() != XOR_NAME_LEN {
let err = CoreError::Unexpected("Requested `name` is incorrect size.".to_owned());
let err = SelfEncryptionStorageError::from(err);
return Box::new(futures::failed(err));
}
let name = {
let mut temp = [0u8; XOR_NAME_LEN];
temp.clone_from_slice(name);
XorName(temp)
};
self.client
.get_idata(name)
.map(|data| data.value().clone())
.map_err(From::from)
.into_box()
}
fn put(&mut self, _: Vec<u8>, data: Vec<u8>) -> Box<Future<Item = (), Error = Self::Error>> {
trace!("Self encrypt invoked PutIData.");
let data = ImmutableData::new(data);
self.client.put_idata(data).map_err(From::from).into_box()
}
}
#[derive(Debug)]
pub struct SelfEncryptionStorageError(pub Box<CoreError>);
impl Display for SelfEncryptionStorageError {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
Display::fmt(&*self.0, formatter)
}
}
impl Error for SelfEncryptionStorageError {
fn description(&self) -> &str {
self.0.description()
}
fn cause(&self) -> Option<&Error> {
self.0.cause()
}
}
impl From<CoreError> for SelfEncryptionStorageError {
fn from(error: CoreError) -> SelfEncryptionStorageError {
SelfEncryptionStorageError(Box::new(error))
}
}
impl StorageError for SelfEncryptionStorageError {}