You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/src/guide/basics.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,6 @@
2
2
3
3
Welcome to the Bon Guide Book 🍬! It's meant to walk you through `bon` so you don't get lost while exploring it 🐈.
4
4
5
-
Code examples may use either `fn` or `struct` builder syntax interchangeably. Both follow the same principles unless stated otherwise.
5
+
Some code examples have tabs for "Struct", "Function" and "Method" syntax. Some examples may not have them, but it doesn't necessarily mean the described feature works only with one kind of syntax.
6
6
7
7
Now, let's begin with the first topic of [Optional Members](./basics/optional-members).
Copy file name to clipboardExpand all lines: website/src/guide/basics/custom-conversions.md
+54-3Lines changed: 54 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@
4
4
5
5
You can pass a custom closure to `#[builder(with)]`. It will define the signature of the setter and perform a conversion.
6
6
7
-
```rust
8
-
usebon::Builder;
7
+
::: code-group
9
8
9
+
```rust [Struct]
10
10
structPoint {
11
11
x:u32,
12
12
y:u32,
13
13
}
14
14
15
-
#[derive(Builder)]
15
+
#[derive(bon::Builder)]
16
16
structExample {
17
17
#[builder(with =|x:u32, y:u32|Point { x, y })] // [!code highlight]
18
18
point:Point,
@@ -26,6 +26,57 @@ assert_eq!(value.point.x, 2);
26
26
assert_eq!(value.point.y, 3);
27
27
```
28
28
29
+
```rust [Function]
30
+
structPoint {
31
+
x:u32,
32
+
y:u32,
33
+
}
34
+
35
+
#[bon::builder]
36
+
fnexample(
37
+
#[builder(with=|x:u32, y:u32|Point { x, y })] // [!code highlight]
38
+
point:Point,
39
+
) ->Point {
40
+
point
41
+
}
42
+
43
+
letvalue=example()
44
+
.point(2, 3) // [!code highlight]
45
+
.call();
46
+
47
+
assert_eq!(value.x, 2);
48
+
assert_eq!(value.y, 3);
49
+
```
50
+
51
+
```rust [Method]
52
+
structPoint {
53
+
x:u32,
54
+
y:u32,
55
+
}
56
+
57
+
structExample;
58
+
59
+
#[bon::bon]
60
+
implExample {
61
+
#[builder]
62
+
fnexample(
63
+
#[builder(with=|x:u32, y:u32|Point { x, y })] // [!code highlight]
64
+
point:Point,
65
+
) ->Point {
66
+
point
67
+
}
68
+
}
69
+
70
+
letvalue=Example::example()
71
+
.point(2, 3) // [!code highlight]
72
+
.call();
73
+
74
+
assert_eq!(value.x, 2);
75
+
assert_eq!(value.y, 3);
76
+
```
77
+
78
+
:::
79
+
29
80
You can make the setter fallible by passing a [fallible closure](../../reference/builder/member/with#fallible-closure).
30
81
31
82
You can pass one of [well-known functions](../../reference/builder/member/with#well-known-functions) instead of a closure to `#[builder(with)]`. If any of them fit your use, this will save you some characters to type.
// This will output the current state of the builder to `stderr`
74
+
dbg!(&builder);
75
+
76
+
// You can also format the debug output to `String`:
77
+
assert_eq!(
78
+
format!("{builder:?}"),
79
+
// Only the fields that were set will be output
80
+
r#"ExampleMethodBuilder { name: "Bon" }"#
81
+
);
82
+
83
+
// Finish building
84
+
builder.is_admin(true).call();
85
+
```
86
+
87
+
:::
88
+
33
89
You can also derive the `Clone` and `Into` traits for your builder using this same attribute. See more details in the [reference for the `#[builder(derive(...))]` attribute](../../reference/builder/top-level/derive).
Copy file name to clipboardExpand all lines: website/src/guide/basics/documenting.md
+41-12Lines changed: 41 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,9 @@
2
2
3
3
In regular Rust, it's not possible to place doc comments on function arguments. But with `#[builder]` it is. Documentation written on the arguments will be placed on the generated setter methods.
4
4
5
-
**Example:**
6
-
7
5
````rust
8
-
usebon::builder;
9
-
10
6
/// Function that returns a greeting special-tailored for a given person
11
-
#[builder]
7
+
#[bon::builder]
12
8
fngreet(
13
9
/// Name of the person to greet.
14
10
///
@@ -33,26 +29,59 @@ This works because Rust compiler checks for invalid placement of `#[doc = ...]`
33
29
34
30
When `#[derive(Builder)]` is placed on top of a struct, then documentation on the struct fields will be copied to the docs on the setter methods.
35
31
36
-
## Custom `doc`attributes
32
+
## Custom `doc`Attributes
37
33
38
34
You can override documentation on other items generated by builder macros. There are multiple attributes accepting a `doc { ... }` block.
39
35
40
-
```rust
41
-
usebon::Builder;
36
+
::: code-group
42
37
43
-
#[derive(Builder)]
38
+
```rust [Struct]
39
+
#[derive(bon::Builder)]
44
40
#[builder(
45
41
builder_type(doc {
46
42
/// Custom docs on the builder struct itself
47
43
}),
48
-
start_fn(doc {
49
-
/// Custom docs on the starting function
44
+
finish_fn(doc {
45
+
/// Custom docs on the finishing function
50
46
}),
51
47
// ...
52
48
)]
53
49
structExample {}
54
50
```
55
51
52
+
```rust [Function]
53
+
#[bon::builder(
54
+
builder_type(doc {
55
+
/// Custom docs on the builder struct itself
56
+
}),
57
+
finish_fn(doc {
58
+
/// Custom docs on the finishing function
59
+
}),
60
+
// ...
61
+
)]
62
+
fnexample() {}
63
+
```
64
+
65
+
```rust [Method]
66
+
structExample;
67
+
68
+
#[bon::bon]
69
+
implExample {
70
+
#[builder(
71
+
builder_type(doc {
72
+
/// Custom docs on the builder struct itself
73
+
}),
74
+
finish_fn(doc {
75
+
/// Custom docs on the finishing function
76
+
}),
77
+
// ...
78
+
)]
79
+
fnexample() {}
80
+
}
81
+
```
82
+
83
+
:::
84
+
56
85
You can document the following items this way:
57
86
58
87
| Attribute | Documentation target |
@@ -64,6 +93,6 @@ You can document the following items this way:
64
93
|[`setters`](../../reference/builder/member/setters#doc)| Custom docs for setters. Prevents copying them from the field/argument |
65
94
|[`getter`](../../reference/builder/member/getter#doc)| Custom docs for a getter. Prevents copying them from the field/argument |
66
95
67
-
## Positional members
96
+
## Positional Members
68
97
69
98
Documentation comments are allowed on [positional members](./positional-members). However, since there are no separate setter methods generated for them, the docs on these members will not be copied anywhere, and thus they won't appear in `rustdoc`. Instead, it's recommended to write documentation for these members on the top level of the struct or function.
If you have members of type `String`, or `PathBuf`, and you need to set them to a hard-coded string literal, then you have to write `.to_owned()` or `.to_string()` or `.into()`.
However, you can ask `bon` to generate setters that accept `impl Into<T>` to remove the need for manual conversion.
24
66
25
67
This can be configured with [`#[builder(into)]`](../../reference/builder/member/into) for a single member or with [`#[builder(on({type}, into))]`](../../reference/builder/top-level/on) for many members at once.
26
68
27
-
```rust
28
-
usebon::Builder;
69
+
::: code-group
70
+
71
+
```rust [Struct]
29
72
usestd::path::PathBuf;
30
73
31
74
// All setters for members of type `String` will accept `impl Into<String>` // [!code highlight]
`Into` conversions don't always make sense, and you should be aware of their downsides as well. The article [Into Conversions In-Depth](../patterns/into-conversions-in-depth) provides recommendations on when it makes sense to use and to avoid `Into` conversions.
0 commit comments