Skip to content

Commit

Permalink
Implement FromStrRegex for some types.
Browse files Browse the repository at this point in the history
  • Loading branch information
frozenlib committed Nov 17, 2024
1 parent ba14921 commit 58bf847
Showing 1 changed file with 100 additions and 1 deletion.
101 changes: 100 additions & 1 deletion parse-display/src/from_str_regex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use core::num::NonZero;
use std::{ffi::OsString, path::PathBuf};

/// A trait for getting regex patterns that match strings parseable by [`FromStr`](core::str::FromStr).
///
/// When using [`#[derive(FromStr)]`](derive@crate::Display) with the [`#[from_str(regex_infer)]`](derive@crate::Display#from_strregex_infer) attribute,
Expand All @@ -8,9 +11,30 @@ pub trait FromStrRegex: core::str::FromStr {
/// Note: Matching this pattern does not guarantee that the string can be parsed successfully.
fn from_str_regex() -> String;
}

impl FromStrRegex for char {
fn from_str_regex() -> String {
r"(?s:.)".into()
}
}

fn regex_any() -> String {
r"(?s:.*?)".into()
}

impl FromStrRegex for String {
fn from_str_regex() -> String {
"(?s:.*?)".into()
regex_any()
}
}
impl FromStrRegex for OsString {
fn from_str_regex() -> String {
regex_any()
}
}
impl FromStrRegex for PathBuf {
fn from_str_regex() -> String {
regex_any()
}
}

Expand All @@ -28,56 +52,131 @@ impl FromStrRegex for u8 {
regex_uint()
}
}
impl FromStrRegex for NonZero<u8> {
fn from_str_regex() -> String {
regex_uint()
}
}

impl FromStrRegex for u16 {
fn from_str_regex() -> String {
regex_uint()
}
}
impl FromStrRegex for NonZero<u16> {
fn from_str_regex() -> String {
regex_uint()
}
}

impl FromStrRegex for u32 {
fn from_str_regex() -> String {
regex_uint()
}
}
impl FromStrRegex for NonZero<u32> {
fn from_str_regex() -> String {
regex_uint()
}
}

impl FromStrRegex for u64 {
fn from_str_regex() -> String {
regex_uint()
}
}
impl FromStrRegex for NonZero<u64> {
fn from_str_regex() -> String {
regex_uint()
}
}

impl FromStrRegex for u128 {
fn from_str_regex() -> String {
regex_uint()
}
}
impl FromStrRegex for NonZero<u128> {
fn from_str_regex() -> String {
regex_uint()
}
}

impl FromStrRegex for usize {
fn from_str_regex() -> String {
regex_uint()
}
}
impl FromStrRegex for NonZero<usize> {
fn from_str_regex() -> String {
regex_uint()
}
}

fn regex_sint() -> String {
r"-?[0-9]+".into()
}

impl FromStrRegex for i8 {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for NonZero<i8> {
fn from_str_regex() -> String {
regex_sint()
}
}

impl FromStrRegex for i16 {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for NonZero<i16> {
fn from_str_regex() -> String {
regex_sint()
}
}

impl FromStrRegex for i32 {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for NonZero<i32> {
fn from_str_regex() -> String {
regex_sint()
}
}

impl FromStrRegex for i64 {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for NonZero<i64> {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for i128 {
fn from_str_regex() -> String {
regex_sint()
}
}

impl FromStrRegex for isize {
fn from_str_regex() -> String {
regex_sint()
}
}
impl FromStrRegex for NonZero<isize> {
fn from_str_regex() -> String {
regex_sint()
}
}

fn regex_f() -> String {
r"(?i:[+-]?([0-9]+\.?|[0-9]*\.[0-9]+)(e[+-]?[0-9]+)?|[+-]?inf|nan)".into()
}
Expand Down

0 comments on commit 58bf847

Please sign in to comment.