Skip to content

Commit 6a684e6

Browse files
committed
reader supports nil
1 parent bb46283 commit 6a684e6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/reader.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ pub fn try_read_bool(input: &str) -> IResult<&str, Value> {
326326
Ok((rest_input, Value::Boolean(bool.parse().unwrap())))
327327
}
328328

329+
// Tries to parse &str into Value::Nil
330+
/// Expects:
331+
/// nil
332+
/// Example success:
333+
/// nil => Value::Nil
334+
pub fn try_read_nil(input: &str) -> IResult<&str, Value> {
335+
named!(nil_parser<&str,&str>, tag!("nil"));
336+
let (rest_input, nil) = nil_parser(input)?;
337+
Ok((rest_input, Value::Nil))
338+
}
339+
329340
/// Tries to parse &str into Value::double
330341
///
331342
pub fn try_read_f64(input: &str) -> IResult<&str, Value> {
@@ -490,6 +501,7 @@ pub fn try_read(input: &str) -> IResult<&str, Value> {
490501
try_read_f64,
491502
try_read_i32,
492503
try_read_bool,
504+
try_read_nil,
493505
try_read_symbol,
494506
try_read_keyword,
495507
try_read_list,
@@ -698,6 +710,16 @@ mod tests {
698710
}
699711
}
700712

713+
mod try_read_nil_tests {
714+
use crate::reader::try_read_nil;
715+
use crate::value::Value;
716+
717+
#[test]
718+
fn try_read_nil_test() {
719+
assert_eq!(Value::Nil, try_read_nil("nil ").ok().unwrap().1);
720+
}
721+
}
722+
701723
mod try_read_symbol_tests {
702724
use crate::reader::try_read_symbol;
703725
use crate::symbol::Symbol;

0 commit comments

Comments
 (0)