Expand description
If serde
turns your structs into markup files,
then edres
turns your markup files into structs.
This crate has three main related functionalities:
- Generate a set of structs representing the contents of a single markup file.
- Generate an enum to represent the keys of a map in a single markup file (and optionally structs to represent the values).
- Generate an enum to represent the files of a directory (and optionally structs to represent the contents of those files).
The crate is mainly intended to be used in build.rs
build
scripts, but could also be used inside a proc-macro. (See the
codegen
module.)
To use this crate, make sure you enable at least one of the serde features to support the kinds of files you’re using:
json
toml
yaml
There are two sets of functions provided at the top level:
the create_
functions which will write a Rust source file,
and the generate_
functions which simply return Rust code as
a string.
§Examples
§Generating structs
This example takes a single config file and generates a struct that is compatible with it:
let my_file_content = "
title = \"My TOML file\"
[my_table]
value = 10
";
let generated_code = generate_structs_from_source(
my_file_content,
"MyStruct",
Format::Toml,
&Options::minimal(),
).unwrap();
assert_eq!(generated_code, quote!(
#[allow(non_camel_case_types)]
pub struct MyStruct {
pub title: std::borrow::Cow<'static, str>,
pub my_table: MyStruct__my_table,
}
#[allow(non_camel_case_types)]
pub struct MyStruct__my_table {
pub value: i64,
}
).to_string());
§Generating enums
This example takes a single config file and generates an enum for the keys of the file, and structs for the values:
let my_file_content = "
Key1:
name: First key
Key2:
name: Second key
";
let generated_code = generate_enum_from_source(
my_file_content,
"MyEnum",
Format::Yaml,
&Options {
enums: EnumOptions {
values_struct: Some(ValuesStructOptions::minimal()),
..EnumOptions::minimal()
},
..Options::minimal()
},
).unwrap();
assert_eq!(generated_code, quote!(
pub enum MyEnum {
Key1,
Key2,
}
#[allow(non_camel_case_types)]
pub struct MyEnum__Value {
pub name: std::borrow::Cow<'static, str>,
}
).to_string());
§Generating file enums
This example a directory full of JSON files and generates an enum variant for each file name, and a struct to represent the contents of them all.
/* Example JSON file:
{
"name": "file1.json",
"number": 1
}
*/
let generated_code = generate_enum_from_filenames(
"./dir_full_of_json_files",
"MyFileEnum",
&Options {
enums: EnumOptions {
values_struct: Some(ValuesStructOptions::minimal()),
..EnumOptions::minimal()
},
..Options::minimal()
},
).unwrap();
assert_eq!(generated_code, quote!(
pub enum MyFileEnum {
File1,
File2,
File3,
}
#[allow(non_camel_case_types)]
pub struct MyFileEnum__Value {
pub name: std::borrow::Cow<'static, str>,
pub number: i32,
}
).to_string());
Modules§
- codegen
- The purpose of this module is to convert generic
Values
to Rust structs and enums. - options
- This module contains the structs for configuring the behaviour of the public APIs.
- parsing
- This module contains all of the utilities for parsing markup files.
- value
- Contains the
Value
type - a markup-agnostic generic value.
Structs§
- Enum
Options - Options specific to how
edres
should generate enums. - Files
Options - Options specific to how
edres
should handle input files. - Options
- Contains the full set of options for all public APIs in this crate.
- Output
Options - Options specific to how
edres
should handle its output. - Parse
Options - Options specific to how
edres
should parse markup. - Struct
Options - Options specific to how
edres
should generate structs. - Values
Struct Options - Options specific to how
edres
should generate structs for values associated with enum variants.
Enums§
- Error
- An error type for errors while generating config struct modules.
- Float
Size - Used to specify the default size of floating point values (providing they fit within the given size).
- Format
- Represents an input markup format for a config file.
- IntSize
- Used to specify the default size of integer values (providing they fit within the given size).
- Serde
Support - Options for serde support.
Functions§
- create_
enum - Create a Rust source file that defines an enum based on the map keys of the given markup file.
- create_
enum_ from_ filenames - Create a Rust source file that defines an enum based on the file names within the given directory.
- create_
enum_ from_ source - Create a Rust source file that defines an enum based on the map keys of the given markup source.
- create_
structs - Create a Rust source file that defines a set of structs based on a given markup file.
- create_
structs_ from_ files - Create a Rust source file that defines a set of structs based on the contents of the files in the given directory.
- create_
structs_ from_ source - Create a Rust source file that defines a set of structs based on the given markup source.
- generate_
enum - Generate Rust code that defines an enum based on the map keys of the given markup file.
- generate_
enum_ from_ filenames - Generate Rust code that defines an enum based on the file names within the given directory.
- generate_
enum_ from_ source - Generate Rust code that defines an enum based on the map keys of the given markup source.
- generate_
structs - Generate Rust code that defines a set of structs based on a given markup file.
- generate_
structs_ from_ files - Generate Rust code that defines a set of structs based on the contents of the files in the given directory.
- generate_
structs_ from_ source - Generate Rust code that defines a set of structs based on the given markup source.