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
use std::{error, fmt, result, str::Utf8Error, string::FromUtf16Error};
use crate::guid::Guid;
use crate::tree::Kind;
pub type Result<T> = result::Result<T, Error>;
#[derive(Debug)]
pub struct Error(ErrorKind);
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.0
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self.kind() {
ErrorKind::MalformedString(err) => Some(err.as_ref()),
_ => None,
}
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Error {
Error(kind)
}
}
impl From<FromUtf16Error> for Error {
fn from(error: FromUtf16Error) -> Error {
Error(ErrorKind::MalformedString(error.into()))
}
}
impl From<Utf8Error> for Error {
fn from(error: Utf8Error) -> Error {
Error(ErrorKind::MalformedString(error.into()))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind() {
ErrorKind::MismatchedItemKind(local_kind, remote_kind) => write!(
f,
"Can't merge local kind {} and remote kind {}",
local_kind, remote_kind
),
ErrorKind::DuplicateItem(guid) => write!(f, "Item {} already exists in tree", guid),
ErrorKind::MissingItem(guid) => write!(f, "Item {} doesn't exist in tree", guid),
ErrorKind::InvalidParent(child_guid, parent_guid) => write!(
f,
"Can't insert item {} into non-folder {}",
child_guid, parent_guid
),
ErrorKind::MissingParent(child_guid, parent_guid) => write!(
f,
"Can't insert item {} into nonexistent parent {}",
child_guid, parent_guid
),
ErrorKind::Cycle(guid) => write!(f, "Item {} can't contain itself", guid),
ErrorKind::MergeConflict => write!(f, "Local tree changed during merge"),
ErrorKind::UnmergedLocalItems => {
write!(f, "Merged tree doesn't mention all items from local tree")
}
ErrorKind::UnmergedRemoteItems => {
write!(f, "Merged tree doesn't mention all items from remote tree")
}
ErrorKind::InvalidGuid(invalid_guid) => {
write!(f, "Merged tree contains invalid GUID {}", invalid_guid)
}
ErrorKind::InvalidByte(b) => write!(f, "Invalid byte {} in UTF-16 encoding", b),
ErrorKind::MalformedString(err) => err.fmt(f),
ErrorKind::Abort => write!(f, "Operation aborted"),
}
}
}
#[derive(Debug)]
pub enum ErrorKind {
MismatchedItemKind(Kind, Kind),
DuplicateItem(Guid),
InvalidParent(Guid, Guid),
MissingParent(Guid, Guid),
MissingItem(Guid),
Cycle(Guid),
MergeConflict,
UnmergedLocalItems,
UnmergedRemoteItems,
InvalidGuid(Guid),
InvalidByte(u16),
MalformedString(Box<dyn error::Error + Send + Sync + 'static>),
Abort,
}