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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Data export command for external analysis and backup.
//!
//! Provides comprehensive data export functionality supporting multiple output formats and data types for external analysis, backup, and integration.
//!
//! ## Features
//!
//! - **Export Formats**: CSV, JSON, Excel with formatting and multiple sheets
//! - **Data Types**: Reports, tasks, summaries, and complete data export
//! - **Flexible Output**: Custom file paths and automatic naming
//! - **Date Filtering**: Export data for specific date ranges
//!
//! ## Usage
//!
//! ```bash
//! # Export tasks to CSV
//! kasl export tasks --format csv
//!
//! # Export today's report to Excel
//! kasl export report --format xlsx
//!
//! # Export with custom filename
//! kasl export tasks --format json --output my_tasks.json
//! ```
use crate::;
use Result;
use ;
use Args;
use PathBuf;
/// Command-line arguments for the export command.
///
/// The export command provides flexible options for data extraction,
/// supporting different formats, data types, and output destinations.
/// Executes the data export command.
///
/// Orchestrates the complete export process including date parsing, exporter
/// initialization, data processing, file generation, and user feedback.
/// - Data format conversion errors
/// - Output file write failures
///
/// # Arguments
///
/// * `args` - Parsed command-line arguments specifying export parameters
///
/// # Returns
///
/// Returns `Ok(())` on successful export completion, or an error if
/// any step in the export process fails.
///
/// # Examples
///
/// ```bash
/// # Export today's report as CSV
/// kasl export report --format csv
///
/// # Export tasks from specific date as JSON
/// kasl export tasks --format json --date 2025-01-15
///
/// # Export monthly summary to Excel with custom filename
/// kasl export summary --format excel --output monthly_report.xlsx
///
/// # Export all data for backup purposes
/// kasl export all --format json --output backup_2025_01.json
/// ```
///
/// # Output Files
///
/// Generated files include:
/// - **Metadata**: Export timestamp, data range, format version
/// - **Data Records**: Requested information in chosen format
/// - **Summary Statistics**: Totals, averages, and key metrics
/// - **Format-Specific Features**: Charts (Excel), structured nesting (JSON)
pub async
/// Resolves a default output path for report exports from configuration.
///
/// This is only applied to [`ExportData::Report`] exports when no explicit
/// `--output` was provided and a report output directory is configured. The
/// file name is built from the configured template (defaulting to
/// `daily_report_{date}{seq}`), where `{date}` is the report date and `{seq}`
/// is a per-day sequence suffix (empty for the first file of the day, then
/// `_2`, `_3`, … for subsequent files). The chosen path is guaranteed not to
/// overwrite an existing file.
///
/// Returns `Ok(None)` to defer to the exporter's built-in default naming when
/// the export is not a report or no report directory is configured.
/// Parses a date string supporting both 'today' and ISO format.
///
/// This utility function provides consistent date parsing across the export
/// command, handling both user-friendly keywords and explicit date specifications.
///
/// ## Supported Formats
///
/// - **today** (case-insensitive): Returns current local date
/// - **YYYY-MM-DD**: ISO 8601 date format (e.g., 2025-01-15)
///
/// ## Use Cases
///
/// Different date specifications serve different purposes:
/// - `today`: Quick exports of current work data
/// - Specific dates: Historical analysis, backup creation, data migration
/// - Recent dates: Weekly or monthly review processes
///
/// # Arguments
///
/// * `date_str` - Date string to parse, either 'today' or 'YYYY-MM-DD'
///
/// # Returns
///
/// Returns the parsed `NaiveDate` on success, or an error if the date
/// string is malformed or represents an invalid date.
///
/// # Error Scenarios
///
/// - Malformed date strings (e.g., `2025-13-45`, `invalid-date`)
/// - Wrong date formats (e.g., `01/15/2025`, `15-01-2025`)
/// - Non-existent dates (e.g., `2025-02-30`, `2025-04-31`)
/// - Out-of-range values (e.g., month > 12, day > 31)
///
/// # Examples
///
/// ```text
/// let today = parse_date("today")?; // Current date
/// let christmas = parse_date("2025-12-25")?; // Specific holiday
/// let start_year = parse_date("2025-01-01")?; // Year beginning
/// ```