derpscfg 0.4.0

A brief, incomplete, and mostly wrong derive implementation for scfg
Documentation

derpscfg

A brief, incomplete, and mostly wrong derive implementation for scfg.

A fork of scfg-rs with simple but powerful derive capabilities.

Example

use derpscfg::prelude::*;

#[derive(Debug, Derpscfg)]
pub struct Model {
    #[scfg(param)]
    pub name: String,
    pub max_speed: String,
    pub weight: String,
    pub lines_served: Vec<String>,
}

#[derive(Debug, Derpscfg)]
pub struct Train {
    #[scfg(param)]
    pub name: String,
    pub model: Vec<Model>,
}

#[derive(Debug, Derpscfg)]
pub struct ScfgDemo {
    pub train: Train,
}

fn main() {

    static SCFG_DOC: &str = r#"
train "Shinkansen" {
	model "E5" {
		max-speed 320km/h
		weight 453.5t

		lines-served "Tōhoku" "Hokkaido"
	}

	model "E7" {
		max-speed 275km/h
		weight 540t

		lines-served "Hokuriku" "Jōetsu"
	}
}
"#;

    let demo = derpscfg::parse::<ScfgDemo>(SCFG_DOC).unwrap();
    println!("{demo:?}");
}

Status

Can currently only derive implementations for regular struct types with named fields. Simple enums (without fields) can be used as scalar values. No support for tuples, newtypes, or more complex enums.

Supports parsing to most standard library types that implement FromStr and to Option, Vec, VecDeque, and LinkedList of such types.

Field names with underscores are tried from both field_name and field-name. Overriding field names is not currently implemented. Enum values can be written in both camel or snake case.

Fields annotated with #[scfg(param)] are taken from a directive's parameters in order of declaration. Be careful with cardinality. A Vec will always take all available params, so there can only be one, and it must be the last field from parameters.

The toplevel derpscfg::parse() function can only be used on types that do not declare any fields with param, as the toplevel element does not have any.

If you need the map-style genericity of scfg in parts of your data you can parse a field to derpscfg::Directive (or Option/Vec/etc. thereof).

A list of scalars can come from either repeated directives, or a directive with multiple parameters, hence the following are equivalent:

config {
    value: 5
    value: 6
}

and

config {
    value: 5 6
}

Both would parse fine to a field value: Vec<u64>.

Contributing

Please send patches to the mailing list.

License

MIT OR Apache-2.0