serde_enabled 0.1.0

A small wrapper for enabling/disabling sections in configuartion files easier with Serde.
Documentation
  • Coverage
  • 22.22%
    2 out of 9 items documented1 out of 6 items with examples
  • Size
  • Source code size: 14.07 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 22s Average build duration of successful builds.
  • all releases: 22s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • felipesere

Enable<T> is a wrapper that adds one extra field enabled when the T is serialized with Serde. That field can be true, in which case all of the fields for T need to be present, or it can be false at which point all fields of T can be ommited.

The use case is for configuration YAMLs where sections can be toggled on or off.

use serde::{Deserialize, Serialize};
use serde_enabled::Enable;

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
struct Outside {
    inside: Enable<Inside>,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq)]
struct Inside {
    thing: u32,
    other: String,
}

let raw = indoc::indoc! {r#"
    inside:
        enable: false
        thing: 1
        other: "Great"
    "#};

let o: Outside = serde_yaml::from_str(raw).unwrap();

 assert!(!o.inside.is_enabled());

 let raw = indoc::indoc! {r#"
     inside:
         enable: true
         thing: 1
         other: "Great"
     "#};

 let o: Outside = serde_yaml::from_str(raw).unwrap();

  assert!(o.inside.is_enabled());
  assert_eq!(
        o,
        Outside {
           inside: Enable::On(Inside {
            thing: 1,
            other: "Great".into()
        })
        }
    );