docs: update documentation to match refactored codebase
- Delete obsolete plans/code-review.md (all suggestions implemented) - Rewrite docs/API.md to reflect modular architecture, DashMap, corrected AppError variants, accurate function signatures and module locations - Rewrite docs/ARCHITECTURE.md to reflect DashMap concurrency, remove completed future enhancements, fix AnalysisContext description - Update docs/USER_GUIDE.md with global CLI path options (--my-cars, --cars-db, --times-dir, --tracks-db) and updated troubleshooting
This commit is contained in:
parent
a9a46ee551
commit
d2c1418e5e
4 changed files with 284 additions and 539 deletions
263
docs/API.md
263
docs/API.md
|
|
@ -4,15 +4,37 @@
|
|||
|
||||
This document provides comprehensive API documentation for the Top Drives Records Analyzer, including all public functions, data structures, and their usage patterns.
|
||||
|
||||
## Core Modules
|
||||
## Module Structure
|
||||
|
||||
### Shared Library (`src/lib.rs`)
|
||||
```
|
||||
src/
|
||||
├── lib.rs # Shared library: types, utilities, error handling
|
||||
├── records.rs # Binary entry point: CLI definition and dispatch
|
||||
├── commands/
|
||||
│ ├── mod.rs # Command dispatch, run_analysis(), load_my_cars_or_warn()
|
||||
│ ├── fetch.rs # Fetch command: download car/track/timing data
|
||||
│ ├── best.rs # Best command: best.csv + very_best.csv
|
||||
│ ├── trash.rs # Trash command: trash.csv
|
||||
│ ├── wishlist.rs # Wishlist command: wishlist.csv, add.csv, remove.csv
|
||||
│ ├── details.rs # Details command: details.csv
|
||||
│ ├── track.rs # Track command: stdout query
|
||||
│ └── all.rs # All command: generates all output files
|
||||
├── parsing/
|
||||
│ ├── mod.rs # Core parsing: parse_car_times, update_best_car, parse_all_times
|
||||
│ └── car_list.rs # Car database loading: get_car_list
|
||||
└── output/
|
||||
└── mod.rs # CSV output: write_sorted_csv, write_best_csv, wishlist logic, etc.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Shared Library (`src/lib.rs`)
|
||||
|
||||
Common types and utilities shared across the project.
|
||||
|
||||
#### Public Types
|
||||
### Public Types
|
||||
|
||||
##### `Car`
|
||||
#### `Car`
|
||||
**Core car information structure**
|
||||
|
||||
```rust
|
||||
|
|
@ -20,9 +42,11 @@ Common types and utilities shared across the project.
|
|||
pub struct Car {
|
||||
pub rid: String, // Repository identifier
|
||||
pub brand: String, // Manufacturer/brand name
|
||||
#[serde(rename = "onlyName")]
|
||||
pub only_name: String, // Model name (without brand)
|
||||
pub year: u16, // Model year
|
||||
pub rq: u8, // Rarity level (10-113)
|
||||
#[serde(default)]
|
||||
pub prize: bool, // Whether car is prize-only
|
||||
pub guid: String, // Game-specific card identifier
|
||||
}
|
||||
|
|
@ -32,7 +56,7 @@ pub struct Car {
|
|||
|
||||
**Deserialization**: Uses `serde::Deserialize` with `#[serde(rename = "onlyName")]` for the `only_name` field and `#[serde(default)]` for `prize`.
|
||||
|
||||
##### `BestCarRecord`
|
||||
#### `BestCarRecord`
|
||||
**Best performing car record for a specific track/RQ/condition**
|
||||
|
||||
```rust
|
||||
|
|
@ -51,7 +75,7 @@ pub struct BestCarRecord {
|
|||
}
|
||||
```
|
||||
|
||||
##### `TripletKey`
|
||||
#### `TripletKey`
|
||||
**Composite key for unique track conditions**
|
||||
|
||||
```rust
|
||||
|
|
@ -59,20 +83,21 @@ pub type TripletKey = (String, u8, String);
|
|||
// Track ID RQ Level Surface Condition
|
||||
```
|
||||
|
||||
##### `AnalysisContext`
|
||||
**Mutable analysis state**
|
||||
#### `AnalysisContext`
|
||||
**Mutable analysis state using concurrent DashMap**
|
||||
|
||||
```rust
|
||||
pub struct AnalysisContext {
|
||||
pub best_cars: Arc<Mutex<HashMap<TripletKey, Vec<BestCarRecord>>>>,
|
||||
pub best_cars: Arc<DashMap<TripletKey, Vec<BestCarRecord>>>,
|
||||
}
|
||||
```
|
||||
|
||||
**Methods**:
|
||||
- `new()` — Create a new, empty analysis context
|
||||
- `lock_best_cars()` — Acquire a lock on the best_cars map (returns `Result`, converts poison errors to `AppError`)
|
||||
|
||||
##### `CarCounts`
|
||||
**Thread Safety**: Uses `DashMap` for sharded internal locking, allowing concurrent access from multiple parsing tasks without a global lock.
|
||||
|
||||
#### `CarCounts`
|
||||
**Aggregated per-car win counts**
|
||||
|
||||
```rust
|
||||
|
|
@ -84,18 +109,18 @@ pub struct CarCounts {
|
|||
}
|
||||
```
|
||||
|
||||
##### `MyCarsData`
|
||||
#### `MyCarsData`
|
||||
**Parsed user data from `my_cars.json`**
|
||||
|
||||
```rust
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MyCarsData {
|
||||
pub owned: HashSet<String>, // Owned car GUIDs
|
||||
pub wishlist: HashSet<String>, // Wishlist car GUIDs
|
||||
}
|
||||
```
|
||||
|
||||
##### `AppError`
|
||||
#### `AppError`
|
||||
**Unified error type**
|
||||
|
||||
```rust
|
||||
|
|
@ -109,14 +134,14 @@ pub enum AppError {
|
|||
Parse { file: String, reason: String },
|
||||
#[error("Missing data file: {0}")]
|
||||
MissingFile(String),
|
||||
#[error("Internal error: mutex poisoned — {0}")]
|
||||
Poisoned(String),
|
||||
#[error("Internal error: {0}")]
|
||||
Internal(String),
|
||||
}
|
||||
```
|
||||
|
||||
#### Public Functions
|
||||
### Public Functions
|
||||
|
||||
##### `translate_track_name(track_code: &str) -> String`
|
||||
#### `translate_track_name(track_code: &str) -> String`
|
||||
**Translates internal track identifiers to human-readable names**
|
||||
|
||||
- **Input**: Track code (e.g., `"laguna"`)
|
||||
|
|
@ -124,38 +149,40 @@ pub enum AppError {
|
|||
- **Fallback**: Returns original code if no mapping found
|
||||
- **Data source**: `track_names.json` (compiled into binary via `include_str!`)
|
||||
|
||||
##### `translate_track_type(track_code: &str) -> &'static str`
|
||||
#### `translate_track_type(track_code: &str) -> &'static str`
|
||||
**Translates condition codes to surface names**
|
||||
|
||||
- **Input**: Condition code (e.g., `"00"`)
|
||||
- **Output**: Surface name (e.g., `"Dry"`)
|
||||
- **Fallback**: Returns `"Unknown"` for unrecognized codes
|
||||
- **Fallback**: Returns the raw code string for unrecognized codes
|
||||
|
||||
##### `get_file(file_url: &str, local_file_path: &str) -> Result<(), AppError>`
|
||||
#### `get_file(file_url: &str, local_file_path: &str) -> Result<(), AppError>`
|
||||
**Downloads a file from a URL and saves it locally**
|
||||
|
||||
- Removes existing file before downloading
|
||||
- Writes to a `.tmp` file first, then renames on success (atomic write)
|
||||
- Retries up to 3 times on server errors (5xx) and empty responses
|
||||
- Returns `AppError::Network` on HTTP failures
|
||||
- Returns `AppError::Io` on file write failures
|
||||
|
||||
##### `load_my_cars_data(path: &str) -> Result<MyCarsData, AppError>`
|
||||
#### `load_my_cars_data(path: &str) -> Result<MyCarsData, AppError>`
|
||||
**Loads and parses user car collection data**
|
||||
|
||||
- **Input**: Path to `my_cars.json`
|
||||
- **Returns**: `MyCarsData` with owned and wishlist GUIDs
|
||||
- **Errors**: `AppError::MissingFile` if file not found, `AppError::Parse` if JSON invalid
|
||||
|
||||
##### `compare_cars(a: &Car, b: &Car) -> std::cmp::Ordering`
|
||||
#### `compare_cars(a: &Car, b: &Car) -> std::cmp::Ordering`
|
||||
**Sorting comparator for cars**
|
||||
|
||||
- Sorts by: RQ → Brand → Model name
|
||||
|
||||
##### `compute_car_counts(ctx: &AnalysisContext, car_list: Vec<Car>) -> Result<CarCounts, AppError>`
|
||||
#### `compute_car_counts(ctx: &AnalysisContext, car_list: Vec<Car>) -> Result<CarCounts, AppError>`
|
||||
**Aggregates per-car win counts from analysis context**
|
||||
|
||||
- Iterates all entries in the `DashMap`
|
||||
- Returns `CarCounts` with by_rid, guid_to_rid, car_map, and car_list
|
||||
|
||||
##### `escape_csv_field(field: &str) -> String`
|
||||
#### `escape_csv_field(field: &str) -> String`
|
||||
**Escapes a field for safe CSV inclusion**
|
||||
|
||||
- Wraps in quotes if field contains comma, quote, newline, or carriage return
|
||||
|
|
@ -163,11 +190,36 @@ pub enum AppError {
|
|||
|
||||
---
|
||||
|
||||
### Records Binary (`src/records.rs`)
|
||||
## Records Binary (`src/records.rs`)
|
||||
|
||||
Primary analysis engine for processing JSON-based timing data.
|
||||
CLI entry point. Defines command-line arguments and dispatches to command handlers.
|
||||
|
||||
#### Constants
|
||||
### CLI Global Options
|
||||
|
||||
| Option | Default | Description |
|
||||
|--------|---------|-------------|
|
||||
| `--my-cars` | `my_cars.json` | Path to user's car collection export |
|
||||
| `--cars-db` | `cars_repo.json` | Path to car database file |
|
||||
| `--times-dir` | `times` | Directory containing per-car timing JSON files |
|
||||
| `--tracks-db` | `tracks_repo.json` | Path to tracks database file |
|
||||
|
||||
### CLI Commands
|
||||
|
||||
| Command | Description | Flags |
|
||||
|---------|-------------|-------|
|
||||
| `fetch` | Download latest car, track, and timing data | None |
|
||||
| `best` | Generate `best.csv` and `very_best.csv` | `--min-count N` (default: 10) |
|
||||
| `trash` | Generate `trash.csv` | None |
|
||||
| `wishlist` | Generate `wishlist.csv`, `add.csv`, `remove.csv` | `--min-count N`, `--max-wishlist M` |
|
||||
| `all` | Generate all output files in a single pass | `--min-count N`, `--max-wishlist M` |
|
||||
| `details` | Generate `details.csv` with all timing data | None |
|
||||
| `track` | Show best car for specific track/condition/RQ | `<track> <condition> <rq>` |
|
||||
|
||||
---
|
||||
|
||||
## Parsing Module (`src/parsing/`)
|
||||
|
||||
### Constants (`src/parsing/mod.rs`)
|
||||
|
||||
| Constant | Value | Description |
|
||||
|----------|-------|-------------|
|
||||
|
|
@ -176,57 +228,106 @@ Primary analysis engine for processing JSON-based timing data.
|
|||
| `TIME_TOLERANCE` | 0.005 | Tolerance for tie detection (seconds) |
|
||||
| `TEST_BOWL_FACTOR` | 2000.0 | Multiplier for Test Bowl time inversion |
|
||||
| `CONCURRENCY_LIMIT` | 20 | Maximum concurrent tasks |
|
||||
| `DEFAULT_MY_CARS_PATH` | `"my_cars.json"` | Default user data file path |
|
||||
| `CARS_DB_PATH` | `"cars_repo.json"` | Car database file path |
|
||||
| `TIMES_DIR` | `"times"` | Timing data directory |
|
||||
| `CARS_DB_URL` | GitHub URL | Remote car database URL |
|
||||
| `TRACKS_DB_URL` | GitHub URL | Remote tracks database URL |
|
||||
| `CAR_TIMING_API_BASE` | API URL | Base URL for per-car timing data |
|
||||
|
||||
#### CLI Commands
|
||||
### Key Functions
|
||||
|
||||
| Command | Description | Flags |
|
||||
|---------|-------------|-------|
|
||||
| `fetch` | Download latest car and track data | None |
|
||||
| `best` | Generate `best.csv` and `very_best.csv` | `--min-count N` (default: 10) |
|
||||
| `trash` | Generate `trash.csv` | None |
|
||||
| `wishlist` | Generate `wishlist.csv`, `add.csv`, `remove.csv` | `--min-count N`, `--max-wishlist M` |
|
||||
| `all` | Generate all output files in a single pass | `--min-count N`, `--max-wishlist M` |
|
||||
| `details` | Generate `details.csv` with all timing data | None |
|
||||
| `track` | Show best car for specific track/condition/RQ | `<track> <condition> <rq>` |
|
||||
#### `parse_car_times(car, my_cars, best_cars, times_dir) -> Result<(), AppError>`
|
||||
**Processes timing data for a single car** (`src/parsing/mod.rs`)
|
||||
|
||||
#### Key Internal Functions
|
||||
|
||||
##### `run_analysis(owned: &HashSet<String>) -> Result<(AnalysisContext, CarCounts), AppError>`
|
||||
**Common analysis setup**
|
||||
|
||||
Loads user data, parses all timing files, and computes car counts.
|
||||
|
||||
##### `parse_car_times(car: Car, my_cars: &HashSet<String>, ctx: &AnalysisContext)`
|
||||
**Processes timing data for a single car**
|
||||
|
||||
- Runs inside `tokio::task::spawn_blocking` to avoid blocking async runtime
|
||||
- Reads car JSON file from `times/` directory
|
||||
- **Synchronous** — designed to be called from `tokio::task::spawn_blocking`
|
||||
- Reads car JSON file from the times directory
|
||||
- Processes each configuration and track
|
||||
- Applies quality filters (downvotes, minimum time)
|
||||
- Handles Test Bowl time inversion
|
||||
- Uses `rsplit_once("_a")` for correct track name splitting
|
||||
|
||||
##### `update_best_car(best_cars, track_id, car_rq, surface, new_record)`
|
||||
**Updates best car records with a new performance entry**
|
||||
#### `update_best_car(best_cars, track_id, car_rq, surface, new_record)`
|
||||
**Updates best car records with a new performance entry** (`src/parsing/mod.rs`)
|
||||
|
||||
- Iterates RQ range from car's RQ to `MAX_RQ`
|
||||
- Replaces existing record if new time is faster
|
||||
- Adds to tie list if within `TIME_TOLERANCE`
|
||||
- Prevents duplicate `car_rid` entries in tie lists
|
||||
- Optimized: avoids cloning on the last iteration via `is_last` guard
|
||||
|
||||
##### `parse_all_times(my_cars, ctx) -> Vec<Car>`
|
||||
**Orchestrates concurrent parsing of all car timing files**
|
||||
#### `parse_all_times(my_cars, ctx, cars_db_path, times_dir) -> Result<Vec<Car>, AppError>`
|
||||
**Orchestrates concurrent parsing of all car timing files** (`src/parsing/mod.rs`)
|
||||
|
||||
- Loads car list from database
|
||||
- Uses `Semaphore(CONCURRENCY_LIMIT)` for bounded concurrency
|
||||
- Spawns blocking tasks via `tokio::task::spawn_blocking`
|
||||
- Acquires semaphore in async context, then runs sync parsing via `spawn_blocking`
|
||||
- Returns the full car list for downstream use
|
||||
|
||||
##### `compute_ideal_wishlist(counts, owned, min_count, max_wishlist) -> Vec<(BestCarRecord, usize)>`
|
||||
#### `fetch_times(cars_db_path, times_dir) -> Result<(), AppError>`
|
||||
**Fetches timing data files for all cars** (`src/parsing/mod.rs`)
|
||||
|
||||
- Uses bounded concurrency via semaphore
|
||||
- Returns first error encountered, or `Ok(())` if all succeed
|
||||
|
||||
#### `get_car_list(cars_db_path: &str) -> Result<Vec<Car>, AppError>`
|
||||
**Loads the car database** (`src/parsing/car_list.rs`)
|
||||
|
||||
- Uses serde deserialization for type-safe parsing
|
||||
- Returns `AppError::MissingFile` or `AppError::Parse` on failure
|
||||
|
||||
---
|
||||
|
||||
## Commands Module (`src/commands/`)
|
||||
|
||||
### `run_analysis(owned, cars_db_path, times_dir) -> Result<(AnalysisContext, CarCounts), AppError>`
|
||||
**Common analysis setup** (`src/commands/mod.rs`)
|
||||
|
||||
Loads car database, parses all timing files concurrently, and computes car counts.
|
||||
|
||||
### `load_my_cars_or_warn(path: &str) -> Result<MyCarsData, AppError>`
|
||||
**Loads user data with graceful degradation** (`src/commands/mod.rs`)
|
||||
|
||||
- Returns empty `MyCarsData::default()` with a warning if file is missing
|
||||
- Propagates parse errors and other failures
|
||||
|
||||
---
|
||||
|
||||
## Output Module (`src/output/`)
|
||||
|
||||
### CSV Output Functions
|
||||
|
||||
#### `write_sorted_csv(path, header, entries, cmp, format_row) -> Result<(), AppError>`
|
||||
**Generic sorted CSV writer**
|
||||
|
||||
- Sorts by index (avoids cloning entries)
|
||||
- Writes header row
|
||||
- Formats each row via closure
|
||||
|
||||
#### `write_best_csv(counts)`, `write_very_best_csv(counts, min_count)`
|
||||
**Generate best car CSV files**
|
||||
|
||||
- Format: `RQ,Brand,Model,Year,Count`
|
||||
- Fields are CSV-escaped
|
||||
|
||||
#### `write_trash_csv(counts, owned)`
|
||||
**Generate trash car CSV file**
|
||||
|
||||
- Filters: not best on any track, RQ < 80, not prize car
|
||||
- Format: `RQ,Brand,Model,Year`
|
||||
|
||||
#### `write_wishlist_files(counts, owned, wishlist, min_count, max_wishlist)`
|
||||
**Generate wishlist, add, and remove CSV files**
|
||||
|
||||
- Computes ideal wishlist once
|
||||
- Derives all three files from it
|
||||
- Format: `RQ,Brand,Model,Year,Count`
|
||||
|
||||
#### `write_details_csv(ctx)`
|
||||
**Generate comprehensive timing data CSV**
|
||||
|
||||
- Format: `RQ,TrackID,Condition,CarRQ,Brand,Model,Year,Config,Time`
|
||||
- Sorted by RQ then track ID
|
||||
|
||||
#### `print_best_car(ctx, track, condition, rq) -> Result<(), AppError>`
|
||||
**Print best car for a specific track/condition/RQ to stdout**
|
||||
|
||||
#### `compute_ideal_wishlist(counts, owned, min_count, max_wishlist) -> Vec<(BestCarRecord, usize)>`
|
||||
**Computes the ideal wishlist**
|
||||
|
||||
- Filters to cars with count >= `min_count`
|
||||
|
|
@ -234,39 +335,9 @@ Loads user data, parses all timing files, and computes car counts.
|
|||
- Sorts by RQ
|
||||
- Caps at `max_wishlist`
|
||||
|
||||
#### Output Functions
|
||||
### Sorting Helpers
|
||||
|
||||
##### `write_sorted_csv(path, header, entries, cmp, format_row)`
|
||||
**Generic sorted CSV writer**
|
||||
#### `compare_records(a: &BestCarRecord, b: &BestCarRecord) -> std::cmp::Ordering`
|
||||
**Comparison for BestCarRecord sorting**
|
||||
|
||||
- Sorts by index (avoids cloning entries)
|
||||
- Writes header row
|
||||
- Formats each row via closure
|
||||
|
||||
##### `write_best_csv(counts)`, `write_very_best_csv(counts, min_count)`
|
||||
**Generate best car CSV files**
|
||||
|
||||
- Format: `RQ,Brand,Model,Year,Count`
|
||||
- Fields are CSV-escaped
|
||||
|
||||
##### `write_trash_csv(counts, owned)`
|
||||
**Generate trash car CSV file**
|
||||
|
||||
- Filters: not best on any track, RQ < 80, not prize car
|
||||
- Format: `RQ,Brand,Model,Year`
|
||||
|
||||
##### `write_wishlist_files(counts, owned, wishlist, min_count, max_wishlist)`
|
||||
**Generate wishlist, add, and remove CSV files**
|
||||
|
||||
- Computes ideal wishlist once
|
||||
- Derives all three files from it
|
||||
- Format: `RQ,Brand,Model,Year,Count`
|
||||
|
||||
##### `write_details_csv(ctx)`
|
||||
**Generate comprehensive timing data CSV**
|
||||
|
||||
- Format: `RQ,TrackID,Condition,CarRQ,Brand,Model,Year,Config,Time`
|
||||
- Sorted by RQ then track ID
|
||||
|
||||
##### `print_best_car(ctx, track, condition, rq)`
|
||||
**Print best car for a specific track/condition/RQ to stdout**
|
||||
- Sorts by: RQ → Brand → Model name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue