Skip to main content

graphcal_compiler/syntax/
attribute.rs

1//! Attribute names recognized by the language.
2
3use thiserror::Error;
4
5/// Known attribute names in the language.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum AttributeName {
8    Assumes,
9    ExpectedFail,
10    Hidden,
11    Lazy,
12}
13
14/// Error returned when parsing an unknown attribute name.
15#[derive(Debug, Clone, PartialEq, Eq, Error)]
16#[error("unknown attribute `{raw}`")]
17pub struct UnknownAttributeName {
18    raw: String,
19}
20
21impl UnknownAttributeName {
22    /// Create an unknown-attribute-name error from the original source text.
23    #[must_use]
24    pub fn new(raw: impl Into<String>) -> Self {
25        Self { raw: raw.into() }
26    }
27
28    /// The unrecognized attribute name text.
29    #[must_use]
30    pub fn raw(&self) -> &str {
31        &self.raw
32    }
33
34    /// Consume and return the unrecognized attribute name text.
35    #[must_use]
36    pub fn into_raw(self) -> String {
37        self.raw
38    }
39}
40
41impl std::str::FromStr for AttributeName {
42    type Err = UnknownAttributeName;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s {
46            "assumes" => Ok(Self::Assumes),
47            "expected_fail" => Ok(Self::ExpectedFail),
48            "hidden" => Ok(Self::Hidden),
49            "lazy" => Ok(Self::Lazy),
50            _ => Err(UnknownAttributeName::new(s)),
51        }
52    }
53}