Skip to main content

meta_language/
link_flags.rs

1/// Tree-sitter-compatible parse status flags modeled as link metadata.
2#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
3pub struct LinkFlags {
4    bits: u8,
5}
6
7impl LinkFlags {
8    const IS_ERROR: u8 = 0b0001;
9    const HAS_ERROR: u8 = 0b0010;
10    const IS_MISSING: u8 = 0b0100;
11    const IS_EXTRA: u8 = 0b1000;
12
13    /// Clean link flags.
14    #[must_use]
15    pub const fn clean() -> Self {
16        Self { bits: 0 }
17    }
18
19    /// Flags for an error link.
20    #[must_use]
21    pub const fn error() -> Self {
22        Self {
23            bits: Self::IS_ERROR,
24        }
25    }
26
27    /// Flags for a link that contains an error below it.
28    #[must_use]
29    pub const fn containing_error() -> Self {
30        Self {
31            bits: Self::HAS_ERROR,
32        }
33    }
34
35    /// Flags for a missing link.
36    #[must_use]
37    pub const fn missing() -> Self {
38        Self {
39            bits: Self::IS_MISSING,
40        }
41    }
42
43    /// Flags for an extra/trivia link.
44    #[must_use]
45    pub const fn extra() -> Self {
46        Self {
47            bits: Self::IS_EXTRA,
48        }
49    }
50
51    /// Whether this link is an error link.
52    #[must_use]
53    pub const fn is_error(self) -> bool {
54        self.bits & Self::IS_ERROR != 0
55    }
56
57    /// Whether this link contains an error below it.
58    #[must_use]
59    pub const fn has_error(self) -> bool {
60        self.bits & Self::HAS_ERROR != 0
61    }
62
63    /// Whether this link is missing from the source text.
64    #[must_use]
65    pub const fn is_missing(self) -> bool {
66        self.bits & Self::IS_MISSING != 0
67    }
68
69    /// Whether this link is extra source trivia.
70    #[must_use]
71    pub const fn is_extra(self) -> bool {
72        self.bits & Self::IS_EXTRA != 0
73    }
74}