Function const_value

Source
pub fn const_value(ty: PrimitiveType, input: &str) -> IResult<&str, ConstValue>
Expand description

Parse a const value of the given type.

use rust_lcm_codegen::parser::{const_value, ConstValue, PrimitiveType};

assert_eq!(const_value(PrimitiveType::Int8,    "42"), Ok(("", ConstValue::Int8(   42))));
assert_eq!(const_value(PrimitiveType::Int8,   "-42"), Ok(("", ConstValue::Int8(  -42))));
assert_eq!(const_value(PrimitiveType::Int8,  "0x2f"), Ok(("", ConstValue::Int8( 0x2f))));
assert_eq!(const_value(PrimitiveType::Int8, "-0x2f"), Ok(("", ConstValue::Int8(-0x2f))));
assert_eq!(const_value(PrimitiveType::Int8,   "022"), Ok(("", ConstValue::Int8( 0o22))));
assert_eq!(const_value(PrimitiveType::Int8,  "-022"), Ok(("", ConstValue::Int8(-0o22))));
assert_eq!(const_value(PrimitiveType::Int8,  "1024"),
           Err(Err::Error(("1024", ErrorKind::MapRes))));

assert_eq!(const_value(PrimitiveType::Int16,  "1024"), Ok(("", ConstValue::Int16( 1024))));
assert_eq!(const_value(PrimitiveType::Int16, "-1024"), Ok(("", ConstValue::Int16(-1024))));
assert_eq!(const_value(PrimitiveType::Int16, "32768"),
           Err(Err::Error(("32768", ErrorKind::MapRes))));

assert_eq!(const_value(PrimitiveType::Int32,  "32768"), Ok(("", ConstValue::Int32( 32768))));
assert_eq!(const_value(PrimitiveType::Int32, "-32768"), Ok(("", ConstValue::Int32(-32768))));
assert_eq!(const_value(PrimitiveType::Int32, "2147483648"),
           Err(Err::Error(("2147483648", ErrorKind::MapRes))));

assert_eq!(const_value(PrimitiveType::Int64,  "2147483648"), Ok(("", ConstValue::Int64( 2147483648))));
assert_eq!(const_value(PrimitiveType::Int64, "-2147483648"), Ok(("", ConstValue::Int64(-2147483648))));
assert_eq!(const_value(PrimitiveType::Int64, "92233720368547758073"),
           Err(Err::Error(("92233720368547758073", ErrorKind::MapRes))));

assert_eq!(const_value(PrimitiveType::Float,           "10"), Ok(("", ConstValue::Float(         "10".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float,        "10.35"), Ok(("", ConstValue::Float(      "10.35".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float,       "-10.35"), Ok(("", ConstValue::Float(     "-10.35".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float,     "10.35e12"), Ok(("", ConstValue::Float(   "10.35e12".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float,    "-10.35e12"), Ok(("", ConstValue::Float(  "-10.35e12".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float,  "10.35e12000"), Ok(("", ConstValue::Float("10.35e12000".to_owned()))));
assert_eq!(const_value(PrimitiveType::Float, "asdf"),
           Err(Err::Error(("asdf", ErrorKind::Digit))));

assert_eq!(const_value(PrimitiveType::Double,           "10"), Ok(("", ConstValue::Double(         "10".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double,        "10.35"), Ok(("", ConstValue::Double(      "10.35".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double,       "-10.35"), Ok(("", ConstValue::Double(     "-10.35".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double,     "10.35e12"), Ok(("", ConstValue::Double(   "10.35e12".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double,    "-10.35e12"), Ok(("", ConstValue::Double(  "-10.35e12".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double,  "10.35e12000"), Ok(("", ConstValue::Double("10.35e12000".to_owned()))));
assert_eq!(const_value(PrimitiveType::Double, "asdf"),
           Err(Err::Error(("asdf", ErrorKind::Digit))));

assert_eq!(const_value(PrimitiveType::Byte,   "42"), Ok(("", ConstValue::Byte( 42))));
assert_eq!(const_value(PrimitiveType::Byte,  "-42"), Err(Err::Error(("-42", ErrorKind::Digit))));
assert_eq!(const_value(PrimitiveType::Byte, "1024"), Err(Err::Error(("1024", ErrorKind::MapRes))));