json_web_tolkien/
error.rs1use serde::{de, ser};
2use std::{fmt, result, string::FromUtf8Error};
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum Error {
7 #[error("unable to convert via base64, {source}")]
8 Base64ConversionError {
9 #[from]
10 source: base64ct::Error,
11 },
12 #[error("unable to determine order of elements, use sequences or tuples")]
13 UnableToDetermineElementOrder,
14 #[error("unexpected decoding error, {msg}")]
15 UnexpectedDecodingError { msg: Box<str> },
16 #[error("unexpected encoding error, {msg}")]
17 UnexpectedEncodingError { msg: Box<str> },
18 #[error("unexpected type: bool")]
19 UnexpectedTypeBool,
20 #[error("unexpected type: char")]
21 UnexpectedTypeChar,
22 #[error("unexpected type: float")]
23 UnexpectedTypeFloat,
24 #[error("unexpected type: integer")]
25 UnexpectedTypeInteger,
26 #[error("unexpected type is empty")]
27 UnexpectedEmptyType,
28 #[error("expected byte")]
29 ExpectedByte,
30 #[error("{value} is expected to be a utf8 string")]
31 InvalidEncoding { value: Box<str> },
32 #[error("{value} expected to be a JWS in the format of EncodedHeader.EncodedPayload.Signature")]
33 InvalidJwsFormat { value: Box<str> },
34 #[error("invalid length")]
35 InvalidLength,
36}
37
38pub type Result<T> = result::Result<T, Error>;
39
40impl From<FromUtf8Error> for Error {
41 fn from(err: FromUtf8Error) -> Self {
42 let attempted_bytes: &[u8] = err.as_bytes();
43 let value: Box<str> = {
44 let value = String::from_utf8_lossy(attempted_bytes);
45
46 value.to_string().into_boxed_str()
47 };
48
49 Self::InvalidEncoding { value }
50 }
51}
52
53impl de::Error for Error {
54 fn custom<T>(msg: T) -> Self
55 where
56 T: fmt::Display,
57 {
58 let msg: Box<str> = msg.to_string().into_boxed_str();
59 Self::UnexpectedDecodingError { msg }
60 }
61}
62
63impl ser::Error for Error {
64 fn custom<T>(msg: T) -> Self
65 where
66 T: std::fmt::Display,
67 {
68 let msg: Box<str> = msg.to_string().into_boxed_str();
69
70 Self::UnexpectedEncodingError { msg }
71 }
72}