Skip to content

Commit

Permalink
refactor(parser): Abstract over lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ed Page committed Jan 2, 2021
1 parent 663eb94 commit 48112a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
14 changes: 14 additions & 0 deletions crates/typos/src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ impl<'c> Status<'c> {
}
}

pub fn into_owned(self) -> Status<'static> {
match self {
Status::Valid => Status::Valid,
Status::Invalid => Status::Invalid,
Status::Corrections(corrections) => {
let corrections = corrections
.into_iter()
.map(|c| Cow::Owned(c.into_owned()))
.collect();
Status::Corrections(corrections)
}
}
}

pub fn borrow(&self) -> Status<'_> {
match self {
Status::Corrections(corrections) => {
Expand Down
27 changes: 23 additions & 4 deletions crates/typos/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::tokens;
use crate::Dictionary;
use std::borrow::Cow;

#[derive(Clone)]
pub struct ParserBuilder<'p, 'd> {
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<'p, 'd> TyposParser<'p, 'd> {
Some(corrections) => {
let typo = Typo {
byte_offset: ident.offset(),
typo: ident.token(),
typo: ident.token().into(),
corrections,
};
itertools::Either::Left(Some(typo).into_iter())
Expand All @@ -105,7 +106,7 @@ impl<'p, 'd> TyposParser<'p, 'd> {
Some(corrections) => {
let typo = Typo {
byte_offset: word.offset(),
typo: word.token(),
typo: word.token().into(),
corrections,
};
Some(typo)
Expand All @@ -119,15 +120,33 @@ impl<'p, 'd> TyposParser<'p, 'd> {
#[non_exhaustive]
pub struct Typo<'m> {
pub byte_offset: usize,
pub typo: &'m str,
pub typo: Cow<'m, str>,
pub corrections: crate::Status<'m>,
}

impl<'m> Typo<'m> {
pub fn into_owned(self) -> Typo<'static> {
Typo {
byte_offset: self.byte_offset,
typo: Cow::Owned(self.typo.into_owned()),
corrections: self.corrections.into_owned(),
}
}

pub fn borrow(&self) -> Typo<'_> {
Typo {
byte_offset: self.byte_offset,
typo: Cow::Borrowed(self.typo.as_ref()),
corrections: self.corrections.borrow(),
}
}
}

impl<'m> Default for Typo<'m> {
fn default() -> Self {
Self {
byte_offset: 0,
typo: "",
typo: "".into(),
corrections: crate::Status::Invalid,
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Check for Typos {
context: Some(report::PathContext { path }.into()),
buffer: std::borrow::Cow::Borrowed(file_name.as_bytes()),
byte_offset: typo.byte_offset,
typo: typo.typo,
typo: typo.typo.as_ref(),
corrections: typo.corrections,
};
reporter.report(msg.into())?;
Expand All @@ -134,7 +134,7 @@ impl Check for Typos {
context: Some(report::FileContext { path, line_num }.into()),
buffer: std::borrow::Cow::Borrowed(line),
byte_offset: line_offset,
typo: typo.typo,
typo: typo.typo.as_ref(),
corrections: typo.corrections,
};
reporter.report(msg.into())?;
Expand Down

0 comments on commit 48112a4

Please sign in to comment.