Skip to main content

parse_display/
from_str_regex.rs

1use core::num::NonZero;
2use std::{ffi::OsString, path::PathBuf};
3
4use crate::ANY_REGEX;
5
6/// A trait for getting regex patterns that match strings parseable by [`FromStr`](core::str::FromStr).
7///
8/// When using [`#[derive(FromStr)]`](derive@crate::FromStr) with the [`#[from_str(regex_infer)]`](derive@crate::Display#from_strregex_infer) attribute,
9/// the regex pattern is obtained from the `FromStrRegex` implementation of the field's type.
10pub trait FromStrRegex: core::str::FromStr {
11    /// Returns a regex pattern for strings that might be parseable by [`FromStr`](core::str::FromStr).
12    ///
13    /// Note: Matching this pattern does not guarantee that the string can be parsed successfully.
14    fn from_str_regex() -> String;
15}
16
17impl FromStrRegex for char {
18    fn from_str_regex() -> String {
19        r"(?s:.)".into()
20    }
21}
22
23fn regex_any() -> String {
24    ANY_REGEX.into()
25}
26
27impl FromStrRegex for String {
28    fn from_str_regex() -> String {
29        regex_any()
30    }
31}
32impl FromStrRegex for OsString {
33    fn from_str_regex() -> String {
34        regex_any()
35    }
36}
37impl FromStrRegex for PathBuf {
38    fn from_str_regex() -> String {
39        regex_any()
40    }
41}
42
43impl FromStrRegex for bool {
44    fn from_str_regex() -> String {
45        r"true|false".into()
46    }
47}
48
49fn regex_uint() -> String {
50    r"[0-9]+".into()
51}
52impl FromStrRegex for u8 {
53    fn from_str_regex() -> String {
54        regex_uint()
55    }
56}
57impl FromStrRegex for NonZero<u8> {
58    fn from_str_regex() -> String {
59        regex_uint()
60    }
61}
62
63impl FromStrRegex for u16 {
64    fn from_str_regex() -> String {
65        regex_uint()
66    }
67}
68impl FromStrRegex for NonZero<u16> {
69    fn from_str_regex() -> String {
70        regex_uint()
71    }
72}
73
74impl FromStrRegex for u32 {
75    fn from_str_regex() -> String {
76        regex_uint()
77    }
78}
79impl FromStrRegex for NonZero<u32> {
80    fn from_str_regex() -> String {
81        regex_uint()
82    }
83}
84
85impl FromStrRegex for u64 {
86    fn from_str_regex() -> String {
87        regex_uint()
88    }
89}
90impl FromStrRegex for NonZero<u64> {
91    fn from_str_regex() -> String {
92        regex_uint()
93    }
94}
95
96impl FromStrRegex for u128 {
97    fn from_str_regex() -> String {
98        regex_uint()
99    }
100}
101impl FromStrRegex for NonZero<u128> {
102    fn from_str_regex() -> String {
103        regex_uint()
104    }
105}
106
107impl FromStrRegex for usize {
108    fn from_str_regex() -> String {
109        regex_uint()
110    }
111}
112impl FromStrRegex for NonZero<usize> {
113    fn from_str_regex() -> String {
114        regex_uint()
115    }
116}
117
118fn regex_sint() -> String {
119    r"-?[0-9]+".into()
120}
121
122impl FromStrRegex for i8 {
123    fn from_str_regex() -> String {
124        regex_sint()
125    }
126}
127impl FromStrRegex for NonZero<i8> {
128    fn from_str_regex() -> String {
129        regex_sint()
130    }
131}
132
133impl FromStrRegex for i16 {
134    fn from_str_regex() -> String {
135        regex_sint()
136    }
137}
138impl FromStrRegex for NonZero<i16> {
139    fn from_str_regex() -> String {
140        regex_sint()
141    }
142}
143
144impl FromStrRegex for i32 {
145    fn from_str_regex() -> String {
146        regex_sint()
147    }
148}
149impl FromStrRegex for NonZero<i32> {
150    fn from_str_regex() -> String {
151        regex_sint()
152    }
153}
154
155impl FromStrRegex for i64 {
156    fn from_str_regex() -> String {
157        regex_sint()
158    }
159}
160impl FromStrRegex for NonZero<i64> {
161    fn from_str_regex() -> String {
162        regex_sint()
163    }
164}
165impl FromStrRegex for i128 {
166    fn from_str_regex() -> String {
167        regex_sint()
168    }
169}
170impl FromStrRegex for NonZero<i128> {
171    fn from_str_regex() -> String {
172        regex_sint()
173    }
174}
175
176impl FromStrRegex for isize {
177    fn from_str_regex() -> String {
178        regex_sint()
179    }
180}
181impl FromStrRegex for NonZero<isize> {
182    fn from_str_regex() -> String {
183        regex_sint()
184    }
185}
186
187fn regex_f() -> String {
188    r"(?i:[+-]?([0-9]+\.?|[0-9]*\.[0-9]+)(e[+-]?[0-9]+)?|[+-]?inf|nan)".into()
189}
190impl FromStrRegex for f32 {
191    fn from_str_regex() -> String {
192        regex_f()
193    }
194}
195impl FromStrRegex for f64 {
196    fn from_str_regex() -> String {
197        regex_f()
198    }
199}