Skip to content

Commit

Permalink
Refactor Line predicates (#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 22, 2024
1 parent b81ba66 commit 7909958
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ pub(crate) struct Line<'src> {
}

impl Line<'_> {
pub(crate) fn is_empty(&self) -> bool {
self.fragments.is_empty()
fn first(&self) -> Option<&str> {
if let Fragment::Text { token } = self.fragments.first()? {
Some(token.lexeme())
} else {
None
}
}

pub(crate) fn is_comment(&self) -> bool {
matches!(
self.fragments.first(),
Some(Fragment::Text { token }) if token.lexeme().starts_with('#'),
)
self.first().is_some_and(|text| text.starts_with('#'))
}

pub(crate) fn is_continuation(&self) -> bool {
Expand All @@ -28,26 +29,23 @@ impl Line<'_> {
)
}

pub(crate) fn is_shebang(&self) -> bool {
matches!(
self.fragments.first(),
Some(Fragment::Text { token }) if token.lexeme().starts_with("#!"),
)
pub(crate) fn is_empty(&self) -> bool {
self.fragments.is_empty()
}

pub(crate) fn is_infallible(&self) -> bool {
self
.first()
.is_some_and(|text| text.starts_with('-') || text.starts_with("@-"))
}

pub(crate) fn is_quiet(&self) -> bool {
matches!(
self.fragments.first(),
Some(Fragment::Text { token })
if token.lexeme().starts_with('@') || token.lexeme().starts_with("-@"),
)
self
.first()
.is_some_and(|text| text.starts_with('@') || text.starts_with("-@"))
}

pub(crate) fn is_infallible(&self) -> bool {
matches!(
self.fragments.first(),
Some(Fragment::Text { token })
if token.lexeme().starts_with('-') || token.lexeme().starts_with("@-"),
)
pub(crate) fn is_shebang(&self) -> bool {
self.first().is_some_and(|text| text.starts_with("#!"))
}
}

0 comments on commit 7909958

Please sign in to comment.