Skip to content

Commit

Permalink
refactor(s3s/sig_v2): simplify parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Feb 1, 2025
1 parent 3885cf6 commit 890f133
Showing 1 changed file with 5 additions and 27 deletions.
32 changes: 5 additions & 27 deletions crates/s3s/src/sig_v2/authorization_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,14 @@ pub struct ParseAuthorizationV2Error {
}

impl<'a> AuthorizationV2<'a> {
pub fn parse(input: &'a str) -> Result<Self, ParseAuthorizationV2Error> {
match parser::parse_authorization(input) {
Ok(("", ans)) => Ok(ans),
Ok(_) | Err(_) => Err(ParseAuthorizationV2Error { _priv: () }),
}
}
}

mod parser {
use super::AuthorizationV2;

use crate::utils::parser::consume;
pub fn parse(mut input: &'a str) -> Result<Self, ParseAuthorizationV2Error> {
let err = || ParseAuthorizationV2Error { _priv: () };

use nom::bytes::complete::{tag, take, take_till};
use nom::combinator::rest;
use nom::sequence::terminated;
use nom::IResult;
input = input.strip_prefix("AWS ").ok_or_else(err)?;

pub fn parse_authorization(mut input: &str) -> IResult<&str, AuthorizationV2<'_>> {
let s = &mut input;

consume(s, tag("AWS "))?;
let access_key = consume(s, until_colon0)?;
let signature = consume(s, rest)?;

Ok((input, AuthorizationV2 { access_key, signature }))
}
let (access_key, signature) = input.split_once(':').ok_or_else(err)?;

fn until_colon0(input: &str) -> IResult<&str, &str> {
terminated(take_till(|c| c == ':'), take(1_usize))(input)
Ok(Self { access_key, signature })
}
}

Expand Down

0 comments on commit 890f133

Please sign in to comment.