# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
`prune-backup` is a Rust CLI tool that manages backup file rotation based on creation dates. It scans a directory and applies retention policies (keep-last, hourly, daily, weekly, monthly, yearly), moving files that don't match any policy to a `.trash` subdirectory.
## Build and Test Commands
```bash
# Run all checks (fmt, clippy, test)
make all
# Individual commands
make fmt # Check formatting
make clippy # Lint with pedantic warnings
make test # Run tests
make install # Install binary
# Run a specific test
cargo test test_name
# Run with example
cargo run -- /path/to/backups --keep-last 5 --keep-daily 7
# Dry run (no files moved)
cargo run -- /path/to/backups --dry-run
```
## Architecture
**Two-crate structure:**
- `src/main.rs` - CLI argument parsing with clap, converts args to `RetentionConfig`, calls library
- `src/lib.rs` - Core logic: file scanning, retention selection, trash operations
**Key types:**
- `FileInfo` - File path + creation timestamp
- `RetentionConfig` - All retention policy values (keep_last, keep_hourly, etc.)
**Core algorithm in `select_files_to_keep_with_datetime()`:**
Retention policies are applied in cascading order. Each policy only considers files not already kept by previous policies:
1. keep-last (absolute count)
2. keep-hourly (1 per hour within N hours)
3. keep-daily (1 per day within N days)
4. keep-weekly (1 per ISO week within N weeks)
5. keep-monthly (1 per month within N months)
6. keep-yearly (1 per year within N years)
Files are sorted newest-first, so the "latest" file in each time period is the first encountered.
**Testing approach:**
- Unit tests in `src/lib.rs` use mock `FileInfo` with controlled timestamps
- Integration tests in `tests/integration.rs` use `tempfile` + `filetime` to create real files with specific modification times
- The `_with_datetime` variant of selection allows injecting a fixed "now" for deterministic tests