std: implement ParseFragment for Option

Closes #17
This commit is contained in:
Dmitriy Pleshevskiy 2022-07-26 23:27:11 +03:00
parent 103c8f9249
commit 62e38ccf6f
2 changed files with 38 additions and 3 deletions

View file

@ -3,6 +3,4 @@
mod bool; mod bool;
mod number; mod number;
mod option;
pub use self::bool::*;
pub use number::*;

37
src/std/option.rs Normal file
View file

@ -0,0 +1,37 @@
use crate::core::{EString, ParseFragment};
impl<T> ParseFragment for Option<T>
where
T: ParseFragment,
{
fn parse_frag(es: EString) -> crate::Result<Self> {
if es.is_empty() {
Ok(None)
} else {
es.parse().map(Some)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_parse_empty_string_as_none() {
let estr = EString::from("");
match estr.parse::<Option<i32>>() {
Ok(res) => assert_eq!(res, None),
_ => unreachable!(),
}
}
#[test]
fn should_parse_number_as_some() {
let estr = EString::from("99");
match estr.parse::<Option<i32>>() {
Ok(res) => assert_eq!(res, Some(99)),
_ => unreachable!(),
}
}
}