diff --git a/src/std.rs b/src/std.rs index 3d33818..113f5d0 100644 --- a/src/std.rs +++ b/src/std.rs @@ -3,6 +3,4 @@ mod bool; mod number; - -pub use self::bool::*; -pub use number::*; +mod option; diff --git a/src/std/option.rs b/src/std/option.rs new file mode 100644 index 0000000..1b14faa --- /dev/null +++ b/src/std/option.rs @@ -0,0 +1,37 @@ +use crate::core::{EString, ParseFragment}; + +impl ParseFragment for Option +where + T: ParseFragment, +{ + fn parse_frag(es: EString) -> crate::Result { + 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::>() { + Ok(res) => assert_eq!(res, None), + _ => unreachable!(), + } + } + + #[test] + fn should_parse_number_as_some() { + let estr = EString::from("99"); + match estr.parse::>() { + Ok(res) => assert_eq!(res, Some(99)), + _ => unreachable!(), + } + } +}