Skip to main content

adaptive_card_core/
error.rs

1//! Error type for the core library.
2
3use crate::types::{Host, ValidationReport};
4
5/// Result alias used throughout the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Library error enum. Specific variants let callers match on failure mode.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11    #[error("invalid card JSON: {0}")]
12    InvalidJson(#[from] serde_json::Error),
13
14    #[error("card is not an AdaptiveCard (missing or invalid 'type' field)")]
15    NotAnAdaptiveCard,
16
17    #[error("schema validation failed: {errors} error(s)")]
18    SchemaInvalid {
19        errors: usize,
20        report: Box<ValidationReport>,
21    },
22
23    #[error("unsupported card version: {0}")]
24    UnsupportedVersion(String),
25
26    #[error("host {host:?} incompatible: {reason}")]
27    HostIncompatible { host: Host, reason: String },
28
29    #[error("transform would lose data (strict mode): {0}")]
30    TransformLossy(String),
31
32    #[error("data shape not recognized for data_to_card")]
33    UnrecognizedDataShape,
34
35    #[error("knowledge base entry not found: {id}")]
36    KnowledgeEntryNotFound { id: String },
37
38    #[error("knowledge base load failed: {0}")]
39    KnowledgeLoad(String),
40
41    #[error("internal error: {0}")]
42    Internal(String),
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn error_display_formats() {
51        let e = Error::NotAnAdaptiveCard;
52        assert_eq!(
53            e.to_string(),
54            "card is not an AdaptiveCard (missing or invalid 'type' field)"
55        );
56
57        let e = Error::UnsupportedVersion("2.0".to_string());
58        assert_eq!(e.to_string(), "unsupported card version: 2.0");
59    }
60
61    #[test]
62    fn error_from_serde_json() {
63        let bad: Result<serde_json::Value> = serde_json::from_str("not json").map_err(Error::from);
64        assert!(matches!(bad, Err(Error::InvalidJson(_))));
65    }
66}