Skip to main content

specta_serde/
error.rs

1use std::{error, fmt};
2
3/// Detected a type which Serde is unable to export.
4// TODO: The error should show a path to the type causing the issue like the BigInt error reporting.
5#[derive(Debug, PartialEq)]
6pub enum Error {
7    /// A map key uses a type that cannot be represented as a Serde map key.
8    InvalidMapKey,
9    /// Internally tagged enum layout is invalid for the encountered enum shape.
10    InvalidInternallyTaggedEnum,
11    /// `#[specta(skip)]` was used in a way that prevents valid serialization.
12    InvalidUsageOfSkip,
13}
14
15impl fmt::Display for Error {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Error::InvalidMapKey => writeln!(f, "A map key must be a 'string' or 'number' type"),
19            Error::InvalidInternallyTaggedEnum => writeln!(
20                f,
21                "#[specta(tag = \"...\")] cannot be used with tuple variants"
22            ),
23            Error::InvalidUsageOfSkip => writeln!(
24                f,
25                "the usage of #[specta(skip)] means the type can't be serialized"
26            ),
27        }
28    }
29}
30
31impl error::Error for Error {}