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 number;
pub use self::bool::*;
pub use number::*;
mod option;

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!(),
}
}
}