1 unstable release
0.1.0 | Apr 12, 2024 |
---|
#2581 in Rust patterns
29KB
367 lines
build-deftly
This crate is a project to test the derive-deftly
macro system
by attempting to clone the derive_builder
crate.
As it stands, I think I have gotten an implementation for every feature that
derive_builder
has, but in some cases I had to give them a different syntax.
I have not tested every combination of features; probably some combinations
will break.
Converting to use this crate.
Instead of this...
use derive_builder::Builder;
#[derive(Builder)]
struct MyStruct {
...
}
Say this:
use derive_deftly::Deftly;
use build_deftly::templates::*;
#[derive(Deftly)]
#[derive_deftly(Builder)]
If you use any #[builder(...)]
options,
you will have to change them to #[deftly(builder(...))]
instead.
Some options have a different form; see below.
Supported options
So far, these options are supported.
(Remember, they go inside #[deftly(builder(...))]
.)
-
pattern = "PAT"
— Supported on fields and structs.PAT
can beimmutable
,owned
, ormutable
(the default). -
setter(skip)
,setter(custom)
— Supported on fields and structs. Note that we do not supportskip=true
,skip=false
,custom=true
, orcustom=false
. Instead ofskip=true
, sayskip
. Instead ofcustom=true
, saycustom
. Instead ofskip=false
orcustom=false
, say "enabled", as insetter(enabled)
. (See derive-deftly#40, derive-deftly#48, derive-deftly#49.) -
vis = "VISIBILITY"
— Supported on fields and structs. By default, builders and setters have the same visibility as the original struct. To override this on the builder and its methods, say (for example)vis="pub"
orvis="pub(crate)
. This replacesderive_builder
'spublic
andprivate
options. -
name = "NAME"
— Supported on structs. Renames the generated builder structure and error enum. -
setter(prefix = "PREFIX")
— Supported on fields and structs. Adds a prefix (with an underscore) to setter methods. -
setter(name = "PREFIX")
— Supported on fields. Rename a single setter method. -
setter(into)
— Supported on fields. Makes a setter generic overV:Into<..>
. -
setter(try_into)
— Supported on fields. Makes a setter generic overV:TryInto<..>
. -
setter(try_into)
— Supported on fields of typeOption<T>
. Makes a setter take aT
instead of anOption<T>
. (When used withinto
, makes the setter takeimpl Into<T>
instead ofimpl Into<Option<T>>
; behaves analogously with try_into.) -
default
,default_val = "EXPR"
— Supported on fields. Unlikederive_builder
, these are separate options, sincederive_deftly
doesn't support options with optional values. (See see derive-deftly#40, derive-deftly#48.) -
build_fn(skip)
— Supported on structs. Skips generation of thebuild()
function. -
build_fn(name = "NAME")
— Supported on structs. Provides a new name for thebuild()
function. -
build_fn(validate = "PATH")
— Supported on structs. Provides a function to run before the rest of the builder, to validate its inputs. The function must take the Builder by reference, and returnResult<_, String>
. -
build_fn(error = "NAME")
— Supported on structs. Provides a user-supplied error type. It must implementFrom<UninitializedFieldError>
. -
derive = "TYPES"
— Supported on structs. Causes the generated Builder to derive the provided list of comma-separated types. Unlike derive_builder, this must be a string. (See derive-deftly#56.) -
struct_attr = "ATTRS"
,impl_attr = "ATTRS"
— Supported on structs. Declares one or more attributes that should be applied verbatim to the builder or to itsimpl
block. These need to include the full attribute syntax, as instruct_attr = "#[must_use]"
. (This differs fromderive_builder
in its name and syntax; see derive-deftly#56.) -
field_attr = "ATTRS"
,setter_attr = "ATTRS"
— Supported on fields. Declares one or more attributes that should be applied verbatim to the field in the builder, or to its setter function. These need to include the full attribute syntax, as infield_attr = "#[serde(default)]")
. (This differs fromderive_builder
in its name and syntax; see derive-deftly#56.) -
field(ty)
— Supported on fields. Overrides the type of the field as stored in the builder. -
field(build_fn)
,field(try_build_fn)
— Supported on fields. Overrides the code used to generate the field from the field stored in the builder. These options take an expression that must evaluate to a function on &self. Thebuild_fn
variant should return the type of the field as it appears in the underlying struct; Thetry_build_fn
should return aResult
. (This differs fromderive_builder
in its name and syntax; see derive-deftly#55.) -
sub_builder
,sub_builder(fn_name)
— Supported on fields. Behaves as inderive-deftly-fork-arti
.
Additional features
We add a Self::builder()
method to your original type
that returns an empty builder.
Dependencies
~1.9–2.6MB
~50K SLoC