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    UnrepresentableMessageName { message: String },
11    NameClash(Box<NameClash>),
12}
13
14impl From<Box<NameClash>> for Error {
15    fn from(clash: Box<NameClash>) -> Self {
16        Error::NameClash(clash)
17    }
18}
19
20impl std::fmt::Display for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Error::UnrepresentableName { field } => {
24                write!(f, "field '{field}' cannot be a rust method name")
25            }
26            Error::UnrepresentableValue { field, description } => {
27                write!(
28                    f,
29                    "value '{description}' of field '{field}' cannot be a rust variant name"
30                )
31            }
32            Error::UnrepresentableGroupName { context } => {
33                write!(f, "{context} cannot be a rust name")
34            }
35            Error::UnrepresentableMessageName { message } => {
36                write!(f, "message '{message}' cannot be a rust module name")
37            }
38            Error::NameClash(clash) => write!(f, "rust name {clash}"),
39        }
40    }
41}
42
43impl std::error::Error for Error {}