1use std::fmt;
4
5#[derive(Debug, Clone)]
7pub enum CodegenError {
8 UnsupportedConstruct {
10 construct: String,
12 target_id: String,
14 },
15 NoSynthesisStrategy {
17 construct: String,
19 target_id: String,
21 },
22 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}