Skip to main content

bock_codegen/
error.rs

1//! Codegen error types.
2
3use std::fmt;
4
5/// Errors that can occur during code generation.
6#[derive(Debug, Clone)]
7pub enum CodegenError {
8    /// An AIR construct has no viable representation in the target.
9    UnsupportedConstruct {
10        /// Description of the construct (e.g., "algebraic_types").
11        construct: String,
12        /// Target that lacks support.
13        target_id: String,
14    },
15    /// A required capability gap has no known synthesis strategy.
16    NoSynthesisStrategy {
17        /// The construct that needs synthesis.
18        construct: String,
19        /// Target that needs the synthesis.
20        target_id: String,
21    },
22    /// Generic internal error.
23    Internal(String),
24}
25
26impl fmt::Display for CodegenError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Self::UnsupportedConstruct {
30                construct,
31                target_id,
32            } => write!(
33                f,
34                "unsupported construct `{construct}` for target `{target_id}`"
35            ),
36            Self::NoSynthesisStrategy {
37                construct,
38                target_id,
39            } => write!(
40                f,
41                "no synthesis strategy for `{construct}` on target `{target_id}`"
42            ),
43            Self::Internal(msg) => write!(f, "internal codegen error: {msg}"),
44        }
45    }
46}
47
48impl std::error::Error for CodegenError {}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn unsupported_construct_display() {
56        let err = CodegenError::UnsupportedConstruct {
57            construct: "algebraic_types".into(),
58            target_id: "js".into(),
59        };
60        let msg = format!("{err}");
61        assert!(msg.contains("algebraic_types"));
62        assert!(msg.contains("js"));
63    }
64
65    #[test]
66    fn no_synthesis_display() {
67        let err = CodegenError::NoSynthesisStrategy {
68            construct: "pattern_matching".into(),
69            target_id: "go".into(),
70        };
71        let msg = format!("{err}");
72        assert!(msg.contains("pattern_matching"));
73        assert!(msg.contains("go"));
74    }
75
76    #[test]
77    fn internal_display() {
78        let err = CodegenError::Internal("oops".into());
79        assert!(format!("{err}").contains("oops"));
80    }
81}