serde_firestore_value/
error.rs

1use crate::google::firestore::v1::value::ValueType;
2
3use super::{value_type_ext::ValueTypeExt, value_type_name::ValueTypeName};
4
5/// A specialized [`Result`] type for this crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Represents all possible errors that can occur when serializing or deserializing a Firestore Value.
9#[derive(Debug, thiserror::Error)]
10#[error(transparent)]
11pub struct Error {
12    #[from]
13    code: ErrorCode,
14}
15
16impl Error {
17    pub(super) fn invalid_value_type(value_type: &ValueType, expected: ValueTypeName) -> Self {
18        <Self as serde::de::Error>::invalid_type(
19            serde::de::Unexpected::Other(value_type.name().as_str()),
20            &expected.as_str(),
21        )
22    }
23}
24
25impl serde::de::Error for Error {
26    fn custom<T: std::fmt::Display>(msg: T) -> Self {
27        Error::from(ErrorCode::Custom(msg.to_string()))
28    }
29}
30
31impl serde::ser::Error for Error {
32    fn custom<T: std::fmt::Display>(msg: T) -> Self {
33        Error::from(ErrorCode::Custom(msg.to_string()))
34    }
35}
36
37#[derive(Debug, thiserror::Error)]
38pub(crate) enum ErrorCode {
39    #[error("{0}")]
40    Custom(String),
41    #[error("i16 out of range")]
42    I16OutOfRange,
43    #[error("i32 out of range")]
44    I32OutOfRange,
45    #[error("i8 out of range")]
46    I8OutOfRange,
47    #[error("key must be a string")]
48    KeyMustBeAString,
49    #[error("maximum byte length (1,048,487 bytes = 1MiB - 89 bytes) exceeded")]
50    MaximumByteLengthExceeded,
51    #[error("reference value must be a string")]
52    ReferenceValueMustBeAString,
53    #[error("string is empty")]
54    StringIsEmpty,
55    #[error("too many chars")]
56    TooManyChars,
57    #[error("u16 out of range")]
58    U16OutOfRange,
59    #[error("u32 out of range")]
60    U32OutOfRange,
61    #[error("u64 is not supported")]
62    U64IsNotSupported,
63    #[error("u8 out of range")]
64    U8OutOfRange,
65    #[error("value type must be some")]
66    ValueTypeMustBeSome,
67}