facet-default

Crates.iofacet-default
lib.rsfacet-default
version0.43.2
created_at2025-12-29 20:37:52.806577+00
updated_at2026-01-23 18:04:12.188035+00
descriptionDerive Default for facet types with custom field defaults
homepage
repositoryhttps://github.com/facet-rs/facet
max_upload_size
id2011303
size23,625
Amos Wenger (fasterthanlime)

documentation

README

facet-default

Coverage Status crates.io documentation MIT/Apache-2.0 licensed Discord

facet-default

Derive Default for your types using facet's plugin system with custom field defaults.

Usage

use facet::Facet;
use facet_default as default;

#[derive(Facet, Debug)]
#[facet(derive(Default))]
pub struct Config {
    #[facet(default::value = "localhost")]
    host: String,
    #[facet(default::value = 8080u16)]
    port: u16,
    #[facet(default::func = "default_timeout")]
    timeout: std::time::Duration,
    // No attribute = uses Default::default()
    debug: bool,
}

fn default_timeout() -> std::time::Duration {
    std::time::Duration::from_secs(30)
}

Attributes

Field Level

  • #[facet(default::value = literal)] - Use a literal value (converted via .into())
  • #[facet(default::func = "path")] - Call a function to get the default value

Fields without attributes use Default::default().

Note: For numeric literals, use type suffixes to ensure correct types (e.g., 8080u16 instead of 8080 for a u16 field). String literals are automatically converted via .into().

Enums

For enums, mark the default variant with #[facet(default::variant)]:

use facet::Facet;
use facet_default as default;

#[derive(Facet, Debug, PartialEq)]
#[facet(derive(Default))]
#[repr(u8)]
pub enum Status {
    #[facet(default::variant)]
    Pending,
    Active,
    Done,
}

assert_eq!(Status::default(), Status::Pending);

Enum variants with fields also work - fields use their own default attributes:

use facet::Facet;
use facet_default as default;

#[derive(Facet, Debug)]
#[facet(derive(Default))]
#[repr(u8)]
pub enum Request {
    #[facet(default::variant)]
    Get {
        #[facet(default::value = "/")]
        path: String,
        #[facet(default::value = 80u16)]
        port: u16,
    },
    Post { path: String, body: String },
}

Sponsors

Thanks to all individual sponsors:

GitHub Sponsors Patreon

...along with corporate sponsors:

AWS Zed Depot

...without whom this work could not exist.

Special thanks

The facet logo was drawn by Misiasart.

License

Licensed under either of:

at your option.

Commit count: 3380

cargo fmt