Skip to main content

refix_codegen/rust/
error.rs

1use refix_dictionary::MemberContext;
2
3use crate::NameClash;
4
5#[derive(Debug, Eq, PartialEq)]
6pub enum Error {
7    UnrepresentableName { field: String },
8    UnrepresentableValue { field: String, description: String },
9    UnrepresentableGroupName { context: MemberContext },
10    UnrepresentableComponentName { component: String },
11    NameClash(Box<NameClash>),
12    ModuleClash(Box<NameClash>),
13}
14
15impl From<Box<NameClash>> for Error {
16    fn from(clash: Box<NameClash>) -> Self {
17        Error::NameClash(clash)
18    }
19}
20
21impl std::fmt::Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Error::UnrepresentableName { field } => {
25                write!(f, "field '{field}' cannot be a rust method name")
26            }
27            Error::UnrepresentableValue { field, description } => {
28                write!(
29                    f,
30                    "value '{description}' of field '{field}' cannot be a rust variant name"
31                )
32            }
33            Error::UnrepresentableGroupName { context } => {
34                write!(f, "{context} cannot be a rust name")
35            }
36            Error::UnrepresentableComponentName { component } => {
37                write!(f, "component '{component}' cannot be a rust method name")
38            }
39            Error::NameClash(clash) => write!(f, "rust name {clash}"),
40            Error::ModuleClash(clash) => write!(f, "rust module {clash}"),
41        }
42    }
43}
44
45impl std::error::Error for Error {}