npx lint-staged

# Only format and re-add Rust files that are already staged
TOPLEVEL=$(git rev-parse --show-toplevel)

format_staged_rust() {
  local dir="$1"
  local staged=""
  staged=$(git diff --staged --name-only --diff-filter=ACM -- ":(top,literal)$dir/*.rs" ":(top,literal)$dir/**/*.rs" 2>/dev/null)
  if [ -n "$staged" ]; then
    echo "$staged" | while IFS= read -r f; do
      rustfmt "$TOPLEVEL/$f" 2>/dev/null || true
    done
    echo "$staged" | while IFS= read -r f; do
      git add "$TOPLEVEL/$f" 2>/dev/null || true
    done
  fi
}

format_staged_rust "src-tauri"
format_staged_rust "crates/dbx-core"
format_staged_rust "crates/dbx-web"
