use crate::text::parse_result::IonParseResult;
use nom::bytes::streaming::tag;
use nom::combinator::{map, recognize};
use nom::sequence::terminated;
use nom::Parser;
use crate::text::parsers::stop_character;
use crate::text::text_value::TextValue;
pub(crate) fn parse_boolean(input: &str) -> IonParseResult<TextValue> {
map(
recognize(terminated(tag("true").or(tag("false")), stop_character)),
|bool_text: &str| match bool_text {
"true" => TextValue::Bool(true),
"false" => TextValue::Bool(false),
_ => unreachable!("text had to match 'true' or 'false' before reaching this point"),
},
)(input)
}
#[cfg(test)]
mod boolean_parsing_tests {
use crate::text::parsers::boolean::parse_boolean;
use crate::text::parsers::unit_test_support::{parse_test_err, parse_test_ok};
use crate::text::text_value::TextValue;
fn parse_equals(text: &str, expected: bool) {
parse_test_ok(parse_boolean, text, TextValue::Bool(expected))
}
fn parse_fails(text: &str) {
parse_test_err(parse_boolean, text)
}
#[test]
fn test_parse_booleans() {
parse_equals("true ", true);
parse_equals("false ", false);
parse_fails("ture ");
parse_fails("fasle ");
parse_fails("True ");
parse_fails("TRUE ");
parse_fails(" true");
parse_fails("true");
}
}