archunit/slices/uml/
plantuml_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
use std::{error::Error, fmt};

/// Invalid input in the supported PlantUML component-diagram subset.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PlantUmlError {
    message: String,
    line: Option<usize>,
}

impl PlantUmlError {
    pub(super) fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            line: None,
        }
    }

    pub(super) fn at_line(line: usize, message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            line: Some(line),
        }
    }

    /// Returns the stable diagnostic reason.
    #[must_use]
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Returns the one-based source line for a parser failure, when available.
    #[must_use]
    pub const fn line(&self) -> Option<usize> {
        self.line
    }
}

impl fmt::Display for PlantUmlError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(line) = self.line {
            write!(
                formatter,
                "invalid PlantUML at line {line}: {}",
                self.message
            )
        } else {
            write!(formatter, "invalid PlantUML: {}", self.message)
        }
    }
}

impl Error for PlantUmlError {}