masterror 0.27.0

Application error types and response mapping
Documentation
# SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
#
# SPDX-License-Identifier: MIT

# Makefile.toml for cargo-make
# Usage:
#   cargo make             # pretty help (default)
#   cargo make ci          # fmt, clippy, tests, package
#   TAG=v0.3.0 cargo make release
#   CARGO_REGISTRY_TOKEN=... TAG=v0.3.0 cargo make publish

[env]
ALL_FEATURES = "true" # toggle --all-features for clippy/test

# ------- Default task (no root 'default_task' key) -------

[tasks.default]
category = "Meta"
description = "Default task -> help"
run_task = "help"

# ------- Core checks -------

[tasks.clippy]
category = "Lint"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ "${ALL_FEATURES}" = "true" ]; then
    cargo clippy --workspace --all-targets --all-features -- -D warnings
  else
    cargo clippy --workspace --all-targets -- -D warnings
  fi
''']
description = "Run clippy for all targets; fail on warnings"

[tasks.test]
category = "Test"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ "${ALL_FEATURES}" = "true" ]; then
    cargo test --workspace --all-features --no-fail-fast
  else
    cargo test --workspace --no-fail-fast
  fi
''']
description = "Run tests (optionally with all features)"

[tasks.package]
category = "Package"
clear = true
command = "cargo"
args = ["package", "--locked"]
description = "Verify that the crate can be packaged cleanly"

[tasks.ci]
category = "Meta"
dependencies = ["format", "clippy", "test", "package"]
description = "Run fmt, clippy, tests, and packaging checks"

# ------- Release gatekeeping -------

[tasks.check_tag_env]
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ -z "${TAG:-}" ]; then
    echo "TAG env var is required, e.g. TAG=v0.3.0"
    exit 1
  fi
