rasn_compiler/validator/
error.rs

1use core::fmt::{Display, Formatter, Result};
2use std::error::Error;
3
4use crate::intermediate::error::GrammarError;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct LinkerError {
8    pub pdu: Option<String>,
9    pub details: String,
10    pub kind: LinkerErrorType,
11}
12
13impl LinkerError {
14    pub fn new(pdu: Option<String>, details: &str, kind: LinkerErrorType) -> Self {
15        LinkerError {
16            pdu,
17            details: details.into(),
18            kind,
19        }
20    }
21
22    pub fn contextualize(&mut self, pdu: &str) {
23        self.pdu = Some(pdu.into())
24    }
25}
26
27#[derive(Debug, Clone, PartialEq)]
28pub enum LinkerErrorType {
29    MissingDependency,
30    InvalidConstraintsError,
31    Unknown,
32}
33
34impl Error for LinkerError {}
35
36impl Display for LinkerError {
37    fn fmt(&self, f: &mut Formatter) -> Result {
38        write!(
39            f,
40            "{:?} validating PDU {}: {}",
41            self.kind,
42            self.pdu.as_ref().unwrap_or(&"".into()),
43            self.details
44        )
45    }
46}
47
48impl From<GrammarError> for LinkerError {
49    fn from(value: GrammarError) -> Self {
50        Self {
51            pdu: None,
52            details: value.details,
53            kind: LinkerErrorType::Unknown,
54        }
55    }
56}