diff --git a/Makefile b/Makefile index 538f7568..fccd9743 100644 --- a/Makefile +++ b/Makefile @@ -130,8 +130,11 @@ docs: @echo "" # Add a blank line @cd docs && mdbook build # Build mdBook +format: + ./tools/format_all.sh + check: - @cargo fmt --check # Check Rust format issues + ./tools/format_all.sh --check # Check Rust format issues @cargo kclippy -- -D warnings # Make build fail if any warnings are found by rustc and clippy clean: diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 00000000..7a416e7b --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +imports_granularity="Crate" +group_imports="StdExternalCrate" +reorder_imports=true \ No newline at end of file diff --git a/tools/format_all.sh b/tools/format_all.sh new file mode 100755 index 00000000..c040c25b --- /dev/null +++ b/tools/format_all.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# SPDX-License-Identifier: MPL-2.0 + +WORKSPACE_ROOT="$(dirname "$(readlink -f "$0")")/.." + +EXCLUDED_CRATES=$(sed -n '/^\[workspace\]/,/^\[.*\]/{/exclude = \[/,/\]/p}' "$WORKSPACE_ROOT/Cargo.toml" | grep -v "exclude = \[" | tr -d '", \]') + + +CHECK_MODE=false + +if [ "$#" -eq 1 ]; then + if [ "$1" == "--check" ]; then + CHECK_MODE=true + else + echo "Error: Invalid argument. Only '--check' is allowed." + exit 1 + fi +elif [ "$#" -gt 1 ]; then + echo "Error: Too many arguments. Only '--check' is allowed." + exit 1 +fi + +cd $WORKSPACE_ROOT +if [ "$CHECK_MODE" = true ]; then + cargo fmt --check +else + cargo fmt +fi + +for CRATE in $EXCLUDED_CRATES; do + CRATE_DIR="$WORKSPACE_ROOT/$CRATE" + + # `cargo-component` crate currently is pinned to use Rust nightly-2023-02-05 version, + # and when using this script in the current Docker environment, it will + # additionally download this version of Rust. + # Here temporarily skip processing this crate for now considering that this crate + # is not currently in use or under development. + case "$CRATE" in + *cargo-component*) + continue + ;; + esac + + if [ -d "$CRATE_DIR" ]; then + if [ "$CHECK_MODE" = true ]; then + (cd "$CRATE_DIR" && cargo fmt --check) + else + (cd "$CRATE_DIR" && cargo fmt) + fi + else + echo "Directory for crate $CRATE does not exist" + fi +done +