Skip to content

Standized code formatting via rustfmt #1155

@Gijsreyn

Description

@Gijsreyn

Summary of the new feature / enhancement

As a contributor to the DSC repository,
I want to have a clear and consistent Rust code style across the project,
So that:

  • Pull request focuses on behavior changes rather than style arguments.
  • All code "looks like it came from the same author."
  • CI can automatically detect style violations and reject PRs that don't conform
  • New contributors have less friction

Currently, formatting style is ad hoc. This causes friction in reviews, occasional style comment debates, and some churn when refactoring.

I propose we adopt rustfmt with a small, curated rustfmt.toml config file and enforce it in the CI (separate or Pester tests) with cargo fmt --check.

Proposed formatting policy

  1. Use rustfmt as the canonical formatter:

    • Developers should run cargo fmt locally (or via IDE) before committing.
    • The project includes a rustfmt.toml file with agreed-upon overrides of defaults.
  2. Minimal deviations from default style:

    • Keep most defaults. Only override settings that are meaningful
    • Example options to override: max_width, imports_granularity, reorder_imports, wrap_comments.
  3. Check in CI and locally

    • Either a CI pipeline or Pester test should run:
    cargo fmt --all -- --check -- --files-with-diff
    • If formatting is incorrect, the build should fail and show the diff.
  4. One-time bulk reformat

    • Upon acceptance, run cargo fmt over the existing codebase.
    • After that, all future PRs must remain formatted.
  5. Developer tooling / integration

    • Configure popular tooling (VS Code) to auto-run formatting on save using rustfmt
  6. Versioning and toolchain alignment

    • We pin the rustfmt version implicitly by pinning the Rust toolchain version.

Example rustfmt.toml

Below is a suggestion for a minimal config that tweaks a few settings to get started (whilst retaining most of the default style):

# rustfmt.toml for DSC

# Basic layout
max_width = 100
hard_tabs = false
tab_spaces = 4

# Imports
reorder_imports = true
imports_granularity = "Crate"
group_imports = "StdExternalCrate"

# Comments & docs
wrap_comments = true
format_code_in_doc_comments = true
normalize_doc_attributes = true

# Macro bodies, etc.
format_macro_bodies = true

# Edition (for parsing behavior)
edition = "2021"

Rationale for some choices:

  • imports_granularity = "Crate" means use std::{fs, io} rather than separate lines.
  • group_imports = "StdExternalCrate" groups standard library imports, external crates, and local modules separately (not sure about compliance here)
  • Enabling comment wrapping and doc formatting keeps comments from drifting into odd indent or width shapes.
  • format_macro_bodies = true ensures macro invocation bodies are formatted consistently with the rest of the code.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions