#deserialize #serialization #serde #utilities #no-alloc

no-std serde-bool

Single value, true or false, boolean deserializers

8 releases

0.1.4 Sep 15, 2025
0.1.3 Dec 1, 2023
0.1.2 Nov 30, 2023
0.0.3 Nov 30, 2023

#361 in Encoding

Download history 208034/week @ 2025-11-11 188594/week @ 2025-11-18 78361/week @ 2025-11-25 132814/week @ 2025-12-02 325013/week @ 2025-12-09 240747/week @ 2025-12-16 44743/week @ 2025-12-23 76677/week @ 2025-12-30 255935/week @ 2026-01-06 329361/week @ 2026-01-13 432967/week @ 2026-01-20 356993/week @ 2026-01-27 278662/week @ 2026-02-03 254059/week @ 2026-02-10 289055/week @ 2026-02-17 290371/week @ 2026-02-24

1,183,120 downloads per month
Used in 5 crates (3 directly)

MIT/Apache

12KB
166 lines

serde-bool

crates.io Documentation dependency status MIT or Apache 2.0 licensed
CI codecov Version Download

Single value, true or false, boolean deserializers.

Examples

Supporting serde untagged enums where only one boolean value is valid, allowing fallthrough to the next variant. Avoids need to wrap all fields in Option<_> just in case feature is disabled.

#[derive(Debug, serde::Deserialize)]
struct Config {
    feature: FeatureConfig,
}

#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
enum FeatureConfig {
    Disabled {
        enabled: serde_bool::False
    },

    Enabled {
        #[serde(default)]
        enabled: serde_bool::True,
        key: String,
        secret: String,
    }
}

// disabled variant is matched
let config = toml::from_str::<Config>(r#"
    [feature]
    enabled = false
"#).unwrap();
assert!(matches!(config.feature, FeatureConfig::Disabled { .. }));

// if the type used `enabled: bool`, this would cause issues and require Option<_> wrappers plus
// further validation... instead an error is returned immediately regarding the missing fields
let config = toml::from_str::<Config>(r#"
    [feature]
    enabled = true
"#).unwrap_err();

// using a `#[serde(default)]` annotation makes `enabled = true` optional here
let config = toml::from_str::<Config>(r#"
    [feature]
    key = "foo"
    secret = "bar"
"#).unwrap();
assert!(matches!(config.feature, FeatureConfig::Enabled { .. }));

// extra keys can exists in the disabled case, but as usual will not be captured
let config = toml::from_str::<Config>(r#"
    [feature]
    enabled = false
    key = "foo"
    secret = "bar"
"#).unwrap();
assert!(matches!(config.feature, FeatureConfig::Disabled { .. }));

Dependencies

~130–425KB