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
use std::borrow::Cow;
use std::error;
use std::fmt;
use std::io;
use std::result;
use bson::{self, oid};
use itertools::Itertools;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct PartialSave {
pub cause: Box<Error>,
pub successful_ids: Vec<oid::ObjectId>,
}
impl error::Error for PartialSave {
fn description(&self) -> &str {
"save operation completed partially"
}
fn cause(&self) -> Option<&error::Error> {
Some(&*self.cause)
}
}
struct OidHexDisplay(oid::ObjectId);
impl fmt::Display for OidHexDisplay {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
static CHARS: &'static [u8] = b"0123456789abcdef";
for &byte in &self.0.bytes() {
try!(write!(
f,
"{}{}",
CHARS[(byte >> 4) as usize] as char,
CHARS[(byte & 0xf) as usize] as char
));
}
Ok(())
}
}
impl fmt::Display for PartialSave {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.successful_ids.is_empty() {
write!(f, "saved nothing due to an error: {}", self.cause)
} else {
write!(
f,
"only saved objects with ids: [{}] due to an error: {}",
self.successful_ids
.iter()
.cloned()
.map(OidHexDisplay)
.join(", "),
self.cause
)
}
}
}
quick_error! {
#[derive(Debug)]
pub enum Error {
Io(err: io::Error) {
from()
description("I/O error")
display("I/O error: {}", err)
cause(err)
}
BsonEncoding(err: bson::EncoderError) {
from()
description("BSON encoding error")
display("BSON encoding error: {}", err)
cause(err)
}
BsonDecoding(err: bson::DecoderError) {
from()
description("BSON decoding error")
display("BSON decoding error: {}", err)
cause(err)
}
PartialSave(err: PartialSave) {
from()
description("partial save")
display("partial save: {}", err)
cause(&*err.cause)
}
Other(msg: Cow<'static, str>) {
description(&*msg)
display("{}", msg)
from(s: &'static str) -> (s.into())
from(s: String) -> (s.into())
}
}
}