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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use crate::ffi::{
c4error_getDescription, c4error_getMessage, C4Error, C4ErrorDomain, FLSliceResult,
};
use std::{fmt, os::raw::c_int};
pub enum Error {
C4Error(C4Error),
InvalidUtf8,
LogicError(String),
SerdeFleece(serde_fleece::Error),
NulError(std::ffi::NulError),
InvalidQuery {
pos: c_int,
query_expr: String,
err: C4Error,
},
}
impl std::error::Error for Error {}
pub(crate) type Result<T> = std::result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::C4Error(err) => {
let (msg, desc) = into_msg_desc(*err);
write!(
fmt,
"c4 error {}: {}",
desc.as_utf8_lossy(),
msg.as_utf8_lossy()
)
}
Error::InvalidUtf8 => fmt.write_str("Utf8 encoding error"),
Error::LogicError(msg) => write!(fmt, "logic error: {}", msg),
Error::SerdeFleece(err) => write!(fmt, "serde+flecce error: {}", err),
Error::NulError(err) => write!(fmt, "argument contains null character: {}", err),
Error::InvalidQuery {
pos,
query_expr,
err,
} => {
let (msg, desc) = into_msg_desc(*err);
write!(
fmt,
"Can not parse query {}, error at {}: {} {}",
query_expr,
pos,
desc.as_utf8_lossy(),
msg.as_utf8_lossy()
)
}
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::C4Error(err) => {
let (msg, desc) = into_msg_desc(*err);
write!(
fmt,
"{:?} / {}: {}",
*err,
desc.as_utf8_lossy(),
msg.as_utf8_lossy()
)
}
Error::InvalidUtf8 => write!(fmt, "Invalid UTF-8 error"),
Error::LogicError(msg) => write!(fmt, "LogicError: {}", msg),
Error::SerdeFleece(err) => write!(fmt, "SerdeFleece error: {}", err),
Error::NulError(err) => write!(fmt, "NulError: {:?}", err),
Error::InvalidQuery {
pos,
query_expr,
err,
} => {
let (msg, desc) = into_msg_desc(*err);
write!(
fmt,
"InvalidQuery {{ query_expr {}, pos {}, err {:?} / {}: {} }}",
query_expr,
pos,
*err,
desc.as_utf8_lossy(),
msg.as_utf8_lossy()
)
}
}
}
}
impl From<C4Error> for Error {
fn from(err: C4Error) -> Self {
Error::C4Error(err)
}
}
impl From<serde_fleece::Error> for Error {
fn from(err: serde_fleece::Error) -> Self {
Error::SerdeFleece(err)
}
}
impl From<std::ffi::NulError> for Error {
fn from(err: std::ffi::NulError) -> Self {
Error::NulError(err)
}
}
#[inline]
pub(crate) fn c4error_init() -> C4Error {
C4Error {
domain: C4ErrorDomain::kC4MaxErrorDomainPlus1,
code: 0,
internal_info: 0,
}
}
fn into_msg_desc(err: C4Error) -> (FLSliceResult, FLSliceResult) {
let msg = unsafe { c4error_getMessage(err) };
let desc = unsafe { c4error_getDescription(err) };
(msg, desc)
}