Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(derive): allow naming the builder and setting field visibility #223

Merged
merged 4 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions confik-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ struct FieldImplementer {
/// The field type.
ty: Type,

/// `pub`, `pub(crate)`, etc.
vis: Visibility,

/// Optional attributes to forward to the builder's field.
forward: Option<Forward>,
}
Expand Down Expand Up @@ -383,6 +386,7 @@ impl FieldImplementer {
forward,
from,
try_from,
vis,
..
} = field_impl.as_ref();

Expand Down Expand Up @@ -415,7 +419,7 @@ impl FieldImplementer {
Ok(quote_spanned! { ident.span() =>
#[serde(default)]
#forward
#ident #ty
#vis #ident #ty
})
}

Expand Down Expand Up @@ -628,6 +632,11 @@ struct RootImplementer {
/// This can be serde attributes e.g. `#[confik(forward(serde(default)))]` but also others like
/// `#[confik(forward(derive(Hash)))]`
forward: Option<Forward>,

/// A name to use for the builder.
///
/// Setting this also puts the builder in the local module, so that the name is accessible.
name: Option<Ident>,
}

impl RootImplementer {
Expand Down Expand Up @@ -655,7 +664,11 @@ impl RootImplementer {
///
/// Use [`Self::is_dataless`] first to determine whether a builder will exist.
fn builder_name(&self) -> Ident {
format_ident!("{}ConfigBuilder", self.ident)
if let Some(name) = self.name.as_ref() {
name.clone()
} else {
format_ident!("{}ConfigBuilder", self.ident)
}
}

/// Defines the builder for the target.
Expand Down Expand Up @@ -920,18 +933,35 @@ fn derive_macro_builder_inner(target_struct: &DeriveInput) -> syn::Result<proc_m
)]
};

let full_derive = quote! {
#overall_lint_overrides
const _: () = {
#impl_lint_overrides
#target_impl

let full_derive = if implementer.name.is_some() {
quote! {
#overall_lint_overrides
#struct_lint_overrides
#builder_struct

#impl_lint_overrides
#builder_impl
};
#overall_lint_overrides
const _: () = {
#impl_lint_overrides
#target_impl

#impl_lint_overrides
#builder_impl
};
}
} else {
quote! {
#overall_lint_overrides
const _: () = {
#impl_lint_overrides
#target_impl

#struct_lint_overrides
#builder_struct

#impl_lint_overrides
#builder_impl
};
}
};

Ok(full_derive.into())
Expand Down
6 changes: 5 additions & 1 deletion confik-macros/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ fn compile_macros() {
t.pass("tests/trybuild/22-dataless-types.rs");
t.pass("tests/trybuild/23-where-clause.rs");
t.pass("tests/trybuild/24-field-try-from.rs");
t.pass("tests/trybuild/pass-enum-untagged.rs");
t.pass("tests/trybuild/25-pass-enum-untagged.rs");
t.pass("tests/trybuild/26-named-builder.rs");
t.pass("tests/trybuild/27-field-access.rs");
t.pass("tests/trybuild/28-field-vis.rs");
t.pass("tests/trybuild/29-named-field-vis.rs");

t.compile_fail("tests/trybuild/fail-default-parse.rs");
t.compile_fail("tests/trybuild/fail-default-invalid-expr.rs");
Expand Down
2 changes: 1 addition & 1 deletion confik-macros/tests/trybuild/22-dataless-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct A;
struct B {}

#[derive(Configuration, Debug)]
struct C ();
struct C();

fn main() {
let _builder = A::builder().try_build().expect("No data required");
Expand Down
15 changes: 15 additions & 0 deletions confik-macros/tests/trybuild/26-named-builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Check that we can name and reference a builder.
use confik::ConfigurationBuilder;

#[derive(confik::Configuration, Debug, PartialEq)]
#[confik(name = C)]
struct Config {
#[confik(default)]
param: String,
}

fn main() {
let Config { .. } = C::default()
.try_build()
.expect("Default builder should succeed");
}
16 changes: 16 additions & 0 deletions confik-macros/tests/trybuild/27-field-access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Check that we can reference builder fields
use confik::Configuration;

#[derive(Configuration, Debug, PartialEq)]
struct Config {
#[confik(default)]
param: String,
}

type Builder = <Config as Configuration>::Builder;

fn main() {
let _ = Builder {
param: Default::default(),
};
}
19 changes: 19 additions & 0 deletions confik-macros/tests/trybuild/28-field-vis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! Check that we can reference builder fields in a different module, when they're public

pub mod config {
use confik::Configuration;

#[derive(Configuration, Debug, PartialEq)]
pub struct Config {
#[confik(default)]
pub param: String,
}

pub type Builder = <Config as Configuration>::Builder;
}

fn main() {
let _ = config::Builder {
param: Default::default(),
};
}
18 changes: 18 additions & 0 deletions confik-macros/tests/trybuild/29-named-field-vis.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Check that we can reference builder fields in a different module, when they're public, using the builder's name

pub mod config {
use confik::Configuration;

#[derive(Configuration, Debug, PartialEq)]
#[confik(name = Builder)]
pub struct Config {
#[confik(default)]
pub param: String,
}
}

fn main() {
let _ = config::Builder {
param: Default::default(),
};
}
7 changes: 7 additions & 0 deletions confik/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#[confik(forward(derive(Hash)))]
struct Config(usize);
```
- Add a new `confik(name = ...)` attribute, that provides a custom name for the `Configuration::Builder` `struct` or `enum`.
- This will also place the builder in the local module, so that its name is in a known location
```rust
#[derive(Configuration)]
#[confik(name = Builder)]
struct Config {}
```

## 0.13.0

Expand Down
32 changes: 32 additions & 0 deletions confik/src/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,38 @@ struct Config {
}
```

### Named builders

If you want to directly access the builders, you can provide them with a name. This will also place the builder in the local module, to ensure there's a known path with which to reference them.

```rust
#[derive(confik::Configuration)]
#[confik(name = Builder)]
struct Config {
data: usize,
}

let _ = Builder { data: Default::default() };
```

### Field and Builder visibility

Field and builder visibility are directly inherited from the underlying type. E.g.

```rust
mod config {
#[derive(confik::Configuration)]
pub struct Config {
pub data: usize,
}
}

// Required as you can't use this syntax for struct initialisation.
type Builder = <config::Config as confik::Configuration>::Builder;

let _ = Builder { data: Default::default() };
```

## Macro Limitations

### Custom `Deserialize` Implementations
Expand Down