pub struct FromStrParser<T> { /* private fields */ }Expand description
Generic parser implementation for any type that implements FromStr trait
This parser consumes exactly one token and parses it using the target type’s FromStr
implementation for producing the parser’s result. Note, that on failure, the error value is
discarded. FromStrParser does not recognize any attributes and does not yield any completion
suggestions.
Note, that there is no blanket implementation of Parsable that uses this parser
implementation. If you want to use this implementation for any custom type, you must either
implement Parsable for it or specify this parser explicitly when performing parsing or
completion.
The implementation is similar to IntegerParser with the only difference being more specific
error handling by the IntegerParser.
§Example
The following example demonstrates how to use FromStrParser for custom types that implement
FromStr:
use cmdparse::parsers::FromStrParser;
use cmdparse::{parse, Parsable};
use std::str::FromStr;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum MyBool {
Yes,
No,
}
impl FromStr for MyBool {
type Err = (); // the error type is discarded by the parser
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"yes" => Ok(MyBool::Yes),
"no" => Ok(MyBool::No),
_ => Err(()),
}
}
}
impl<Ctx> Parsable<Ctx> for MyBool {
type Parser = FromStrParser<Self>;
}
assert_eq!(parse::<_, MyBool>("yes", ()), Ok(MyBool::Yes));
assert_eq!(parse::<_, MyBool>("no", ()), Ok(MyBool::No));Trait Implementations§
Source§impl<T: Clone> Clone for FromStrParser<T>
impl<T: Clone> Clone for FromStrParser<T>
Source§fn clone(&self) -> FromStrParser<T>
fn clone(&self) -> FromStrParser<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for FromStrParser<T>
impl<T> Debug for FromStrParser<T>
Source§impl<T> Default for FromStrParser<T>
impl<T> Default for FromStrParser<T>
Source§impl<T: FromStr, Ctx> Parser<Ctx> for FromStrParser<T>
impl<T: FromStr, Ctx> Parser<Ctx> for FromStrParser<T>
Source§fn parse<'a>(
&self,
input: TokenStream<'a>,
_ctx: Ctx,
) -> ParseResult<'a, Self::Value>
fn parse<'a>( &self, input: TokenStream<'a>, _ctx: Ctx, ) -> ParseResult<'a, Self::Value>
Value. Read more