Ref: https://wicg.github.io/urlpattern/#parse-a-pattern-string
( input: &str, options: &Options, encoding_callback: F, )
| 367 | |
| 368 | // Ref: https://wicg.github.io/urlpattern/#parse-a-pattern-string |
| 369 | pub fn parse_pattern_string<F>( |
| 370 | input: &str, |
| 371 | options: &Options, |
| 372 | encoding_callback: F, |
| 373 | ) -> Result<Vec<Part>, Error> |
| 374 | where |
| 375 | F: Fn(&str) -> Result<String, Error>, |
| 376 | { |
| 377 | let token_list = crate::tokenizer::tokenize( |
| 378 | input, |
| 379 | crate::tokenizer::TokenizePolicy::Strict, |
| 380 | )?; |
| 381 | |
| 382 | let mut parser = PatternParser { |
| 383 | token_list, |
| 384 | encoding_callback, |
| 385 | segment_wildcard_regexp: options.generate_segment_wildcard_regexp(), |
| 386 | part_list: vec![], |
| 387 | pending_fixed_value: String::new(), |
| 388 | index: 0, |
| 389 | next_numeric_name: 0, |
| 390 | }; |
| 391 | |
| 392 | while parser.index < parser.token_list.len() { |
| 393 | let char_token = parser.try_consume_token(TokenType::Char); |
| 394 | let mut name_token = parser.try_consume_token(TokenType::Name); |
| 395 | let mut regexp_or_wildcard_token = |
| 396 | parser.try_consume_regexp_or_wildcard_token(name_token.is_none()); |
| 397 | if name_token.is_some() || regexp_or_wildcard_token.is_some() { |
| 398 | let mut prefix = String::new(); |
| 399 | if let Some(char_token) = char_token { |
| 400 | char_token.value.clone_into(&mut prefix); |
| 401 | } |
| 402 | if !prefix.is_empty() && prefix != options.prefix_code_point { |
| 403 | parser.pending_fixed_value.push_str(&prefix); |
| 404 | prefix = String::new(); |
| 405 | } |
| 406 | parser.maybe_add_part_from_pending_fixed_value()?; |
| 407 | let modifier_token = parser.try_consume_modifier_token(); |
| 408 | parser.add_part( |
| 409 | &prefix, |
| 410 | name_token, |
| 411 | regexp_or_wildcard_token, |
| 412 | "", |
| 413 | modifier_token, |
| 414 | )?; |
| 415 | continue; |
| 416 | } |
| 417 | let mut fixed_token = char_token; |
| 418 | if fixed_token.is_none() { |
| 419 | fixed_token = parser.try_consume_token(TokenType::EscapedChar); |
| 420 | } |
| 421 | if let Some(fixed_token) = fixed_token { |
| 422 | parser.pending_fixed_value.push_str(fixed_token.value); |
| 423 | continue; |
| 424 | } |
| 425 | let open_token = parser.try_consume_token(TokenType::Open); |
| 426 | if open_token.is_some() { |
no test coverage detected