Skip to content

Commit 8891d9b

Browse files
Non specific attributes (#222)
1 parent b6a93f0 commit 8891d9b

File tree

15 files changed

+99
-53
lines changed

15 files changed

+99
-53
lines changed

Diff for: confik-macros/src/lib.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ impl FromMeta for FieldTryFrom {
6666
}
6767
}
6868

69-
/// Handles requesting to forward `serde` attributes.
69+
/// Handles requesting to forward attributes.
7070
#[derive(Debug)]
71-
struct ForwardSerde {
71+
struct Forward {
7272
items: Vec<NestedMeta>,
7373
}
7474

75-
impl ToTokens for ForwardSerde {
75+
impl ToTokens for Forward {
7676
fn into_token_stream(self) -> TokenStream {
7777
self.to_token_stream()
7878
}
@@ -83,11 +83,11 @@ impl ToTokens for ForwardSerde {
8383

8484
fn to_token_stream(&self) -> TokenStream {
8585
let Self { items } = self;
86-
quote!(#[serde(#( #items ),*)])
86+
quote!(#( #[ #items ] )*)
8787
}
8888
}
8989

90-
impl FromMeta for ForwardSerde {
90+
impl FromMeta for Forward {
9191
fn from_list(items: &[NestedMeta]) -> darling::Result<Self> {
9292
let items = items.to_vec();
9393

@@ -129,8 +129,8 @@ struct VariantImplementer {
129129
/// Optional explicit override of the variant's discriminant.
130130
discriminant: Option<Expr>,
131131

132-
/// Optional attributes to forward to serde.
133-
forward_serde: Option<ForwardSerde>,
132+
/// Optional attributes to forward to the builder's variant.
133+
forward: Option<Forward>,
134134
}
135135

136136
impl VariantImplementer {
@@ -140,7 +140,7 @@ impl VariantImplementer {
140140
ident,
141141
fields,
142142
discriminant,
143-
forward_serde,
143+
forward,
144144
} = var_impl.as_ref();
145145

146146
let field_vec = fields
@@ -154,7 +154,7 @@ impl VariantImplementer {
154154
.map(|disc| quote_spanned!(disc.span() => = discriminant));
155155

156156
Ok(quote_spanned! { var_impl.span() =>
157-
#forward_serde
157+
#forward
158158
#ident #fields #discriminant
159159
})
160160
}
@@ -330,8 +330,8 @@ struct FieldImplementer {
330330
/// The field type.
331331
ty: Type,
332332

333-
/// Optional attributes to forward to serde.
334-
forward_serde: Option<ForwardSerde>,
333+
/// Optional attributes to forward to the builder's field.
334+
forward: Option<Forward>,
335335
}
336336

337337
impl FieldImplementer {
@@ -380,7 +380,7 @@ impl FieldImplementer {
380380
ty,
381381
ident,
382382
secret,
383-
forward_serde,
383+
forward,
384384
from,
385385
try_from,
386386
..
@@ -414,7 +414,7 @@ impl FieldImplementer {
414414

415415
Ok(quote_spanned! { ident.span() =>
416416
#[serde(default)]
417-
#forward_serde
417+
#forward
418418
#ident #ty
419419
})
420420
}
@@ -623,11 +623,11 @@ struct RootImplementer {
623623
/// `pub`, `pub(crate)`, etc.
624624
vis: Visibility,
625625

626-
/// Optional attributes to forward to serde.
627-
forward_serde: Option<ForwardSerde>,
628-
629-
/// Derives needed by the builder, e.g. `Hash`.
630-
derive: Option<Derive>,
626+
/// Optional attributes to forward to the builder struct/enum.
627+
///
628+
/// This can be serde attributes e.g. `#[confik(forward(serde(default)))]` but also others like
629+
/// `#[confik(forward(derive(Hash)))]`
630+
forward: Option<Forward>,
631631
}
632632

633633
impl RootImplementer {
@@ -665,8 +665,7 @@ impl RootImplementer {
665665
data,
666666
generics,
667667
vis,
668-
forward_serde,
669-
derive: additional_derives,
668+
forward,
670669
..
671670
} = self;
672671

@@ -725,9 +724,9 @@ impl RootImplementer {
725724
let (_impl_generics, type_generics, where_clause) = generics.split_for_impl();
726725

727726
Ok(quote_spanned! { target_name.span() =>
728-
#[derive(::std::default::Default, ::confik::__exports::__serde::Deserialize, #additional_derives )]
727+
#[derive(::std::default::Default, ::confik::__exports::__serde::Deserialize)]
729728
#[serde(crate = "::confik::__exports::__serde")]
730-
#forward_serde
729+
#forward
731730
#vis #enum_or_struct_token #builder_name #type_generics #where_clause
732731
#bracketed_data
733732
#terminator

Diff for: confik-macros/tests/trybuild.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn compile_macros() {
3434
t.compile_fail("tests/trybuild/fail-default-invalid-expr.rs");
3535
t.compile_fail("tests/trybuild/fail-config-name-value.rs");
3636
t.compile_fail("tests/trybuild/fail-secret-extra-attr.rs");
37-
t.compile_fail("tests/trybuild/fail-derive-literal.rs");
37+
t.compile_fail("tests/trybuild/fail-forward-literal.rs");
3838
t.compile_fail("tests/trybuild/fail-field-from-unknown-type.rs");
3939
t.compile_fail("tests/trybuild/fail-uncreatable-type.rs");
4040
t.compile_fail("tests/trybuild/fail-not-a-type.rs");

Diff for: confik-macros/tests/trybuild/19-derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::{BTreeSet, HashSet};
33
use confik::Configuration;
44

55
#[derive(Configuration)]
6-
#[confik(derive(::std::hash::Hash, std::cmp::Ord, PartialOrd, Eq, PartialEq, Clone))]
6+
#[confik(forward(derive(::std::hash::Hash, std::cmp::Ord, PartialOrd, Eq, PartialEq, Clone)))]
77
struct Target {
88
item: usize,
99
}

Diff for: confik-macros/tests/trybuild/23-where-clause.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ impl MyTrait for () {
1313
}
1414

1515
#[derive(Configuration)]
16-
#[confik(forward_serde(bound = "C: MyTrait + DeserializeOwned"))]
16+
#[confik(forward(serde(bound = "C: MyTrait + DeserializeOwned")))]
1717
struct Config<C>
1818
where
1919
C: MyTrait + Default + DeserializeOwned,

Diff for: confik-macros/tests/trybuild/fail-derive-literal.stderr

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[derive(confik::Configuration)]
2-
#[confik(derive("hello world"))]
2+
#[confik(forward("hello world"))]
33
struct A;
44

55
fn main() {}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: expected identifier, found `"hello world"`
2+
--> tests/trybuild/fail-forward-literal.rs:2:18
3+
|
4+
2 | #[confik(forward("hello world"))]
5+
| ^^^^^^^^^^^^^ expected identifier
6+
7+
error: proc-macro derive produced unparsable tokens
8+
--> tests/trybuild/fail-forward-literal.rs:1:10
9+
|
10+
1 | #[derive(confik::Configuration)]
11+
| ^^^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0412]: cannot find type `AConfigBuilder` in this scope
14+
--> tests/trybuild/fail-forward-literal.rs:3:8
15+
|
16+
3 | struct A;
17+
| ^ not found in this scope

Diff for: confik/CHANGELOG.md

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
## Unreleased
44

55
- Implement `Configuration` for [`js_option::JsOption`](https://docs.rs/js_option/0.1.1/js_option/enum.JsOption.html)
6+
- Add a new `confik(forward(...))` attribute. As well as allowing for forwarding general attributes to the builder, this:
7+
- Replaces `confik(forward_serde(...))`. E.g.
8+
```rust
9+
#[derive(Configuration)]
10+
struct Config {
11+
#[confik(forward(serde(default)))]
12+
num: usize,
13+
}
14+
```
15+
- Replaces `confik(derive(...))`. E.g.
16+
```rust
17+
#[derive(Configuration)]
18+
#[confik(forward(derive(Hash)))]
19+
struct Config(usize);
20+
```
621

722
## 0.13.0
823

Diff for: confik/examples/derives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct Config {
99
}
1010

1111
#[derive(Debug, Configuration, Hash, Eq, PartialEq)]
12-
#[confik(derive(Hash, Eq, PartialEq))]
12+
#[confik(forward(derive(Hash, Eq, PartialEq)))]
1313
struct Value {
1414
inner: String,
1515
}

Diff for: confik/src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{Configuration, MissingValue};
66

77
/// The database type, used to determine the connection string format
88
#[derive(Debug, Clone, PartialEq, Eq, Configuration)]
9-
#[confik(forward_serde(rename_all = "lowercase"))]
9+
#[confik(forward(serde(rename_all = "lowercase")))]
1010
enum DatabaseKind {
1111
Mysql,
1212
Postgres,

Diff for: confik/src/lib.md

+31-11
Original file line numberDiff line numberDiff line change
@@ -69,34 +69,54 @@ If a secret is found in an insecure source, an error will be returned. You can o
6969

7070
The derive macro is called `Configuration` and is used as normal:
7171

72-
```
72+
```rust
7373
#[derive(confik::Configuration)]
7474
struct Config {
7575
data: usize,
7676
}
7777
```
7878

79-
### Forwarding Attributes To `Deserialize`
79+
### Forwarding Attributes
80+
81+
This allows forwarding any kind of attribute on to the builder.
8082

81-
The serde attributes used for customizing a `Deserialize` derive typically are achieved by adding `#[confik(forward_serde(...))` attributes.
83+
#### Serde
84+
85+
The serde attributes used for customizing a `Deserialize` derive are achieved by adding `#[confik(forward(serde(...)))]` attributes.
8286

8387
For example:
8488

89+
```rust
90+
# use confik::Configuration;
91+
#[derive(Configuration, Debug, PartialEq, Eq)]
92+
struct Field {
93+
#[confik(forward(serde(rename = "other_name")))]
94+
field1: usize,
95+
}
8596
```
86-
#[derive(confik::Configuration)]
87-
struct Config {
88-
#[confik(forward_serde(rename = "other_data"))]
89-
data: usize,
97+
#### Derives
98+
99+
If you need additional derives for your type, these can be added via `#[confik(forward(derive...))]` attributes.
100+
101+
For example:
102+
103+
```rust
104+
# use confik::Configuration;
105+
#[derive(Debug, Configuration, Hash, Eq, PartialEq)]
106+
#[confik(forward(derive(Hash, Eq, PartialEq)))]
107+
struct Value {
108+
inner: String,
90109
}
91110
```
92111

112+
93113
### Defaults
94114

95115
Defaults are specified on a per-field basis.
96116

97117
- Defaults only apply if no data has been read for that field. E.g., if `data` in the below example has one value read in, it will return an error.
98118

99-
```
119+
```rust
100120
# #[cfg(feature = "toml")]
101121
# {
102122
use confik::{Configuration, TomlSource};
@@ -148,7 +168,7 @@ Defaults are specified on a per-field basis.
148168

149169
- Defaults can be given by any rust expression, and have [`Into::into`] run over them. E.g.,
150170

151-
```
171+
```rust
152172
const DEFAULT_VALUE: u8 = 4;
153173

154174
#[derive(confik::Configuration)]
@@ -164,7 +184,7 @@ Defaults are specified on a per-field basis.
164184

165185
- Alternatively, a default without a given value called [`Default::default`]. E.g.,
166186

167-
```
187+
```rust
168188
use confik::{Configuration};
169189

170190
#[derive(Configuration)]
@@ -193,7 +213,7 @@ This crate provides implementations of [`Configuration`] for a number of `std` t
193213

194214
If there's another foreign type used in your config, then you will not be able to implement [`Configuration`] for it. Instead any type that implements [`Into`] or [`TryInto`] can be used.
195215

196-
```
216+
```rust
197217
struct ForeignType {
198218
data: usize,
199219
}

Diff for: confik/tests/serde_forward/mod.rs renamed to confik/tests/forward/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
use confik::Configuration;
22

33
#[derive(Configuration, Debug, PartialEq, Eq)]
4-
#[confik(forward_serde(rename_all = "UPPERCASE"))]
4+
#[confik(forward(serde(rename_all = "UPPERCASE")))]
55
struct Container {
66
field: usize,
77
}
88

99
#[derive(Configuration, Debug, PartialEq, Eq)]
1010
struct Inner {
11-
#[confik(forward_serde(rename = "outer"))]
11+
#[confik(forward(serde(rename = "outer")))]
1212
inner: usize,
1313
}
1414

1515
#[derive(Configuration, Debug, PartialEq, Eq)]
1616
struct Field {
17-
#[confik(forward_serde(rename = "other_name"))]
17+
#[confik(forward(serde(rename = "other_name")))]
1818
field1: usize,
19-
#[confik(forward_serde(flatten))]
19+
#[confik(forward(serde(flatten)))]
2020
field2: Inner,
2121
}
2222

@@ -25,7 +25,7 @@ enum Clothes {
2525
Hat,
2626
// Put some data in to force use of a custom builder
2727
Scarf(usize),
28-
#[confik(forward_serde(alias = "Gloves", alias = "SomethingElse"))]
28+
#[confik(forward(serde(alias = "Gloves", alias = "SomethingElse")))]
2929
Other,
3030
}
3131

Diff for: confik/tests/keyed_containers/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ macro_rules! create_tests_for {
33
use confik::Configuration;
44

55
#[derive(Debug, Configuration, PartialEq, Eq, Hash, Ord, PartialOrd)]
6-
#[confik(derive(Hash, PartialEq, Eq, Ord, PartialOrd))]
6+
#[confik(forward(derive(Hash, PartialEq, Eq, Ord, PartialOrd)))]
77
struct TwoVals {
88
first: usize,
99
second: usize,

Diff for: confik/tests/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ mod array;
33
mod common;
44
mod complex_enums;
55
mod defaulting_containers;
6+
mod forward;
67
mod keyed_containers;
78
mod option_builder;
89
mod secret;
910
mod secret_option;
10-
mod serde_forward;
1111
mod singly_nested_tests;
1212
mod third_party;
1313
mod unkeyed_containers;
@@ -84,7 +84,7 @@ mod toml {
8484
fn from_humantime() {
8585
#[derive(Debug, PartialEq, Eq, Configuration)]
8686
struct Config {
87-
#[confik(forward_serde(with = "humantime_serde"))]
87+
#[confik(forward(serde(with = "humantime_serde")))]
8888
timeout: Duration,
8989
}
9090

Diff for: confik/tests/unkeyed_containers/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ macro_rules! create_tests_for {
33
use confik::Configuration;
44

55
#[derive(Debug, Configuration, PartialEq, Eq, Hash, Ord, PartialOrd)]
6-
#[confik(derive(Hash, PartialEq, Eq, Ord, PartialOrd))]
6+
#[confik(forward(derive(Hash, PartialEq, Eq, Ord, PartialOrd)))]
77
struct TwoVals {
88
first: usize,
99
second: usize,

0 commit comments

Comments
 (0)