Skip to content

Commit a4b4be2

Browse files
authored
Init Action
1 parent 9605291 commit a4b4be2

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

action.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: "Check formatting of Rust code with rustfmt"
2+
description: |
3+
Run `cargo fmt` and check Rust code.
4+
Highlights places which are not correctly formatted.
5+
branding:
6+
icon: "check-square"
7+
color: "yellow"
8+
9+
runs:
10+
using: composite
11+
steps:
12+
- name: "Install rustfmt Problem Matcher"
13+
shell: bash
14+
run: echo "::add-matcher::${{ github.action_path }}/rust.json"
15+
- name: Rustfmt Job Summary
16+
shell: bash
17+
run: |
18+
# Run cargo and store the original output
19+
CARGO_STATUS=0
20+
CARGO_OUTPUT=$(cargo fmt --all -- --color=always --check 2>/dev/null) || CARGO_STATUS=$?
21+
22+
if [ ${CARGO_STATUS} -eq 0 ]; then
23+
cat <<MARKDOWN_INTRO >> $GITHUB_STEP_SUMMARY
24+
# Rustfmt Results
25+
26+
The code is formatted perfectly!
27+
MARKDOWN_INTRO
28+
else
29+
cat <<MARKDOWN_INTRO >> $GITHUB_STEP_SUMMARY
30+
# Rustfmt Results
31+
32+
\`cargo fmt\` reported formatting errors in the following locations.
33+
You can fix them by executing the following command and committing the changes.
34+
\`\`\`bash
35+
cargo fmt --all
36+
\`\`\`
37+
MARKDOWN_INTRO
38+
39+
echo "${CARGO_OUTPUT}" |
40+
# Strip color codes
41+
sed 's/\x1B\[[0-9;]*[A-Za-z]//g' |
42+
# Strip (some) cursor movements
43+
sed 's/\x1B.[A-G]//g' |
44+
tr "\n" "\r" |
45+
# Wrap each location into a HTML details
46+
sed -E 's#Diff in ([^\r]*?) at line ([[:digit:]]+):\r((:?[ +-][^\r]*\r)+)#<details>\n<summary>\1:\2</summary>\n\n```diff\n\3```\n\n</details>\n\n#g' |
47+
tr "\r" "\n" >> $GITHUB_STEP_SUMMARY
48+
fi
49+
50+
# Print the original cargo message
51+
echo "${CARGO_OUTPUT}"
52+
# Exit with the same status as cargo
53+
exit "${CARGO_STATUS}"

rust.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "rustfmt",
5+
"severity": "warning",
6+
"pattern": [
7+
{
8+
"regexp": "^(Diff in (.+)) at line (\\d+):$",
9+
"message": 1,
10+
"file": 2,
11+
"line": 3
12+
}
13+
]
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)