Trait sscanf::RegexRepresentation [−][src]
A Trait used by scanf
to obtain the Regex of a Type
Has one associated Constant: REGEX
, which should be set to a regular Expression.
Implement this trait for a Type that you want to be parsed using scanf.
The Regular Expression should match the string representation as exactly as possible.
Any incorrect matches might be caught in the from_str parsing, but that might cause this
regex to take characters that could have been matched by other placeholders, leading to
unexpected parsing failures. Also: Since scanf
only returns an Option
it will just say
None
whether the regex matching failed or the parsing failed, so you should avoid parsing
failures by writing a proper regex as much as possible.
Example
Let’s say we want to add a Fraction parser
struct Fraction(isize, usize);
Which can be obtained from any string of the kind ±X/Y
or just X
impl sscanf::RegexRepresentation for Fraction { /// matches an optional '-' or '+' followed by a number. /// possibly with a '/' and another Number const REGEX: &'static str = r"[-+]?\d+(/\d+)?"; } impl std::str::FromStr for Fraction { type Err = std::num::ParseIntError; fn from_str(s: &str) -> Result<Self, Self::Err> { let mut iter = s.split('/'); let num = iter.next().unwrap().parse::<isize>()?; let mut denom = 1; if let Some(d) = iter.next() { denom = d.parse::<usize>()?; } Ok(Fraction(num, denom)) } }
Now we can use this Fraction
struct in scanf
:
use sscanf::scanf; let output = scanf!("2/5", "{}", Fraction); assert_eq!(output, Some(Fraction(2, 5))); let output = scanf!("-25/3", "{}", Fraction); assert_eq!(output, Some(Fraction(-25, 3))); let output = scanf!("8", "{}", Fraction); assert_eq!(output, Some(Fraction(8, 1))); let output = scanf!("6e/3", "{}", Fraction); assert_eq!(output, None); let output = scanf!("6/-3", "{}", Fraction); assert_eq!(output, None); // only first number can be negative let output = scanf!("6/3", "{}", Fraction); assert_eq!(output, Some(Fraction(6, 3)));
Associated Constants
const REGEX: &'static str
[src]
A regular Expression that exactly matches any String representation of the implementing Type
Implementations on Foreign Types
impl RegexRepresentation for usize
[src]
const REGEX: &'static str
[src]
Matches any positive number
The length of this match might not fit into the size of the type
impl RegexRepresentation for u64
[src]
const REGEX: &'static str
[src]
Matches any positive number
The length of this match might not fit into the size of the type
impl RegexRepresentation for u128
[src]
const REGEX: &'static str
[src]
Matches any positive number
The length of this match might not fit into the size of the type
impl RegexRepresentation for isize
[src]
const REGEX: &'static str
[src]
Matches any positive or negative number
The length of this match might not fit into the size of the type
impl RegexRepresentation for i64
[src]
const REGEX: &'static str
[src]
Matches any positive or negative number
The length of this match might not fit into the size of the type
impl RegexRepresentation for i128
[src]
const REGEX: &'static str
[src]
Matches any positive or negative number
The length of this match might not fit into the size of the type
impl RegexRepresentation for f32
[src]
const REGEX: &'static str
[src]
Matches any floating point number
Does NOT support stuff like inf
nan
or 3e10
. See FullF32
for those.
impl RegexRepresentation for f64
[src]
const REGEX: &'static str
[src]
Matches any floating point number
Does NOT support stuff like inf
nan
or 3e10
. See FullF32
for those.
impl RegexRepresentation for String
[src]
impl RegexRepresentation for char
[src]
impl RegexRepresentation for bool
[src]
impl RegexRepresentation for u8
[src]
const REGEX: &'static str
[src]
Matches a number with up to 3 digits.
The Number matched by this might be too big for the type
impl RegexRepresentation for u16
[src]
const REGEX: &'static str
[src]
Matches a number with up to 5 digits.
The Number matched by this might be too big for the type
impl RegexRepresentation for u32
[src]
const REGEX: &'static str
[src]
Matches a number with up to 10 digits.
The Number matched by this might be too big for the type
impl RegexRepresentation for i8
[src]
const REGEX: &'static str
[src]
Matches a number with possible sign and up to 3 digits.
The Number matched by this might be too big for the type
impl RegexRepresentation for i16
[src]
const REGEX: &'static str
[src]
Matches a number with possible sign and up to 5 digits.
The Number matched by this might be too big for the type
impl RegexRepresentation for i32
[src]
const REGEX: &'static str
[src]
Matches a number with possible sign and up to 10 digits.
The Number matched by this might be too big for the type
Implementors
impl RegexRepresentation for FullF32
[src]
const REGEX: &'static str
[src]
Matches any floating point number, including nan
, inf
, 2.0e5
, …
See FromStr on f32 for details