Attributes

Attributes are used to customize the Deserr implementations produced by deserr's derive.

There are three categories of attributes:

use deserr::Deserr;

#[derive(Deserr)]
#[deserr(deny_unknown_fields)]  // <-- this is a container attribute
struct S {
    #[deserr(default)]  // <-- this is a field attribute
    f: i32,
}

#[derive(Deserr)]
#[deserr(rename_all = camelCase)]  // <-- this is also a container attribute
enum E {
    #[deserr(rename = "_deserr")]  // <-- this is a variant attribute
    DeserrIsGreat,
    SerdeIsAwesome
}

fn main() {}

Note that a single struct, enum, variant, or field may have multiple attributes on it.

Feature comparison table with serde

Datastructure support

datastructureserdedeserrnote
Structyesyes
Tuple structyesno
Untagged Enumyesno
Untagged unit Enumyesyes
Tagged Enumyesyes

Container attributes

featuresserdedeserrnote
renameyesno
rename_allyesyes
deny_unknown_fieldsyesyesWith deserr you can call a custom function when an unknown field is encountered
tagyesyes
tag+contentyesno
untaggedyesnoit's only supported for unit enums
boundyesnoCan be emulated with where_predicate
defaultyesno
remoteyesno
transparentyesno
fromyesyes
try_fromyesyes
intoyesno
crateyesno
validatenoyesAllows you to validate the content of struct after it has been deserialized
errornoyesSpecify the error type that should be used while deserializing this structure
where_predicatenoyesLet you add where clauses to the generated Deserr implementation

Field attributes

featuresserdedeserrnote
renameyesyes
aliasyesno
defaultyesyes
flattenyesnoserde doesn't support flattening + denying unknown field
skipyesyes
deserialize_withyesnoBut it's kinda emulated with from and try_from
withyesno
borrowyesnodeserr does not support types with references
boundyesno
mapnoyesAllows you to map the value after it was deserialized
fromnoyesDeserialize this field from an infallible function
try_fromnoyesDeserialize this field from a fallible function
missing_field_errornoyesAllows you to return a custom error if this field is missing
errornoyesSpecify the error type that should be used while deserializing this field