Skip to main content

specta_go/
error.rs

1use std::{error, fmt, io};
2
3use crate::Layout;
4
5#[derive(Debug)]
6/// Errors that can occur during Go code generation.
7pub enum Error {
8    /// IO error during file operations.
9    Io(io::Error),
10    /// Formatting error while writing generated code.
11    Fmt(fmt::Error),
12    /// Custom format callback failed.
13    Format {
14        /// Context describing which format callback failed.
15        message: &'static str,
16        /// The underlying format error.
17        source: specta::FormatError,
18    },
19    /// A generated Go identifier used a forbidden name.
20    ForbiddenName {
21        /// Path to the type or field containing the forbidden name.
22        path: String,
23        /// The forbidden Go identifier.
24        name: String,
25    },
26    /// A BigInt value was encountered but cannot be represented in Go.
27    BigIntForbidden {
28        /// Path to the unsupported BigInt value.
29        path: String,
30    },
31    /// The configured layout cannot be exported by this operation.
32    UnableToExport(Layout),
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Error::Io(e) => write!(f, "IO error: {e}"),
39            Error::Fmt(e) => write!(f, "Fmt error: {e}"),
40            Error::Format { message, source } => write!(f, "Format error: {message}: {source}"),
41            Error::ForbiddenName { path, name } => {
42                write!(f, "Forbidden name: {name} in {path}")
43            }
44            Error::BigIntForbidden { path } => {
45                write!(f, "BigInt forbidden in {path}")
46            }
47            Error::UnableToExport(layout) => {
48                write!(f, "Unable to export layout: {layout:?}")
49            }
50        }
51    }
52}
53
54impl error::Error for Error {}
55
56impl From<io::Error> for Error {
57    fn from(e: io::Error) -> Self {
58        Self::Io(e)
59    }
60}
61
62impl From<fmt::Error> for Error {
63    fn from(e: fmt::Error) -> Self {
64        Self::Fmt(e)
65    }
66}
67
68impl Error {
69    pub(crate) fn format(message: &'static str, source: specta::FormatError) -> Self {
70        Self::Format { message, source }
71    }
72}