pub fn define_structs(
    data: &Struct,
    struct_name: &str,
    source_file_path: Option<&Path>,
    options: &Options
) -> Result<TokenStream, Error>
Expand description

Define a set of Rust structs based on the given value.

While you can manually create a Struct, the intended way to use this crate is to either use the functions in the root of this crate, or use the parsing module to read Structs from markup files.

Examples

let tokens = codegen::define_structs(
    &Struct::from_pairs([
        ("number", Value::I32(10)),
        ("string", Value::String("ten".into())),
        ("nested", Value::Struct(
            Struct::from_pairs([
                ("boolean", Value::Bool(true)),
            ])
        )),
    ]),
    "StructName",
    None,
    &Options::minimal(),
).unwrap();

assert_eq!(tokens.to_string(), quote!(
    #[allow(non_camel_case_types)]
    pub struct StructName {
        pub number: i32,
        pub string: std::borrow::Cow<'static, str>,
        pub nested: StructName__nested,
    }

    #[allow(non_camel_case_types)]
    pub struct StructName__nested {
        pub boolean: bool,
    }
).to_string());