rasn_compiler/validator/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use core::fmt::{Display, Formatter, Result};
use std::error::Error;

use crate::intermediate::error::GrammarError;

#[derive(Debug, Clone, PartialEq)]
pub struct LinkerError {
    pub pdu: Option<String>,
    pub details: String,
    pub kind: LinkerErrorType,
}

impl LinkerError {
    pub fn new(pdu: Option<String>, details: &str, kind: LinkerErrorType) -> Self {
        LinkerError {
            pdu,
            details: details.into(),
            kind,
        }
    }

    pub fn contextualize(&mut self, pdu: &str) {
        self.pdu = Some(pdu.into())
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum LinkerErrorType {
    MissingDependency,
    InvalidConstraintsError,
    Unknown,
}

impl Error for LinkerError {}

impl Display for LinkerError {
    fn fmt(&self, f: &mut Formatter) -> Result {
        write!(
            f,
            "{:?} validating PDU {}: {}",
            self.kind,
            self.pdu.as_ref().unwrap_or(&"".into()),
            self.details
        )
    }
}

impl From<GrammarError> for LinkerError {
    fn from(value: GrammarError) -> Self {
        Self {
            pdu: None,
            details: value.details,
            kind: LinkerErrorType::Unknown,
        }
    }
}