Skip to content

Commit 3f1b9ae

Browse files
authored
Document how to format code (#2375)
* Document how to format code * Fix typo * Fix formatting * Ignore code snippet
1 parent 87b535c commit 3f1b9ae

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [Preventing the Derivation of `Default`](./nodefault.md)
2323
- [Annotating types with `#[must-use]`](./must-use-types.md)
2424
- [Field visibility](./visibility.md)
25+
- [Code formatting](./code-formatting.md)
2526
- [Generating Bindings to C++](./cpp.md)
2627
- [Generating Bindings to Objective-c](./objc.md)
2728
- [Using Unions](./using-unions.md)

book/src/code-formatting.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Code Formatting
2+
3+
`bindgen` uses `rustfmt` to format the emitted bindings. This section describes
4+
how to adjust the `rustfmt` behavior when being used from `bindgen`.
5+
6+
## Passing a `rustfmt.toml` configuration file
7+
8+
`rustfmt` should automatically use any `rustfmt.toml` file that is present in
9+
the directory from where `bindgen` will be run. If you want to use a
10+
configuration file that has a different name or that is in a different
11+
directory you can use the `--rustfmt-configuration-file` flag or the
12+
[`Builder::rustfmt_configuration_file`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.rustfmt_configuration_file)
13+
method.
14+
15+
## Using a nightly release of `rustfmt`
16+
17+
If the `rustfmt` command does not correspond to a nightly release of `rustfmt`
18+
but you have `rustup` available, you can use `nightly` by following these
19+
steps:
20+
21+
### When using `bindgen` as a CLI application
22+
23+
Use `rustup run` to run `bindgen`:
24+
25+
```bash
26+
$ rustup run nightly bindgen [ARGS]
27+
```
28+
29+
### When using `bindgen` as a library
30+
31+
Take the output of the following command:
32+
```bash
33+
$ rustup which rustfmt --toolchain=nightly
34+
```
35+
and pass it to
36+
[`Builder::with_rustfmt`](https://docs.rs/bindgen/latest/bindgen/struct.Builder.html#method.with_rustfmt):
37+
38+
```rust,ignore
39+
use bindgen::Builder;
40+
use std::process::Command;
41+
42+
fn main() {
43+
let output = Command::new("rustup")
44+
.args(["which", "rustfmt", "--toolchain", "nightly"])
45+
.output()
46+
.expect("Could not spawn `rustup` command");
47+
48+
assert!(
49+
output.status.success(),
50+
"Unsuccessful status code when running `rustup`: {:?}",
51+
output
52+
);
53+
54+
let rustfmt_path =
55+
String::from_utf8(output.stdout).expect("The `rustfmt` path is not valid `utf-8`");
56+
57+
let bindings = Builder::default()
58+
.header("path/to/input.h")
59+
.with_rustfmt(rustfmt_path)
60+
.generate()
61+
.expect("Could not generate bindings");
62+
63+
bindings
64+
.write_to_file("path/to/output.rs")
65+
.expect("Could not write bindings");
66+
}
67+
```
68+
69+
These two methods also apply to any other toolchain available in your system.

book/src/faq.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,19 @@ $ bindgen <input_headers> -- --target=armv7a-none-eabi
108108
```
109109
If you are using `bindgen` as a library, you should call
110110
`builder.clang_arg("--target=armv7a-none-eabi")` on your `builder`.
111+
112+
### How can I normalize `#[doc]` attributes?
113+
114+
`bindgen` emits all the documentation using `#[doc]` attributes by default. If
115+
you want to use the more user-friendly `///` syntax, you have to create a
116+
`rustfmt.toml` file with the following contents:
117+
118+
```toml
119+
normalize_doc_attributes = true
120+
```
121+
122+
Then, you have set up bindgen so it passes this file to `rustfmt`. Given that
123+
the `normalize_doc_attributes` option is
124+
[unstable](https://github.com/rust-lang/rustfmt/issues/3351), you also have to
125+
set up bindgen to use a `nightly` release of `rustfmt`. Please check the [code
126+
formatting](./code-formatting.md) section for further information.

0 commit comments

Comments
 (0)