Skip to content

Commit 2a107e3

Browse files
committed
Rust API: Apply cargo fmt and integrate rustfmt into bazel test suite
Run rustfmt across all crates for consistent formatting. Add a rustfmt_test target using the bazel Rust toolchain's rustfmt binary, integrated into the test suite so formatting is enforced automatically.
1 parent e479b76 commit 2a107e3

28 files changed

+900
-521
lines changed

lib/rust/BUILD

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# See moteus-protocol/ for the no_std protocol crate
1919
# See moteus/ for the full client library with transports
2020

21-
load("@rules_rust//rust:defs.bzl", "rust_binary")
21+
load("@rules_rust//rust:defs.bzl", "rust_binary", "rustfmt_test")
2222

2323
test_suite(
2424
name = "host",
@@ -30,6 +30,7 @@ test_suite(
3030
"//lib/rust/moteus:moteus-doc-test-all-features",
3131
"//lib/rust:clippy",
3232
"//lib/rust:rustdoc",
33+
"//lib/rust:fmt",
3334
],
3435
)
3536

@@ -46,6 +47,17 @@ sh_test(
4647
size = "small",
4748
)
4849

50+
# Formatting check using the bazel Rust toolchain's rustfmt.
51+
rustfmt_test(
52+
name = "fmt",
53+
targets = [
54+
"//lib/rust/moteus-derive",
55+
"//lib/rust/moteus-protocol",
56+
"//lib/rust/moteus:moteus",
57+
],
58+
size = "small",
59+
)
60+
4961
# Rustdoc lint checks (warnings as errors).
5062
sh_test(
5163
name = "rustdoc",

lib/rust/moteus-derive/src/lib.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use proc_macro::TokenStream;
2121
use quote::quote;
22-
use syn::{parse_macro_input, DeriveInput, Data, Fields};
22+
use syn::{parse_macro_input, Data, DeriveInput, Fields};
2323

2424
/// Derives builder-style setter methods for struct fields.
2525
///
@@ -77,60 +77,67 @@ pub fn derive_setters(input: TokenStream) -> TokenStream {
7777
let methods = match &input.data {
7878
Data::Struct(data) => match &data.fields {
7979
Fields::Named(fields) => {
80-
fields.named.iter().filter_map(|f| {
81-
let field_name = f.ident.as_ref()?;
82-
let field_ty = &f.ty;
80+
fields
81+
.named
82+
.iter()
83+
.filter_map(|f| {
84+
let field_name = f.ident.as_ref()?;
85+
let field_ty = &f.ty;
8386

84-
// Parse field attributes
85-
let mut skip = false;
86-
let mut use_into = false;
87-
let mut use_raw = false;
87+
// Parse field attributes
88+
let mut skip = false;
89+
let mut use_into = false;
90+
let mut use_raw = false;
8891

89-
for attr in &f.attrs {
90-
if attr.path().is_ident("setters") {
91-
let _ = attr.parse_nested_meta(|meta| {
92-
if meta.path.is_ident("skip") {
93-
skip = true;
94-
} else if meta.path.is_ident("into") {
95-
use_into = true;
96-
} else if meta.path.is_ident("raw") {
97-
use_raw = true;
98-
}
99-
Ok(())
100-
});
92+
for attr in &f.attrs {
93+
if attr.path().is_ident("setters") {
94+
let _ = attr.parse_nested_meta(|meta| {
95+
if meta.path.is_ident("skip") {
96+
skip = true;
97+
} else if meta.path.is_ident("into") {
98+
use_into = true;
99+
} else if meta.path.is_ident("raw") {
100+
use_raw = true;
101+
}
102+
Ok(())
103+
});
104+
}
101105
}
102-
}
103106

104-
if skip {
105-
return None;
106-
}
107+
if skip {
108+
return None;
109+
}
107110

108-
// Auto-detect Option<T> types (unless raw is specified)
109-
let is_option = !use_raw && is_option_type(field_ty);
111+
// Auto-detect Option<T> types (unless raw is specified)
112+
let is_option = !use_raw && is_option_type(field_ty);
110113

111-
let (param_ty, value_expr) = if is_option {
112-
let inner_ty = extract_option_inner_type(field_ty);
113-
if use_into {
114-
(quote! { impl Into<#inner_ty> }, quote! { Some(value.into()) })
114+
let (param_ty, value_expr) = if is_option {
115+
let inner_ty = extract_option_inner_type(field_ty);
116+
if use_into {
117+
(
118+
quote! { impl Into<#inner_ty> },
119+
quote! { Some(value.into()) },
120+
)
121+
} else {
122+
(quote! { #inner_ty }, quote! { Some(value) })
123+
}
124+
} else if use_into {
125+
(quote! { impl Into<#field_ty> }, quote! { value.into() })
115126
} else {
116-
(quote! { #inner_ty }, quote! { Some(value) })
117-
}
118-
} else if use_into {
119-
(quote! { impl Into<#field_ty> }, quote! { value.into() })
120-
} else {
121-
(quote! { #field_ty }, quote! { value })
122-
};
127+
(quote! { #field_ty }, quote! { value })
128+
};
123129

124-
let method = quote! {
125-
#[must_use]
126-
pub fn #field_name(mut self, value: #param_ty) -> Self {
127-
self.#field_name = #value_expr;
128-
self
129-
}
130-
};
130+
let method = quote! {
131+
#[must_use]
132+
pub fn #field_name(mut self, value: #param_ty) -> Self {
133+
self.#field_name = #value_expr;
134+
self
135+
}
136+
};
131137

132-
Some(method)
133-
}).collect::<Vec<_>>()
138+
Some(method)
139+
})
140+
.collect::<Vec<_>>()
134141
}
135142
_ => vec![],
136143
},

0 commit comments

Comments
 (0)