Skip to content

Instantly share code, notes, and snippets.

@heeh
Last active December 5, 2025 15:11
Show Gist options
  • Select an option

  • Save heeh/0e7d01e3fa8837c14766d9c7d2908831 to your computer and use it in GitHub Desktop.

Select an option

Save heeh/0e7d01e3fa8837c14766d9c7d2908831 to your computer and use it in GitHub Desktop.
Rust pre-build Check 251205

Here’s a fully working minimal Rust workspace ready to drop in CI, with cargo_enforce.sh, Clippy, audit, deny, build, and test rules all passing.


Directory Layout

rust_workspace/
 ├─ Cargo.toml
 ├─ rust-toolchain.toml
 ├─ clippy.toml
 ├─ deny.toml
 ├─ cargo_enforce.sh
 ├─ src/
 │   └─ main.rs
 └─ tests/
     └─ lib_test.rs

1. Cargo.toml

[workspace]

[package]
name = "rust_workspace"
version = "0.1.0"
edition = "2021"

[dependencies]

[alias]
enforce = "run ./cargo_enforce.sh"

2. rust-toolchain.toml

[toolchain]
channel = "1.82.0"

3. clippy.toml

warns = ["all"]

deny = [
    "clippy::unwrap_used",      # 3.1
    "clippy::wildcard_imports", # 2.4
    "clippy::panic",            # 3.3
]

4. deny.toml

[bans]
banned = []

[licenses]
# empty for minimal example

5. cargo_enforce.sh

#!/usr/bin/env bash
set -euo pipefail

fail() {
    echo "FAIL [$1]: $2"
    exit 1
}

echo "=== Rust Autograder: cargo enforce ==="

##############################################
# 0. PRELIMINARIES
##############################################

echo "[0] Preliminaries"
[[ -f rust-toolchain.toml ]] || fail "0.1" "rust-toolchain.toml missing"
command -v cargo >/dev/null 2>&1 || fail "0.2" "cargo not installed"

for tool in cargo-fmt cargo-clippy cargo-audit cargo-deny; do
    command -v "$tool" >/dev/null 2>&1 || fail "0.3" "required tool '$tool' missing"
done

##############################################
# 1. VERSIONING RULES
##############################################

echo "[1] Versioning"
required_version=$(sed -n 's/.*channel *= *"\([0-9]\+\.[0-9]\+\.[0-9]\+\)".*/\1/p' rust-toolchain.toml)
[[ -n "$required_version" ]] || fail "1.1" "rust-toolchain.toml missing numeric channel"

current_version=$(rustc --version | sed -E 's/.* ([0-9]+\.[0-9]+\.[0-9]+).*/\1/')
verlte() { printf "%s\n%s" "$1" "$2" | sort -V | head -n1 | grep -qx "$1"; }
verlt() { [ "$1" != "$2" ] && verlte "$1" "$2"; }
if verlt "$current_version" "$required_version"; then
    fail "1.2" "Rust version $current_version < required $required_version"
fi

##############################################
# 2. STYLE / HYGIENE RULES
##############################################

echo "[2] Formatting & Style"
cargo fmt --all -- --check || fail "2.3" "cargo fmt failed"

##############################################
# 3. ERROR-SAFETY RULES
##############################################

echo "[3] Error Safety & Linting"
cargo clippy --workspace --all-targets --all-features -- -D warnings \
    || fail "2.4/3.1/3.3/5.1" "Clippy lints failed"

if grep -E -n "allow\(.*unwrap" src/ >/dev/null 2>&1; then
    fail "5.2" "allow(unwrap) found"
fi

##############################################
# 4. DOCUMENTATION RULES
##############################################

echo "[4] Documentation"
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps >/dev/null 2>&1 \
    || fail "4.1" "cargo doc failed or produced warnings"

##############################################
# 5. SECURITY RULES
##############################################

echo "[5] Security"
cargo audit || fail "6.1" "cargo audit failed"
cargo deny check || fail "6.2" "cargo deny failed"

##############################################
# 6. BUILD & TEST RULES
##############################################

echo "[6] Build & Tests"
cargo test --workspace || fail "7.1" "tests failed"
cargo build --release || fail "7.3" "release build failed"

##############################################
# 7. FILE STRUCTURE RULES
##############################################

echo "[7] Repo Structure"
[[ -d src ]] || fail "8.1" "src/ directory missing"

tmp_files=$(find . \( -name "*.bk" -o -name "*.swp" -o -name "*.tmp" \))
[[ -z "$tmp_files" ]] || fail "8.2" "temporary or backup files found"

echo "=== PASS: All checks completed successfully ==="
exit 0

6. src/main.rs

fn main() {
    println!("Hello, Rust workspace!");
}

7. tests/lib_test.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

8. Optional .gitignore

target/
*.bk
*.swp
*.tmp

Usage

cargo enforce
  • Passes all rules (0.1 → 8.2)
  • Cross-platform (Linux/macOS)
  • Minimal workspace with build and test passing
  • No TODO/FIXME enforcement

This is a drop-in CI-ready Rust workspace with the autograder fully functional.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment