Skip to content

Commit e5b7ddd

Browse files
committed
Add FromStrFormat::regex_pattern and make FromStrFormat::regex deprecated.
To unify the signatures between `FromStrFormat` and `FromStrRegex`.
1 parent 7559d3a commit e5b7ddd

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

parse-display/src/from_str_regex.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use core::num::NonZero;
22
use std::{ffi::OsString, path::PathBuf};
33

4+
use crate::ANY_REGEX;
5+
46
/// A trait for getting regex patterns that match strings parseable by [`FromStr`](core::str::FromStr).
57
///
68
/// When using [`#[derive(FromStr)]`](derive@crate::FromStr) with the [`#[from_str(regex_infer)]`](derive@crate::Display#from_strregex_infer) attribute,
@@ -19,7 +21,7 @@ impl FromStrRegex for char {
1921
}
2022

2123
fn regex_any() -> String {
22-
r"(?s:.*?)".into()
24+
ANY_REGEX.into()
2325
}
2426

2527
impl FromStrRegex for String {

parse-display/src/helpers_std.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@ use std::{borrow::Cow, fmt};
55
use regex::Regex;
66
use regex_syntax::ast::{Ast, Flags, GroupKind};
77

8-
use crate::{DisplayFormat, FromStrFormat, FromStrRegex};
8+
use crate::{ANY_REGEX, DisplayFormat, FromStrFormat, FromStrRegex};
99

1010
pub use regex;
1111

1212
#[track_caller]
1313
pub fn to_regex<T, E>(format: &dyn FromStrFormat<T, Err = E>) -> Option<String> {
14-
format.regex()
14+
let s = format.regex_pattern();
15+
if s == ANY_REGEX { None } else { Some(s) }
1516
}
1617

1718
#[track_caller]
1819
pub fn to_ast<T, E>(format: &dyn FromStrFormat<T, Err = E>) -> Option<(String, Ast)> {
19-
let s = format.regex()?;
20-
let ast = ast_from_str(&s);
21-
Some((s, ast))
20+
let s = format.regex_pattern();
21+
if s == ANY_REGEX {
22+
None
23+
} else {
24+
let ast = ast_from_str(&s);
25+
Some((s, ast))
26+
}
2227
}
2328

2429
#[track_caller]

parse-display/src/lib.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,9 @@ pub trait DisplayFormat<T: ?Sized> {
966966
fn write(&self, f: &mut Formatter, value: &T) -> Result;
967967
}
968968

969+
/// Regular expression that matches any string. Equivalent to `"(?s:.*?)"`.
970+
pub const ANY_REGEX: &str = "(?s:.*?)";
971+
969972
/// Parsing method used in [`#[display(with = ...)]` and `#[from_str(with = ...)]`](macro@Display#displaywith---from_strwith--).
970973
pub trait FromStrFormat<T> {
971974
type Err;
@@ -975,7 +978,7 @@ pub trait FromStrFormat<T> {
975978

976979
/// Return a regular expression that the input string needs to match.
977980
///
978-
/// If None is returned, the input will be a string that matches `(?s:.*?)`.
981+
/// By default, [`ANY_REGEX`] is returned, which matches any string.
979982
///
980983
/// # Examples
981984
///
@@ -988,8 +991,8 @@ pub trait FromStrFormat<T> {
988991
/// fn parse(&self, s: &str) -> std::result::Result<String, Self::Err> {
989992
/// s.parse()
990993
/// }
991-
/// fn regex(&self) -> Option<String> {
992-
/// Some(r"[0-9]+".into())
994+
/// fn regex_pattern(&self) -> String {
995+
/// r"[0-9]+".into()
993996
/// }
994997
/// }
995998
///
@@ -1020,8 +1023,8 @@ pub trait FromStrFormat<T> {
10201023
/// fn parse(&self, _s: &str) -> core::result::Result<T, Self::Err> {
10211024
/// Ok(Default::default())
10221025
/// }
1023-
/// fn regex(&self) -> Option<String> {
1024-
/// Some(type_name::<T>().to_string())
1026+
/// fn regex_pattern(&self) -> String {
1027+
/// type_name::<T>().to_string()
10251028
/// }
10261029
/// }
10271030
///
@@ -1031,6 +1034,14 @@ pub trait FromStrFormat<T> {
10311034
/// let _ = X::<u16>::from_str("u16"); // panic on debug mode
10321035
/// ```
10331036
#[cfg(feature = "std")]
1037+
#[allow(deprecated)]
1038+
fn regex_pattern(&self) -> String {
1039+
self.regex().unwrap_or(ANY_REGEX.into())
1040+
}
1041+
1042+
#[cfg(feature = "std")]
1043+
#[deprecated(note = r"use `regex_pattern` instead.
1044+
In `regex_pattern`, use `ANY_REGEX` instead of `None` for patterns that matches any string.")]
10341045
fn regex(&self) -> Option<String> {
10351046
None
10361047
}

0 commit comments

Comments
 (0)