Function field_decl

Source
pub fn field_decl(input: &str) -> IResult<&str, Field>
Expand description

Parse a field declaration, inside a struct, including array dimensions. (doesn’t handle the semicolon or preceding whitespace)

use rust_lcm_codegen::parser::{field_decl, Field, PrimitiveType, Type, StructType, ArrayDimension, ArrayType};

assert_eq!(
    field_decl("int8_t foo"),
    Ok((
        "",
        Field {
            name: "foo".to_owned(),
            ty: Type::Primitive(PrimitiveType::Int8)
        }
    ))
);

assert_eq!(
    field_decl("ns.name foo[dim][2]"),
    Ok((
        "",
        Field {
          name: "foo".to_owned(),
          ty: Type::Array(ArrayType {
            item_type: Box::new(
              Type::Struct(StructType { namespace: Some("ns".to_string()),
                                        name: "name".to_string() })
            ),
            dimensions: vec![
              ArrayDimension::Dynamic { field_name: "dim".to_string() },
              ArrayDimension::Static { size: 2 },
            ]
          })
        }
    ))
);

assert_eq!(field_decl(""), Err(Err::Error(("", ErrorKind::TakeWhile1))));
assert_eq!(field_decl("int8_t *!@"), Err(Err::Error(("*!@", ErrorKind::TakeWhile1))));