Ref: https://wicg.github.io/urlpattern/#generate-a-pattern-string
(part_list: &[Part], options: &Options)
| 179 | |
| 180 | // Ref: https://wicg.github.io/urlpattern/#generate-a-pattern-string |
| 181 | fn generate_pattern_string(part_list: &[Part], options: &Options) -> String { |
| 182 | let mut result = String::new(); |
| 183 | for (i, part) in part_list.iter().enumerate() { |
| 184 | let prev_part: Option<&Part> = |
| 185 | if i == 0 { None } else { part_list.get(i - 1) }; |
| 186 | let next_part: Option<&Part> = part_list.get(i + 1); |
| 187 | if part.kind == PartType::FixedText { |
| 188 | if part.modifier == PartModifier::None { |
| 189 | result.push_str(&escape_pattern_string(&part.value)); |
| 190 | continue; |
| 191 | } |
| 192 | write!( |
| 193 | result, |
| 194 | "{{{}}}{}", |
| 195 | escape_pattern_string(&part.value), |
| 196 | part.modifier |
| 197 | ) |
| 198 | .unwrap(); |
| 199 | continue; |
| 200 | } |
| 201 | let custom_name = !part.name.chars().next().unwrap().is_ascii_digit(); |
| 202 | let mut needs_grouping = !part.suffix.is_empty() |
| 203 | || (!part.prefix.is_empty() && part.prefix != options.prefix_code_point); |
| 204 | if !needs_grouping |
| 205 | && custom_name |
| 206 | && part.kind == PartType::SegmentWildcard |
| 207 | && part.modifier == PartModifier::None |
| 208 | && matches!(next_part, Some(Part { prefix, suffix, .. }) if prefix.is_empty() && suffix.is_empty()) |
| 209 | { |
| 210 | let next_part = next_part.unwrap(); |
| 211 | if next_part.kind == PartType::FixedText { |
| 212 | needs_grouping = is_valid_name_codepoint( |
| 213 | next_part.value.chars().next().unwrap_or_default(), |
| 214 | false, |
| 215 | ); |
| 216 | } else { |
| 217 | needs_grouping = |
| 218 | next_part.name.chars().next().unwrap().is_ascii_digit(); |
| 219 | } |
| 220 | } |
| 221 | if !needs_grouping |
| 222 | && part.prefix.is_empty() |
| 223 | && matches!( |
| 224 | prev_part, |
| 225 | Some(Part { |
| 226 | kind: PartType::FixedText, |
| 227 | value, |
| 228 | .. |
| 229 | }) if !value.is_empty() && value.chars().last().unwrap().to_string() == options.prefix_code_point |
| 230 | ) |
| 231 | { |
| 232 | needs_grouping = true; |
| 233 | } |
| 234 | assert!(!part.name.is_empty()); |
| 235 | if needs_grouping { |
| 236 | result.push('{'); |
| 237 | } |
| 238 | result.push_str(&escape_pattern_string(&part.prefix)); |
no test coverage detected