#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum BuiltinKind {
#[default]
Line,
Block,
}
pub trait TriviaClass {
fn is_line_like(&self) -> bool;
}
impl TriviaClass for BuiltinKind {
fn is_line_like(&self) -> bool {
matches!(self, Self::Line)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Trivia<K = BuiltinKind> {
Comment { kind: K, text: String },
BlankLine,
}
impl<K> Trivia<K> {
#[must_use]
pub const fn is_blank(&self) -> bool {
matches!(self, Self::BlankLine)
}
#[must_use]
pub fn text(&self) -> Option<&str> {
match self {
Self::Comment { text, .. } => Some(text),
Self::BlankLine => None,
}
}
#[must_use]
pub const fn kind(&self) -> Option<&K> {
match self {
Self::Comment { kind, .. } => Some(kind),
Self::BlankLine => None,
}
}
}
impl<K: TriviaClass> Trivia<K> {
#[must_use]
pub fn is_line_like(&self) -> bool {
match self {
Self::Comment { kind, .. } => kind.is_line_like(),
Self::BlankLine => false,
}
}
}
impl Trivia<BuiltinKind> {
#[must_use]
pub fn line(text: impl Into<String>) -> Self {
Self::Comment {
kind: BuiltinKind::Line,
text: text.into(),
}
}
#[must_use]
pub fn block(text: impl Into<String>) -> Self {
Self::Comment {
kind: BuiltinKind::Block,
text: text.into(),
}
}
}