diff --git a/src/constructor_parser.rs b/src/constructor_parser.rs index 5bcd8dc..4fb93cf 100644 --- a/src/constructor_parser.rs +++ b/src/constructor_parser.rs @@ -25,7 +25,7 @@ enum ConstructorStringParserState { // Ref: https://wicg.github.io/urlpattern/#constructor-string-parser struct ConstructorStringParser<'a> { input: &'a str, - token_list: Vec, + token_list: Vec>, result: UrlPatternInit, component_start: usize, token_index: usize, diff --git a/src/parser.rs b/src/parser.rs index 8051530..84052ae 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -176,11 +176,11 @@ impl Part { } // Ref: https://wicg.github.io/urlpattern/#pattern-parser -struct PatternParser +struct PatternParser<'a, F> where F: Fn(&str) -> Result, { - token_list: Vec, + token_list: Vec>, encoding_callback: F, segment_wildcard_regexp: String, part_list: Vec, @@ -189,12 +189,12 @@ where next_numeric_name: usize, } -impl PatternParser +impl<'a, F> PatternParser<'a, F> where F: Fn(&str) -> Result, { // Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-token - fn try_consume_token(&mut self, kind: TokenType) -> Option { + fn try_consume_token(&mut self, kind: TokenType) -> Option> { assert!(self.index < self.token_list.len()); let next_token = self.token_list[self.index].clone(); if next_token.kind != kind { @@ -210,7 +210,7 @@ where fn try_consume_regexp_or_wildcard_token( &mut self, name_token_is_none: bool, - ) -> Option { + ) -> Option> { let token = self.try_consume_token(TokenType::Regexp); if name_token_is_none && token.is_none() { self.try_consume_token(TokenType::Asterisk) @@ -221,7 +221,7 @@ where // Ref: https://wicg.github.io/urlpattern/#try-to-consume-a-modifier-token #[inline] - fn try_consume_modifier_token(&mut self) -> Option { + fn try_consume_modifier_token(&mut self) -> Option> { self .try_consume_token(TokenType::OtherModifier) .or_else(|| self.try_consume_token(TokenType::Asterisk)) @@ -255,7 +255,7 @@ where ) -> Result<(), Error> { let mut modifier = PartModifier::None; if let Some(modifier_token) = modifier_token { - modifier = match modifier_token.value.as_ref() { + modifier = match modifier_token.value { "?" => PartModifier::Optional, "*" => PartModifier::ZeroOrMore, "+" => PartModifier::OneOrMore, @@ -306,7 +306,7 @@ where let mut name = String::new(); if let Some(name_token) = name_token { - name = name_token.value; + name = name_token.value.to_owned(); } else if regexp_or_wildcard_token.is_some() { name = self.next_numeric_name.to_string(); self.next_numeric_name += 1; @@ -344,7 +344,7 @@ where if token.is_none() { break; } - result.push_str(&token.unwrap().value); + result.push_str(token.unwrap().value); } result } @@ -359,7 +359,7 @@ where Error::Parser(ParserError::ExpectedToken( kind, self.token_list[self.index].kind.clone(), - self.token_list[self.index].value.clone(), + self.token_list[self.index].value.to_owned(), )) }) } @@ -419,7 +419,7 @@ where fixed_token = parser.try_consume_token(TokenType::EscapedChar); } if let Some(fixed_token) = fixed_token { - parser.pending_fixed_value.push_str(&fixed_token.value); + parser.pending_fixed_value.push_str(fixed_token.value); continue; } let open_token = parser.try_consume_token(TokenType::Open); diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 4828c0f..7792872 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -27,10 +27,10 @@ pub enum TokenType { // Ref: https://wicg.github.io/urlpattern/#token #[derive(Debug, Clone)] -pub struct Token { +pub struct Token<'a> { pub kind: TokenType, pub index: usize, - pub value: String, + pub value: &'a str, } // Ref: https://wicg.github.io/urlpattern/#tokenize-policy @@ -44,7 +44,7 @@ pub enum TokenizePolicy { struct Tokenizer<'a> { input: &'a str, policy: TokenizePolicy, - token_list: Vec, + token_list: Vec>, index: usize, next_index: usize, code_point: Option, // TODO: get rid of Option @@ -86,7 +86,7 @@ impl<'a> Tokenizer<'a> { value_len: usize, ) { let range = value_pos..(value_pos + value_len); - let value = self.input[range].to_owned(); + let value = &self.input[range]; self.token_list.push(Token { kind, index: self.index,