use core::fmt;
use std::borrow::Cow;
use specta_serde::SerdeError;
use thiserror::Error;
use specta::ImplLocation;
use super::ExportPath;
#[derive(Error, Debug, PartialEq)]
pub enum NamedLocation {
Type,
Field,
Variant,
}
impl fmt::Display for NamedLocation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Type => write!(f, "type"),
Self::Field => write!(f, "field"),
Self::Variant => write!(f, "variant"),
}
}
}
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum ExportError {
#[error("Attempted to export '{0}' but Specta configuration forbids exporting BigInt types (i64, u64, i128, u128) because we don't know if your se/deserializer supports it. You can change this behavior by editing your `ExportConfiguration`!")]
BigIntForbidden(ExportPath),
#[error("Serde error: {0}")]
Serde(#[from] SerdeError),
#[error("Attempted to export '{1}' but was unable to due to {0} name '{2}' conflicting with a reserved keyword in Typescript. Try renaming it or using `#[specta(rename = \"new name\")]`")]
ForbiddenName(NamedLocation, ExportPath, &'static str),
#[error("Attempted to export '{1}' but was unable to due to {0} name '{2}' containing an invalid character")]
InvalidName(NamedLocation, ExportPath, String),
#[error("Attempted to export '{0}' with tagging but the type is not tagged.")]
InvalidTagging(ExportPath),
#[error("Attempted to export '{0}' with internal tagging but the variant is a tuple struct.")]
InvalidTaggedVariantContainingTupleStruct(ExportPath),
#[error("Unable to export type named '{0}' from locations '{:?}' '{:?}'", .1.as_str(), .2.as_str())]
DuplicateTypeName(Cow<'static, str>, ImplLocation, ImplLocation),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("fmt error: {0}")]
Fmt(#[from] std::fmt::Error),
#[error("Failed to export '{0}' due to error: {1}")]
Other(ExportPath, String),
}
impl PartialEq for ExportError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::BigIntForbidden(l0), Self::BigIntForbidden(r0)) => l0 == r0,
(Self::Serde(l0), Self::Serde(r0)) => l0 == r0,
(Self::ForbiddenName(l0, l1, l2), Self::ForbiddenName(r0, r1, r2)) => {
l0 == r0 && l1 == r1 && l2 == r2
}
(Self::InvalidName(l0, l1, l2), Self::InvalidName(r0, r1, r2)) => {
l0 == r0 && l1 == r1 && l2 == r2
}
(Self::InvalidTagging(l0), Self::InvalidTagging(r0)) => l0 == r0,
(
Self::InvalidTaggedVariantContainingTupleStruct(l0),
Self::InvalidTaggedVariantContainingTupleStruct(r0),
) => l0 == r0,
(Self::DuplicateTypeName(l0, l1, l2), Self::DuplicateTypeName(r0, r1, r2)) => {
l0 == r0 && l1 == r1 && l2 == r2
}
(Self::Io(l0), Self::Io(r0)) => l0.to_string() == r0.to_string(), (Self::Other(l0, l1), Self::Other(r0, r1)) => l0 == r0 && l1 == r1,
_ => false,
}
}
}