Skip to content

Commit

Permalink
Fix lifetime annotations in parsers (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
helio-frota authored Feb 13, 2025
1 parent a8bb7f3 commit f1dc642
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
34 changes: 17 additions & 17 deletions sectxtlib/src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ impl SecurityTxtParser {
}
}

pub fn parse<'a>(&'a self, text: &'a str) -> Result<Vec<Option<RawField>>, ParseError> {
pub fn parse<'a>(&'a self, text: &'a str) -> Result<Vec<Option<RawField<'a>>>, ParseError> {
let (_, msg) = self.body_parser(text)?;
Ok(msg)
}

// body = signed / unsigned
// signed is handled separately.
fn body_parser<'a>(&'a self, i: &'a str) -> IResult<&str, Vec<Option<RawField>>> {
fn body_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, Vec<Option<RawField<'a>>>> {
all_consuming(|x| self.unsigned_parser(x))(i)
}

Expand All @@ -41,12 +41,12 @@ impl SecurityTxtParser {
// ; except that if contact-field appears more
// ; than once, the order of those indicates
// ; priority (see Section 3.5.3)
fn unsigned_parser<'a>(&'a self, i: &'a str) -> IResult<&str, Vec<Option<RawField>>> {
fn unsigned_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, Vec<Option<RawField<'a>>>> {
many1(|x| self.line_parser(x))(i)
}

// line = [ (field / comment) ] eol
fn line_parser<'a>(&'a self, i: &'a str) -> IResult<&str, Option<RawField>> {
fn line_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, Option<RawField<'a>>> {
let field_parser_opt = map(|x| self.field_parser(x), Some);
let comment_parser_opt = map(|x| self.comment_parser(x), |_| None);

Expand All @@ -56,7 +56,7 @@ impl SecurityTxtParser {
}

// eol = *WSP [CR] LF
fn eol_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn eol_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
recognize(tuple((take_while(is_wsp), opt(|x| self.cr_parser(x)), |x| {
self.lf_parser(x)
})))(i)
Expand All @@ -70,17 +70,17 @@ impl SecurityTxtParser {
// hiring-field /
// policy-field /
// ext-field
fn field_parser<'a>(&'a self, i: &'a str) -> IResult<&str, RawField> {
fn field_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, RawField<'a>> {
self.ext_name_parser(i)
}

// fs = ":"
fn fs_parser<'a>(&'a self, i: &'a str) -> IResult<&str, char> {
fn fs_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, char> {
char(':')(i)
}

