Skip to main content

serde_fleece/
error.rs

1use crate::ffi::FLError;
2use std::{
3    borrow::Cow,
4    char::ParseCharError,
5    fmt::{self, Display},
6    num::{ParseFloatError, ParseIntError},
7    str::{ParseBoolError, Utf8Error},
8};
9
10#[derive(Debug)]
11pub enum Error {
12    Fleece(FLError),
13    Custom(Box<str>),
14    Unsupported(&'static str),
15    InvalidFormat(Cow<'static, str>),
16}
17
18impl From<FLError> for Error {
19    fn from(v: FLError) -> Self {
20        Error::Fleece(v)
21    }
22}
23
24impl From<Utf8Error> for Error {
25    fn from(_: Utf8Error) -> Self {
26        Error::InvalidFormat("not valid utf-8".into())
27    }
28}
29
30impl From<ParseBoolError> for Error {
31    fn from(err: ParseBoolError) -> Self {
32        Error::InvalidFormat(format!("parsing of bool failed: {err}").into())
33    }
34}
35
36impl From<ParseCharError> for Error {
37    fn from(err: ParseCharError) -> Self {
38        Error::InvalidFormat(format!("parsing of char failed: {err}").into())
39    }
40}
41
42impl From<ParseIntError> for Error {
43    fn from(err: ParseIntError) -> Self {
44        Error::InvalidFormat(format!("parsing of integer failed: {err}").into())
45    }
46}
47
48impl From<ParseFloatError> for Error {
49    fn from(err: ParseFloatError) -> Self {
50        Error::InvalidFormat(format!("parsing of float failed: {err}").into())
51    }
52}
53
54impl Display for Error {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            Error::Fleece(err) => {
58                let msg = match *err {
59                    FLError::kFLNoError => "No error",
60                    FLError::kFLMemoryError => "Out of memory, or allocation failed",
61                    FLError::kFLOutOfRange => " Array index or iterator out of range",
62                    FLError::kFLInvalidData => "Bad input data (NaN, non-string key, etc.)",
63                    FLError::kFLEncodeError => {
64                        "Structural error encoding (missing value, too many ends, etc.)"
65                    }
66                    FLError::kFLJSONError => "Error parsing JSON",
67                    FLError::kFLUnknownValue => {
68                        "Unparseable data in a Value (corrupt? Or from some distant future?)"
69                    }
70                    FLError::kFLInternalError => "Something that shouldn't happen",
71                    FLError::kFLNotFound => "Key not found",
72                    FLError::kFLSharedKeysStateError => {
73                        "Misuse of shared keys (not in transaction, etc.)"
74                    }
75                    FLError::kFLPOSIXError => "Posix error",
76                    FLError::kFLUnsupported => "Operation is unsupported",
77                    _ => "Unknown fleece error",
78                };
79                write!(f, "FLeece error: {msg}")
80            }
81            Error::Custom(msg) => write!(f, "Custom error: {msg}"),
82            Error::Unsupported(msg) => write!(f, "Unsupported operation: {msg}"),
83            Error::InvalidFormat(msg) => write!(f, "invalid fleece data: {msg}"),
84        }
85    }
86}
87
88impl std::error::Error for Error {}
89
90impl serde::ser::Error for Error {
91    fn custom<T>(msg: T) -> Self
92    where
93        T: std::fmt::Display,
94    {
95        Self::Custom(msg.to_string().into())
96    }
97}
98
99impl serde::de::Error for Error {
100    fn custom<T>(msg: T) -> Self
101    where
102        T: Display,
103    {
104        Self::Custom(msg.to_string().into())
105    }
106}