@@ -23,6 +23,8 @@ use crate::value::{ToValue, Value};
2323use std:: rc:: Rc ;
2424
2525use std:: io:: BufRead ;
26+ use nom:: Err :: Error ;
27+ use nom:: error:: ErrorKind ;
2628//
2729// Note; the difference between ours 'parsers'
2830// identifier_parser
@@ -170,6 +172,7 @@ pub fn integer_parser(input: &str) -> IResult<&str, i32> {
170172 ) ;
171173 integer_lexer ( input) . map ( |( rest, digits) | ( rest, digits. parse ( ) . unwrap ( ) ) )
172174}
175+
173176// Currently used to create 'try_readers', which are readers (or
174177// reader functions, at least) that are basically composable InputType
175178// -> IResult<InputType,Value> parsers, that our normal read function
@@ -199,6 +202,18 @@ pub fn try_read_i32(input: &str) -> IResult<&str, Value> {
199202 to_value_parser ( integer_parser) ( input)
200203}
201204
205+ /// Tries to parse &str into Value::Boolean
206+ /// Expects:
207+ /// Booleans
208+ /// Example success:
209+ /// true => Value::Boolean(true)
210+ /// false => Value::Boolean(false)
211+ pub fn try_read_bool ( input : & str ) -> IResult < & str , Value > {
212+ named ! ( bool_parser<& str , & str >, alt!( tag!( "true" ) | tag!( "false" ) ) ) ;
213+ let ( rest_input, bool) = bool_parser ( input) ?;
214+ Ok ( ( rest_input, Value :: Boolean ( bool. parse ( ) . unwrap ( ) ) ) )
215+ }
216+
202217// Perhaps generalize this into reader macros
203218/// Tries to parse &str into Value::Keyword
204219/// Example Successes:
@@ -323,6 +338,7 @@ pub fn try_read(input: &str) -> IResult<&str, Value> {
323338 try_read_map,
324339 try_read_string,
325340 try_read_i32,
341+ try_read_bool,
326342 try_read_symbol,
327343 try_read_keyword,
328344 try_read_list,
@@ -498,6 +514,21 @@ mod tests {
498514 }
499515 }
500516
517+ mod try_read_bool_tests {
518+ use crate :: value:: Value ;
519+ use crate :: reader:: try_read_bool;
520+
521+ #[ test]
522+ fn try_read_boolean_true_test ( ) {
523+ assert_eq ! ( Value :: Boolean ( true ) , try_read_bool( "true " ) . ok( ) . unwrap( ) . 1 ) ;
524+ }
525+
526+ #[ test]
527+ fn try_read_boolean_false_test ( ) {
528+ assert_eq ! ( Value :: Boolean ( false ) , try_read_bool( "false " ) . ok( ) . unwrap( ) . 1 ) ;
529+ }
530+ }
531+
501532 mod try_read_symbol_tests {
502533 use crate :: reader:: try_read_symbol;
503534 use crate :: symbol:: Symbol ;
@@ -601,6 +632,22 @@ mod tests {
601632 try_read( "[] " ) . ok( ) . unwrap( ) . 1
602633 ) ;
603634 }
635+
636+ #[ test]
637+ fn try_read_bool_true_test ( ) {
638+ assert_eq ! (
639+ Value :: Boolean ( true ) ,
640+ try_read( "true " ) . ok( ) . unwrap( ) . 1
641+ )
642+ }
643+
644+ #[ test]
645+ fn try_read_bool_false_test ( ) {
646+ assert_eq ! (
647+ Value :: Boolean ( false ) ,
648+ try_read( "false " ) . ok( ) . unwrap( ) . 1
649+ )
650+ }
604651 }
605652
606653 mod consume_clojure_whitespaces_tests {
0 commit comments