# Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
$ErrorActionPreference = "Stop"
Push-Location (Split-Path $PSScriptRoot -Parent)
function Invoke-Crap4RustGate {
param(
[string]$Label,
[string[]]$Packages,
[string]$Features = "",
[switch]$NoDefaultFeatures,
[switch]$IncludeTestTargets,
[double]$Threshold = 15,
[switch]$UseProjectThreshold,
[string[]]$ExcludePaths = @()
)
Write-Host "$Label..." -ForegroundColor Cyan
$manifestPath = (Resolve-Path (Join-Path $PSScriptRoot "..\Cargo.toml")).Path
$args = @("--manifest-path", $manifestPath)
foreach ($package in $Packages) {
$args += @("--package", $package)
}
if ($Features -ne "") {
$args += @("--features", $Features)
}
if ($NoDefaultFeatures) {
$args += "--no-default-features"
}
if ($IncludeTestTargets) {
$args += "--include-test-targets"
}
foreach ($excludePath in $ExcludePaths) {
$args += @("--exclude-path", $excludePath)
}
$args += @("--warn-only", "--threshold", $Threshold.ToString())
$previousErrorActionPreference = $ErrorActionPreference
$ErrorActionPreference = "Continue"
$output = & cargo crap4rust @args 2>&1
$ErrorActionPreference = $previousErrorActionPreference
$exitCode = $LASTEXITCODE
$output | ForEach-Object { Write-Host $_ }
if ($exitCode -ne 0) {
Write-Host "`nFailed: $Label (exit code $exitCode)" -ForegroundColor Red
Pop-Location
exit 1
}
$summaryLine = $output | Select-String -Pattern "summary:\s+total_functions=.*crappy_functions=(\d+)"
if (-not $summaryLine) {
Write-Host "`nFailed: $Label (could not parse crap4rust summary)" -ForegroundColor Red
Pop-Location
exit 1
}
$crappyCount = [int]$summaryLine.Matches[0].Groups[1].Value
if ($UseProjectThreshold) {
$verdictLine = $output | Select-String -Pattern "verdict=(clean|warn|crappy)"
if (-not $verdictLine) {
Write-Host "`nFailed: $Label (could not parse crap4rust verdict)" -ForegroundColor Red
Pop-Location
exit 1
}
$verdict = $verdictLine.Matches[0].Groups[1].Value
if ($verdict -eq "crappy") {
Write-Host "`nFailed: $Label (project verdict is crappy)" -ForegroundColor Red
Pop-Location
exit 1
}
} else {
if ($crappyCount -gt 0) {
Write-Host "`nFailed: $Label ($crappyCount crappy functions detected)" -ForegroundColor Red
Pop-Location
exit 1
}
}
}
# ---------------------------------------------------------------------------
# CRAP gates
# ---------------------------------------------------------------------------
Invoke-Crap4RustGate "CRAP cargo-crap4rust" @("cargo-crap4rust") -ExcludePaths @("tests/fixtures")
# ---------------------------------------------------------------------------
Write-Host "`ncrap4rust Stage 2 passed!" -ForegroundColor Green
Pop-Location
exit 0