#[derive(Debug)]
pub struct EnumDecodeError {
pub postgres_type: &'static str,
pub received: String,
pub expected: &'static [&'static str],
}
impl std::fmt::Display for EnumDecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"unknown variant `{}` for Postgres enum `{}`; expected one of: {}",
self.received,
self.postgres_type,
self.expected.join(", ")
)
}
}
impl std::error::Error for EnumDecodeError {}
#[cfg(test)]
mod tests {
use super::EnumDecodeError;
#[test]
fn decode_error_display() {
let err = EnumDecodeError {
postgres_type: "vehicle_status",
received: "unknown_val".to_owned(),
expected: &["active", "in_maintenance", "decommissioned"],
};
let msg = err.to_string();
assert!(
msg.contains("unknown_val"),
"error message must include the received value"
);
assert!(
msg.contains("vehicle_status"),
"error message must include the postgres type name"
);
assert!(
msg.contains("active"),
"error message must include expected variants"
);
}
}