1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Worktree command implementation
//!
//! This module handles git worktree management for parallel sessions.
//!
//! # Architecture
//!
//! This module follows a functional programming architecture with clear separation of concerns:
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │ Public API (mod.rs) │
//! │ - run_worktree_command: Main entry point │
//! │ - parse_duration: Utility export │
//! └─────────────────────────────────────────────────────────────┘
//! │
//! ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │ CLI Layer (cli.rs) │
//! │ - Command routing and argument handling │
//! │ - Thin orchestration (< 30 lines per function) │
//! │ - Dependency initialization │
//! └─────────────────────────────────────────────────────────────┘
//! │ │ │
//! ▼ ▼ ▼
//! ┌──────────────┐ ┌──────────────────┐ ┌──────────────┐
//! │ Operations │ │ Presentation │ │ Utils │
//! │ (operations) │ │ (presentation) │ │ (utils) │
//! └──────────────┘ └──────────────────┘ └──────────────┘
//!
//! Business Logic Output Formatting Pure Functions
//! - list_sessions - format_table - parse_duration
//! - merge_session - format_result
//! - cleanup - format_summary
//! ```
//!
//! ## Modules
//!
//! ### `cli`
//! Command handlers that orchestrate operations and presentation.
//! - Handles CLI argument parsing
//! - Initializes dependencies (WorktreeManager, etc.)
//! - Calls operation functions
//! - Calls presentation functions for output
//! - Keeps functions thin (< 30 lines each)
//!
//! ### `operations`
//! Pure business logic functions that return structured results.
//! - Takes dependencies as parameters (dependency injection)
//! - Returns Result types with structured data
//! - No direct I/O (printing, file operations)
//! - Fully testable without mocking I/O
//! - Examples: `list_sessions_operation`, `merge_session_operation`
//!
//! ### `presentation`
//! Pure functions that format data for display.
//! - Takes data structures and returns formatted strings
//! - No I/O operations (printing done by caller)
//! - Easy to test output formatting
//! - Examples: `format_sessions_table`, `format_merge_result`
//!
//! ### `utils`
//! Stateless utility functions.
//! - Pure functions with no side effects
//! - 100% test coverage
//! - Example: `parse_duration` (parses duration strings like "1h", "7d")
//!
//! ## Design Principles
//!
//! 1. **Functional Core, Imperative Shell**
//! - Pure business logic in `operations` and `utils`
//! - I/O at edges in `cli`
//! - Clear separation makes code testable
//!
//! 2. **Dependency Injection**
//! - Functions take dependencies as parameters
//! - No global state or hidden dependencies
//! - Easy to mock for testing
//!
//! 3. **Single Responsibility**
//! - Each module has one clear purpose
//! - Small, focused functions (< 20 lines preferred)
//! - Easy to understand and maintain
//!
//! 4. **Structured Results**
//! - Operations return typed result structs
//! - Makes data flow explicit
//! - Enables clean presentation layer
//!
//! ## Examples
//!
//! ### Listing Sessions
//! ```text
//! CLI: run_worktree_ls()
//! │
//! ├─> Operations: list_sessions_operation() → SessionListResult
//! │
//! └─> Presentation: format_sessions_table() → String
//! └─> CLI: println!(formatted_output)
//! ```
//!
//! ### Merging Sessions
//! ```text
//! CLI: run_worktree_merge()
//! │
//! ├─> Operations: merge_session_operation() → MergeResult
//! │
//! └─> Presentation: format_merge_result() → String
//! └─> CLI: println!(formatted_output)
//! ```
//!
//! ## Testing Strategy
//!
//! - **Utils**: Unit tests for all pure functions (100% coverage)
//! - **Operations**: Unit tests for data structures, integration tests for operations
//! - **Presentation**: Unit tests for formatting logic with various inputs
//! - **CLI**: Integration tests for end-to-end flows
//!
//! ## Adding New Commands
//!
//! To add a new worktree command:
//!
//! 1. Add operation function in `operations.rs`
//! - Define result type if needed
//! - Write pure business logic
//! - Add unit tests
//!
//! 2. Add presentation function in `presentation.rs`
//! - Write formatting logic
//! - Add unit tests
//!
//! 3. Add CLI handler in `cli.rs`
//! - Initialize dependencies
//! - Call operation
//! - Call presentation
//! - Handle errors
//!
//! 4. Update `run_worktree_command` match statement
//!
//! 5. Add integration test
pub use run_worktree_command;
pub use parse_duration;