// comment = "#" *(WSP / VCHAR / %x80-FFFFF)
fn comment_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn comment_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
let matcher = |x| is_wsp(x) || is_vchar(x) || x >= '\u{80}';
preceded(char('#'), take_while(matcher))(i)
}
Expand All @@ -99,7 +99,7 @@ impl SecurityTxtParser {
// uri = < URI as per Section 3 of [RFC3986] >

// ext-field = field-name fs SP unstructured
fn ext_name_parser<'a>(&'a self, i: &'a str) -> IResult<&str, RawField> {
fn ext_name_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, RawField<'a>> {
let (i, (name, _, _, value)) = tuple((
|x| self.field_name_parser(x),
|x| self.fs_parser(x),
Expand All @@ -111,14 +111,14 @@ impl SecurityTxtParser {

// field-name = < imported from Section 3.6.8 of [RFC5322] >
// field-name = 1*ftext
fn field_name_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn field_name_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
take_while1(is_ftext_char)(i)
}

// < imported from [RFC5322] >
// unstructured = *([FWS] VCHAR) *WSP
// Ommitted obsolete part.
fn unstructured_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn unstructured_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
recognize(terminated(
recognize(many0_count(preceded(opt(|x| self.fws_parser(x)), satisfy(is_vchar)))),
take_while(is_wsp),
Expand All @@ -128,29 +128,29 @@ impl SecurityTxtParser {
// < imported from [RFC5322] >
// FWS = [*WSP CRLF] 1*WSP
// Ommitted obsolete part.
fn fws_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn fws_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
recognize(preceded(
opt(tuple((take_while(is_wsp), |x| self.crlf_parser(x)))),
take_while1(is_wsp),
))(i)
}

fn cr_parser<'a>(&'a self, i: &'a str) -> IResult<&str, char> {
fn cr_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, char> {
satisfy(is_cr)(i)
}

// CRLF = CR LF
// ; Internet standard newline
fn crlf_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn crlf_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
crlf(i)
}

fn lf_parser<'a>(&'a self, i: &'a str) -> IResult<&str, char> {
fn lf_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, char> {
satisfy(is_lf)(i)
}

// SP = %x20
fn sp_parser<'a>(&'a self, i: &'a str) -> IResult<&str, char> {
fn sp_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, char> {
char(' ')(i)
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ mod tests {
println!("Input file: {:?}", file);
let buf = fs::read_to_string(file).unwrap();
let txt = unsigned_parser.parse(&buf);
assert_eq!(txt.is_ok(), true);
assert!(txt.is_ok());
}
}

Expand Down
32 changes: 16 additions & 16 deletions sectxtlib/src/pgpcleartextmessage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) struct PGPCleartextMessage<'a> {
pub signature: PGPSignature<'a>,
}

impl<'a> PGPCleartextMessage<'a> {}
impl PGPCleartextMessage<'_> {}

pub(crate) struct PGPCleartextMessageParser {
options: SecurityTxtOptions,
Expand All @@ -38,12 +38,12 @@ impl PGPCleartextMessageParser {
}
}

pub fn parse<'a>(&'a self, text: &'a str) -> Result<PGPCleartextMessage, ParseError> {
pub fn parse<'a>(&'a self, text: &'a str) -> Result<PGPCleartextMessage<'a>, ParseError> {
let (_, msg) = self.signed_parser(text)?;
Ok(msg)
}

fn lf_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn lf_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
match self.options.strict {
true => line_ending(i),
false => tag("\n")(i),
Expand All @@ -55,7 +55,7 @@ impl PGPCleartextMessageParser {
// CRLF
// cleartext
// signature
fn signed_parser<'a>(&'a self, i: &'a str) -> IResult<&str, PGPCleartextMessage> {
fn signed_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, PGPCleartextMessage<'a>> {
let (_, (_, hash_armor_headers, _, cleartext, signature)) = all_consuming(tuple((
|x| self.cleartext_header_parser(x),
many1(|x| self.hash_header_parser(x)),
Expand All @@ -75,12 +75,12 @@ impl PGPCleartextMessageParser {
}

// cleartext-header = %s"-----BEGIN PGP SIGNED MESSAGE-----" CRLF
fn cleartext_header_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn cleartext_header_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
terminated(tag("-----BEGIN PGP SIGNED MESSAGE-----"), |x| self.lf_parser(x))(i)
}

// hash-header = %s"Hash: " hash-alg *("," hash-alg) CRLF
fn hash_header_parser<'a>(&'a self, i: &'a str) -> IResult<&str, Vec<&str>> {
fn hash_header_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, Vec<&'a str>> {
delimited(
tag("Hash: "),
separated_list1(tag(","), |x| self.hash_alg_parser(x)),
Expand All @@ -92,27 +92,27 @@ impl PGPCleartextMessageParser {
// ; imported from RFC 2045; see RFC 4880 Section
// ; 10.3.3 for a pointer to the registry of
// ; valid values
fn hash_alg_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn hash_alg_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
self.token_parser(i)
}

// < Section 5.1 of [RFC2045] >
// token := 1*<any (US-ASCII) CHAR except SPACE, CTLs,
// or tspecials>
fn token_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn token_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
take_while1(is_token_char)(i)
}

// cleartext = *((line-dash / line-from / line-nodash) [CR] LF)
// EOL is handled in branches.
fn cleartext_parser<'a>(&'a self, i: &'a str) -> IResult<&str, String> {
fn cleartext_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, String> {
let (i, lines) = many0(alt((|x| self.line_dash_parser(x), |x| self.line_nodash_parser(x))))(i)?;
Ok((i, lines.join("")))
}

// line-dash = ("- ") "-" *UTF8-char-not-cr
// ; MUST include initial "- "
fn line_dash_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn line_dash_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
preceded(
tag("- "),
recognize(tuple((
Expand All @@ -125,7 +125,7 @@ impl PGPCleartextMessageParser {

// line-nodash = ["- "] *UTF8-char-not-cr
// ; MAY include initial "- "
fn line_nodash_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn line_nodash_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
preceded(
opt(tag("- ")),
recognize(tuple((
Expand All @@ -141,7 +141,7 @@ impl PGPCleartextMessageParser {
// CRLF
// signature-data
// armor-tail
fn signature_parser<'a>(&'a self, i: &'a str) -> IResult<&str, PGPSignature> {
fn signature_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, PGPSignature<'a>> {
let (i, (_, keys, _, signature, _)) = tuple((
|x| self.armor_header_parser(x),
|x| self.armor_keys_parser(x),
Expand All @@ -154,13 +154,13 @@ impl PGPCleartextMessageParser {
}

// armor-header = %s"-----BEGIN PGP SIGNATURE-----" CRLF
fn armor_header_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn armor_header_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
terminated(tag("-----BEGIN PGP SIGNATURE-----"), |x| self.lf_parser(x))(i)
}

// armor-keys = *(token ": " *( VCHAR / WSP ) CRLF)
// ; Armor Header Keys from RFC 4880
fn armor_keys_parser<'a>(&'a self, i: &'a str) -> IResult<&str, Vec<(&str, &str)>> {
fn armor_keys_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, Vec<(&'a str, &'a str)>> {
many0(terminated(
separated_pair(
|x| self.token_parser(x),
Expand All @@ -172,14 +172,14 @@ impl PGPCleartextMessageParser {
}

// armor-tail = %s"-----END PGP SIGNATURE-----" CRLF
fn armor_tail_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn armor_tail_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
terminated(tag("-----END PGP SIGNATURE-----"), |x| self.lf_parser(x))(i)
}

// signature-data = 1*(1*(ALPHA / DIGIT / "=" / "+" / "/") CRLF)
// ; base64; see RFC 4648
// ; includes RFC 4880 checksum
fn signature_data_parser<'a>(&'a self, i: &'a str) -> IResult<&str, &str> {
fn signature_data_parser<'a>(&'a self, i: &'a str) -> IResult<&'a str, &'a str> {
recognize(many1_count(terminated(take_while1(is_signature_data_char), |x| {
self.lf_parser(x)
})))(i)
Expand Down

0 comments on commit f1dc642

Please sign in to comment.