''']
description = "Ensure TAG env var is provided (e.g. TAG=v1.2.3)"

[tasks.ensure_tag_matches_version]
clear = true
script_runner = "bash"
script = [
  '''
  set -euo pipefail
  TAG="${TAG#refs/tags/}"

  if command -v jq >/dev/null 2>&1; then
    FILE_VER="$(cargo metadata --no-deps --format-version=1 | jq -r '.packages[0].version')"
  else
    FILE_VER="$(cargo metadata --no-deps --format-version=1 | sed -n 's/.*"version":"\([^"]*\)".*/\1/p' | head -n1)"
  fi

  if [ -z "${FILE_VER}" ]; then
    echo "Unable to parse version from cargo metadata."
    exit 1
  fi

  if [ "${TAG}" != "v${FILE_VER}" ]; then
    echo "Tag ${TAG} != Cargo.toml version v${FILE_VER}"
    exit 1
  fi

  echo "Tag ${TAG} matches Cargo.toml version v${FILE_VER}"
''',
]
dependencies = ["check_tag_env"]
description = "Ensure git tag matches Cargo.toml version"

[tasks.check_token]
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  if [ -z "${CARGO_REGISTRY_TOKEN:-}" ]; then
    echo "CARGO_REGISTRY_TOKEN is required to publish."
    exit 1
  fi
''']
description = "Ensure crates.io token is present in env"

# ------- Publish & Release -------

[tasks.publish]
category = "Release"
clear = true
script_runner = "bash"
script = ['''
  set -euo pipefail
  cargo publish --locked --token "${CARGO_REGISTRY_TOKEN}"
''']
dependencies = ["check_token", "ensure_tag_matches_version", "ci"]
description = "Publish crate to crates.io after checks and tag/version verification"

[tasks.tag]
category = "Release"
clear = true
script_runner = "bash"
script = [
  '''
  set -euo pipefail

  if command -v jq >/dev/null 2>&1; then
    VER="$(cargo metadata --no-deps --format-version=1 | jq -r '.packages[0].version')"
  else
    VER="$(cargo metadata --no-deps --format-version=1 | sed -n 's/.*"version":"\([^"]*\)".*/\1/p' | head -n1)"
  fi

  if [ -z "${VER}" ]; then
    echo "Unable to extract version from Cargo metadata."
    exit 1
  fi

  TAG="v${VER}"

  if git rev-parse "${TAG}" >/dev/null 2>&1; then
    echo "Tag ${TAG} already exists."
    exit 1
  fi

  git tag "${TAG}"
  echo "Created tag ${TAG}. You can push it with: git push origin ${TAG}"
''',
]
description = "Create git tag vX.Y.Z from Cargo.toml version (does not push)"

[tasks.release]
category = "Release"
clear = true
dependencies = ["publish"]
description = "Run checks and publish the crate (requires TAG and token)"

# ------- Extras -------

[tasks.format]
category = "Format"
clear = true
description = "Format code using rustfmt on nightly (optional)"
script_runner = "bash"
script = ['''
  set -euo pipefail
  if ! rustup toolchain list | grep -q "^nightly"; then
    echo "Installing nightly toolchain for rustfmt..."
    rustup toolchain install nightly >/dev/null
  fi
  rustup component add rustfmt --toolchain nightly >/dev/null
  cargo +nightly fmt --
''']

[tasks.install-hooks]
clear = true
workspace = false
description = "Install git pre-commit hook from .hooks/"
script_runner = "bash"
script = [
  'set -euo pipefail',
  'if [ ! -f .hooks/pre-commit ]; then echo "❌ .hooks/pre-commit not found!"; exit 1; fi',
  'echo "🔗 Linking .hooks/pre-commit to .git/hooks/pre-commit..."',
  'mkdir -p .git/hooks',
  'ln -sf ../../.hooks/pre-commit .git/hooks/pre-commit',
  'chmod +x .hooks/pre-commit',
  'echo "✅ pre-commit hook installed."',
]

# ------- Pretty Help -------

[tasks.help]
clear = true
category = "Meta"
description = "Pretty, colored help with examples"
script_runner = "bash"
script = [
  '''
  set -euo pipefail

  if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
    BOLD="\033[1m"; DIM="\033[2m"; RESET="\033[0m"
    BLUE="\033[34m"; CYAN="\033[36m"; GREEN="\033[32m"; YELLOW="\033[33m"; MAGENTA="\033[35m"
  else
    BOLD=""; DIM=""; RESET=""; BLUE=""; CYAN=""; GREEN=""; YELLOW=""; MAGENTA=""
  fi

  hr() { printf "%s\n" "──────────────────────────────────────────────────────────────────────────────"; }
  row() { printf "  %b%s%b  %b%s%b\n" "$GREEN" "$1" "$RESET" "$DIM" "$2" "$RESET"; }

  if command -v jq >/dev/null 2>&1; then
    META="$(cargo metadata --no-deps --format-version=1)"
    VER="$(printf "%s" "$META" | jq -r '.packages[0].version')"
    MSRV="$(printf "%s" "$META" | jq -r '.packages[0].rust_version // "unknown"')"
    NAME="$(printf "%s" "$META" | jq -r '.packages[0].name')"
  else
    META="$(cargo metadata --no-deps --format-version=1)"
    VER="$(printf "%s" "$META" | sed -n 's/.*\"version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
    MSRV="$(printf "%s" "$META" | sed -n 's/.*\"rust_version\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"; [ -z "$MSRV" ] && MSRV="unknown"
    NAME="$(printf "%s" "$META" | sed -n 's/.*\"name\":\"\\([^\"]*\\)\".*/\\1/p' | head -n1)"
  fi

  BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'unknown')"
  LAST_TAG="$(git describe --tags --abbrev=0 2>/dev/null || echo 'none')"

  printf "%b%s%b  %b(%s, MSRV %s, branch %s, last tag %s)%b\n" \
    "$BOLD$BLUE" "${NAME:-crate} development tasks" "$RESET" \
    "$DIM" "version ${VER:-unknown}" "${MSRV:-unknown}" "${BRANCH}" "${LAST_TAG}" "$RESET"
  hr

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Common commands" "$RESET"
  row "cargo make ci"                  "Run fmt, clippy, tests, and packaging"
  row "cargo make tag"                 "Create git tag v${VER} from Cargo.toml (no push)"
  row "TAG=v${VER} cargo make release" "Run checks and publish to crates.io"
  row "ALL_FEATURES=false cargo make ci" "Run CI without --all-features"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Formatting" "$RESET"
  row "cargo make format"              "Format with nightly rustfmt (unstable features)"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Lint & Tests" "$RESET"
  row "cargo make clippy"              "Run clippy --workspace --all-targets -D warnings"
  row "cargo make test"                "Run tests --workspace"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Packaging & Release" "$RESET"
  row "cargo make package"             "Dry-run packaging (--locked)"
  row "cargo make publish"             "Publish to crates.io (requires TAG and token)"
  echo

  printf "%b%s%b\n" "$BOLD$MAGENTA" "Hooks & Utilities" "$RESET"
  row "cargo make install-hooks"       "Install .hooks/pre-commit"
  echo

  printf "%b%s%b\n" "$BOLD$DIM" "Environment variables:" "$RESET"
  printf "  %b%-22s%b %s\n" "$CYAN" "ALL_FEATURES=true|false" "$RESET" "Enable/disable --all-features for clippy/test"
  printf "  %b%-22s%b %s\n" "$CYAN" "TAG=vX.Y.Z"             "$RESET" "Git tag matching Cargo.toml version"
  printf "  %b%-22s%b %s\n" "$CYAN" "CARGO_REGISTRY_TOKEN"   "$RESET" "crates.io token for publish"
  echo

  printf "%b%s%b\n" "$BOLD$DIM" "Tips:" "$RESET"
  printf "  • Keep %bCargo.lock%b committed\n" "$BOLD" "$RESET"
  printf "  • Tag must exactly match version: %bv%s%b\n" "$BOLD" "${VER}" "$RESET"
  echo
''',
]

# Alias
[tasks.h]
clear = true
run_task = "help"
category = "Meta"
description = "Alias: help"