Skip to main content

keelson_gen/
error.rs

1use std::fmt;
2
3/// Everything that can go wrong between a connection string and the emitted
4/// files. One enum, no `anyhow`: callers (the CLI, tests, build scripts)
5/// match on the kind.
6#[derive(Debug)]
7pub enum GenError {
8    /// The TOML configuration failed to parse or contradicts itself.
9    Config(String),
10    /// The catalog queries failed or returned something unusable.
11    Introspect(String),
12    /// A column's database type has no default mapping and no configured
13    /// override — the honest failure `docs/type-mappings.md` prescribes.
14    UnmappedType {
15        /// `table.column` the failure names.
16        column: String,
17        /// The declared database type that had no mapping.
18        db_type: String,
19    },
20    /// A feature the generator deliberately does not cover yet (MySQL
21    /// emission, composite-column foreign keys, …).
22    Unsupported(String),
23    /// Filesystem trouble writing the output.
24    Io(std::io::Error),
25}
26
27impl fmt::Display for GenError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            GenError::Config(msg) => write!(f, "config: {msg}"),
31            GenError::Introspect(msg) => write!(f, "introspection: {msg}"),
32            GenError::UnmappedType { column, db_type } => write!(
33                f,
34                "no type mapping for {column} (db type `{db_type}`); \
35                 add a [types.map] or [[types.override]] entry"
36            ),
37            GenError::Unsupported(msg) => write!(f, "unsupported: {msg}"),
38            GenError::Io(e) => write!(f, "io: {e}"),
39        }
40    }
41}
42
43impl std::error::Error for GenError {
44    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45        match self {
46            GenError::Io(e) => Some(e),
47            _ => None,
48        }
49    }
50}
51
52impl From<std::io::Error> for GenError {
53    fn from(e: std::io::Error) -> Self {
54        GenError::Io(e)
55    }
56}
57
58/// The crate-wide result.
59pub type Result<T> = std::result::Result<T, GenError>;