Problem Statement
Phase 5 of CLI-to-crate unification is incomplete. While the parsing layer is properly unified, the export layer has 541 lines of duplicated code between src/main.rs (CLI) and src/export.rs (library).
Reference: PR #23
Current State Analysis
✅ Parsing Layer (Unified)
- CLI uses library functions:
parse_frames(), parse_headers_from_text()
- No duplication
- Status: Complete
⚠️ Export Layer (Duplicated)
Library (src/export.rs - 322 lines)
pub fn export_to_csv(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
pub fn export_to_gpx(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
pub fn export_to_event(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
// Private helpers
fn export_headers_to_csv(header: &BBLHeader, output_path: &Path) -> Result<()>
fn export_flight_data_to_csv(log: &BBLLog, output_path: &Path) -> Result<()>
CLI (src/main.rs - 541 lines)
fn export_logs_to_csv(...) // Lines 1074-1127 (54 lines)
fn export_single_log_to_csv(...) // Lines 1128-1171 (44 lines)
fn export_headers_to_csv(...) // Lines 1172-1209 (38 lines) ⚠️ IDENTICAL to export.rs
fn export_flight_data_to_csv(...) // Lines 1210-1507 (298 lines)
fn export_gpx_file(...) // Lines 1509-1573 (65 lines)
fn export_event_file(...) // Lines 1575-1616 (42 lines)
Critical Issue: export_headers_to_csv is byte-for-byte identical in both files, proving duplication rather than intentional divergence.
Test Coverage Gap
- 37 total tests across the codebase
- 0 tests for library export functions
- Most tests are CLI integration tests in
main.rs
Impact
Maintenance Burden
- Bug fixes must be applied in two places
- Risk of divergence: CLI and library exports could produce different output
- Code review complexity
Library Completeness
- Export API exists but untested
- No confidence for external crate consumers
- Documentation claims don't match reality
Detailed Unification Plan
Phase 1: Preparation & Testing (Priority: High)
Step 1.1: Add Comprehensive Library Export Tests
Goal: Ensure library functions are correct before CLI migration
Create new test file: tests/export_integration_tests.rs
Test Requirements:
AI Instructions:
Analyze src/export.rs functions export_to_csv, export_to_gpx, and export_to_event.
Create comprehensive integration tests in tests/export_integration_tests.rs that:
1. Use sample BBL data from tests/fixtures/
2. Export to temporary directories
3. Validate output format and content
4. Test all ExportOptions variations
5. Ensure outputs match blackbox_decode reference when applicable
Step 1.2: Document Current CLI Export Behavior
Goal: Capture any CLI-specific logic before refactoring
AI Instructions:
Compare implementations between:
- src/main.rs:export_headers_to_csv (lines 1172-1209)
- src/export.rs:export_headers_to_csv (lines 124-159)
And:
- src/main.rs:export_flight_data_to_csv (lines 1210-1507)
- src/export.rs:export_flight_data_to_csv (lines 160-289)
Document:
1. Any algorithmic differences
2. Different error handling approaches
3. CLI-specific logging or progress indicators
4. Performance optimizations unique to either
Phase 2: Refactor CLI to Use Library (Priority: High)
Step 2.1: Replace export_headers_to_csv
Target: src/main.rs lines 1172-1209 (38 lines) → DELETE
AI Instructions:
In src/main.rs:
1. Remove function export_headers_to_csv (lines 1172-1209)
2. Find all call sites of export_headers_to_csv in main.rs
3. Since this is a private helper, it's only called by other main.rs export functions
4. Note: The library's export_to_csv already calls this helper internally
5. Update callers to use the library's public API instead
6. Run tests to ensure no behavioral changes
Step 2.2: Replace export_flight_data_to_csv
Target: src/main.rs lines 1210-1507 (298 lines) → DELETE
AI Instructions:
In src/main.rs:
1. Remove function export_flight_data_to_csv (lines 1210-1507)
2. Find all call sites in main.rs
3. The library's export_to_csv already calls export_flight_data_to_csv internally
4. Update callers to use bbl_parser::export_to_csv() instead
5. Ensure debug parameter is handled appropriately
6. Run all tests after changes
Step 2.3: Refactor export_logs_to_csv
Target: src/main.rs lines 1074-1127 (54 lines) → REFACTOR
This function handles batch CSV export for multiple logs. It can be simplified to call the library.
AI Instructions:
In src/main.rs function export_logs_to_csv (lines 1074-1127):
1. Keep the function signature (CLI-specific batch processing)
2. Replace internal logic with calls to:
- bbl_parser::export_to_csv() for each log
3. Keep CLI-specific features:
- Progress messages
- Batch processing loop
- Path construction for output files
4. Reduce function to ~20-30 lines (wrapper only)
5. Run tests after refactoring
Step 2.4: Refactor export_single_log_to_csv
Target: src/main.rs lines 1128-1171 (44 lines) → SIMPLIFY
AI Instructions:
In src/main.rs function export_single_log_to_csv (lines 1128-1171):
1. Keep function signature
2. Replace implementation with direct call to:
bbl_parser::export_to_csv(&log, output_path, &export_options)?
3. Keep CLI-specific status messages
4. Reduce to ~10-15 lines
5. Verify output matches previous behavior
Step 2.5: Refactor export_gpx_file
Target: src/main.rs lines 1509-1573 (65 lines) → SIMPLIFY
AI Instructions:
In src/main.rs function export_gpx_file (lines 1509-1573):
1. Replace implementation with:
bbl_parser::export_to_gpx(&log, output_path, &export_options)?
2. Keep CLI user messages
3. Reduce to ~10 lines
Step 2.6: Refactor export_event_file
Target: src/main.rs lines 1575-1616 (42 lines) → SIMPLIFY
AI Instructions:
In src/main.rs function export_event_file (lines 1575-1616):
1. Replace with:
bbl_parser::export_to_event(&log, output_path, &export_options)?
2. Keep CLI messages
3. Reduce to ~10 lines
Phase 3: Testing & Validation (Priority: Critical)
Step 3.1: Run Full Test Suite
cargo test --all-features
cargo test --release
Expected Results:
Step 3.2: Manual CLI Testing
# Test CSV export
./target/release/bbl_parser tests/fixtures/sample.BBL --csv output.csv
# Test GPX export
./target/release/bbl_parser tests/fixtures/sample.BBL --gpx output.gpx
# Test event export
./target/release/bbl_parser tests/fixtures/sample.BBL --event output.event
# Test batch processing
./target/release/bbl_parser tests/fixtures/ --csv output_dir/
Validation:
Step 3.3: Benchmark Performance
# Before refactoring
hyperfine 'target/release/bbl_parser tests/fixtures/large.BBL --csv /tmp/out.csv'
# After refactoring
hyperfine 'target/release/bbl_parser tests/fixtures/large.BBL --csv /tmp/out.csv'
Goal: No performance regression (within 5% variance)
Phase 4: Code Quality & Documentation (Priority: Medium)
Step 4.1: Update Documentation
AI Instructions:
Update AGENTS.md:
1. Change "Complete CLI-to-Crate Unification Phase 5" to "Phase 5: CLI-to-Crate Unification (Complete)"
2. Add section documenting:
- Parsing layer unified (parse_frames, parse_headers_from_text)
- Export layer unified (export_to_csv, export_to_gpx, export_to_event)
- CLI is now thin wrapper (~200 lines of CLI-specific logic)
3. Update code organization section to reflect new structure
Add to CRATE_USAGE.md:
1. Section "Exporting Data"
2. Example code for each export function
3. Explain ExportOptions configuration
Step 4.2: Remove Dead Code
Target: Reduce src/main.rs from 1821 lines to ~1200 lines (600 line reduction)
Step 4.3: Code Quality Checks
cargo fmt
cargo clippy -- -D warnings
cargo build --release
Requirements:
Phase 5: Library API Stabilization (Priority: Medium)
Step 5.1: Review Public API
Current public exports from src/export.rs:
pub fn export_to_csv(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
pub fn export_to_gpx(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
pub fn export_to_event(log: &BBLLog, path: &Path, options: &ExportOptions) -> Result<()>
Questions to resolve:
Step 5.2: Prepare for crates.io Release
Success Criteria
Quantitative Metrics
Qualitative Goals
Rollback Plan
If issues arise during refactoring:
-
Git branching strategy:
git checkout -b phase5-export-unification
# Make changes
git commit -m "Step X: [description]"
# If issues: git revert <commit>
-
Incremental commits: Each step above should be a separate commit
-
Test between steps: Run cargo test after each major change
-
Keep old code commented: During transition, keep old implementations as comments until validation complete
Estimated Effort
| Phase |
Estimated Time |
Dependencies |
| Phase 1: Testing |
1-2 days |
None |
| Phase 2: Refactoring |
2-3 days |
Phase 1 complete |
| Phase 3: Validation |
1 day |
Phase 2 complete |
| Phase 4: Documentation |
1 day |
Phase 3 complete |
| Phase 5: Stabilization |
1-2 days |
Phase 4 complete |
| Total |
6-9 days |
Sequential |
Additional Context
Why This Matters:
The current duplication creates technical debt that undermines the library-first architecture goal. External consumers cannot trust the library export API if it's untested and unused even by the project's own CLI.
Design Philosophy:
The CLI should be a thin layer demonstrating how to use the library, not reimplementing library functionality. This makes the project:
- Easier to maintain (single source of truth)
- More trustworthy for external consumers
- Better documented (CLI serves as working example)
- More testable (library tests are isolated from CLI concerns)
Issue Created By: AI code review (CodeRabbit)
Analysis Methodology: Static code analysis, test execution, line-by-line comparison
Problem Statement
Phase 5 of CLI-to-crate unification is incomplete. While the parsing layer is properly unified, the export layer has 541 lines of duplicated code between
src/main.rs(CLI) andsrc/export.rs(library).Reference: PR #23
Current State Analysis
✅ Parsing Layer (Unified)
parse_frames(),parse_headers_from_text()Library (
src/export.rs- 322 lines)CLI (
src/main.rs- 541 lines)Critical Issue:
export_headers_to_csvis byte-for-byte identical in both files, proving duplication rather than intentional divergence.Test Coverage Gap
main.rsImpact
Maintenance Burden
Library Completeness
Detailed Unification Plan
Phase 1: Preparation & Testing (Priority: High)
Step 1.1: Add Comprehensive Library Export Tests
Goal: Ensure library functions are correct before CLI migration
Create new test file:
tests/export_integration_tests.rsTest Requirements:
export_to_csv()produces valid CSV with correct headersexport_to_gpx()produces valid GPX XMLexport_to_event()produces correct event log formatExportOptionsconfigurationsAI Instructions:
Step 1.2: Document Current CLI Export Behavior
Goal: Capture any CLI-specific logic before refactoring
AI Instructions:
Phase 2: Refactor CLI to Use Library (Priority: High)
Step 2.1: Replace export_headers_to_csv
Target:
src/main.rslines 1172-1209 (38 lines) → DELETEAI Instructions:
Step 2.2: Replace export_flight_data_to_csv
Target:
src/main.rslines 1210-1507 (298 lines) → DELETEAI Instructions:
Step 2.3: Refactor export_logs_to_csv
Target:
src/main.rslines 1074-1127 (54 lines) → REFACTORThis function handles batch CSV export for multiple logs. It can be simplified to call the library.
AI Instructions:
Step 2.4: Refactor export_single_log_to_csv
Target:
src/main.rslines 1128-1171 (44 lines) → SIMPLIFYAI Instructions:
Step 2.5: Refactor export_gpx_file
Target:
src/main.rslines 1509-1573 (65 lines) → SIMPLIFYAI Instructions:
Step 2.6: Refactor export_event_file
Target:
src/main.rslines 1575-1616 (42 lines) → SIMPLIFYAI Instructions:
Phase 3: Testing & Validation (Priority: Critical)
Step 3.1: Run Full Test Suite
Expected Results:
Step 3.2: Manual CLI Testing
Validation:
Step 3.3: Benchmark Performance
Goal: No performance regression (within 5% variance)
Phase 4: Code Quality & Documentation (Priority: Medium)
Step 4.1: Update Documentation
AGENTS.md: Correct "Phase 5 complete" claim to accurately reflect current stateCRATE_USAGE.md: Add export API examplesREADME.md: Document library export capabilitiessrc/export.rsfunctionsAI Instructions:
Step 4.2: Remove Dead Code
Target: Reduce
src/main.rsfrom 1821 lines to ~1200 lines (600 line reduction)Step 4.3: Code Quality Checks
Requirements:
Phase 5: Library API Stabilization (Priority: Medium)
Step 5.1: Review Public API
Current public exports from
src/export.rs:Questions to resolve:
export_to_csv_string())?Step 5.2: Prepare for crates.io Release
Success Criteria
Quantitative Metrics
src/main.rsreduced from 1821 to ~1200 lines (34% reduction)Qualitative Goals
Rollback Plan
If issues arise during refactoring:
Git branching strategy:
Incremental commits: Each step above should be a separate commit
Test between steps: Run
cargo testafter each major changeKeep old code commented: During transition, keep old implementations as comments until validation complete
Estimated Effort
Additional Context
Why This Matters:
The current duplication creates technical debt that undermines the library-first architecture goal. External consumers cannot trust the library export API if it's untested and unused even by the project's own CLI.
Design Philosophy:
The CLI should be a thin layer demonstrating how to use the library, not reimplementing library functionality. This makes the project:
Issue Created By: AI code review (CodeRabbit)
Analysis Methodology: Static code analysis, test execution, line-by-line comparison