Builds a regular expression that matches some parsed Subpatterns.
(subpatterns: &[Subpattern], case_insensitive: bool)
| 362 | |
| 363 | /// Builds a regular expression that matches some parsed Subpatterns. |
| 364 | fn build_regex(subpatterns: &[Subpattern], case_insensitive: bool) -> Result<Regex, EvalError> { |
| 365 | let mut r = String::from("^"); |
| 366 | for sp in subpatterns { |
| 367 | sp.write_regex_to(&mut r); |
| 368 | } |
| 369 | r.push('$'); |
| 370 | match Regex::new(&r, case_insensitive) { |
| 371 | Ok(regex) => Ok(regex), |
| 372 | Err(RegexCompilationError::PatternTooLarge { .. }) => Err(EvalError::LikePatternTooLong), |
| 373 | Err(RegexCompilationError::RegexError(regex::Error::CompiledTooBig(_))) => { |
| 374 | Err(EvalError::LikePatternTooLong) |
| 375 | } |
| 376 | Err(e) => Err(EvalError::Internal( |
| 377 | format!("build_regex produced invalid regex: {}", e).into(), |
| 378 | )), |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Unit Tests |
| 383 | // |
no test coverage detected