taskdn 0.1.0

Rust library for parsing, querying, and manipulating Taskdn task files
Documentation
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Project entity and related types.

use super::FileReference;
use chrono::NaiveDate;
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;

/// Status of a project.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum ProjectStatus {
    /// Project is being planned.
    #[default]
    Planning,
    /// Project is ready to start.
    Ready,
    /// Project is blocked on something.
    Blocked,
    /// Project is actively being worked on.
    InProgress,
    /// Project is temporarily paused.
    Paused,
    /// Project is complete.
    Done,
}

impl ProjectStatus {
    /// Returns true if this status represents a completed state.
    #[must_use]
    pub fn is_completed(&self) -> bool {
        matches!(self, Self::Done)
    }

    /// Returns true if this status represents an active state.
    #[must_use]
    pub fn is_active(&self) -> bool {
        matches!(self, Self::Ready | Self::InProgress | Self::Blocked)
    }

    /// Returns the canonical string representation (lowercase, hyphenated).
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Planning => "planning",
            Self::Ready => "ready",
            Self::Blocked => "blocked",
            Self::InProgress => "in-progress",
            Self::Paused => "paused",
            Self::Done => "done",
        }
    }
}

impl FromStr for ProjectStatus {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().replace('_', "-").as_str() {
            "planning" => Ok(Self::Planning),
            "ready" => Ok(Self::Ready),
            "blocked" => Ok(Self::Blocked),
            "in-progress" => Ok(Self::InProgress),
            "paused" => Ok(Self::Paused),
            "done" => Ok(Self::Done),
            _ => Err(format!("invalid project status: {s}")),
        }
    }
}

impl std::fmt::Display for ProjectStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// A parsed project file.
#[derive(Debug, Clone, PartialEq)]
pub struct Project {
    /// Absolute path to the project file.
    pub path: PathBuf,

    // Required
    /// The project title.
    pub title: String,

    // Optional
    /// Unique identifier (for external references).
    pub unique_id: Option<String>,
    /// Current status of the project.
    pub status: Option<ProjectStatus>,
    /// Brief description of the project.
    pub description: Option<String>,
    /// Reference to the area this project belongs to.
    pub area: Option<FileReference>,
    /// When the project started.
    pub start_date: Option<NaiveDate>,
    /// When the project is expected to end.
    pub end_date: Option<NaiveDate>,
    /// Projects that must complete before this one can proceed.
    pub blocked_by: Vec<FileReference>,

    /// Markdown body.
    pub body: String,
    /// Unknown frontmatter fields.
    pub extra: HashMap<String, serde_yaml::Value>,
}

impl Project {
    /// Returns the filename without path (e.g., "my-project.md").
    #[must_use]
    pub fn filename(&self) -> &str {
        self.path.file_name().and_then(|n| n.to_str()).unwrap_or("")
    }
}

/// Parsed project content without a file path.
#[derive(Debug, Clone, PartialEq)]
pub struct ParsedProject {
    /// The project title.
    pub title: String,
    /// Unique identifier.
    pub unique_id: Option<String>,
    /// Current status.
    pub status: Option<ProjectStatus>,
    /// Brief description.
    pub description: Option<String>,
    /// Reference to the area.
    pub area: Option<FileReference>,
    /// Start date.
    pub start_date: Option<NaiveDate>,
    /// End date.
    pub end_date: Option<NaiveDate>,
    /// Blocking projects.
    pub blocked_by: Vec<FileReference>,
    /// Markdown body.
    pub body: String,
    /// Unknown frontmatter fields.
    pub extra: HashMap<String, serde_yaml::Value>,
}

impl ParsedProject {
    /// Convert to a Project by associating with a file path.
    #[must_use]
    pub fn with_path(self, path: impl Into<PathBuf>) -> Project {
        Project {
            path: path.into(),
            title: self.title,
            unique_id: self.unique_id,
            status: self.status,
            description: self.description,
            area: self.area,
            start_date: self.start_date,
            end_date: self.end_date,
            blocked_by: self.blocked_by,
            body: self.body,
            extra: self.extra,
        }
    }
}

/// Data for creating a new project.
#[derive(Debug, Clone, Default)]
pub struct NewProject {
    /// The project title (required).
    pub title: String,
    /// Optional custom filename.
    pub filename: Option<String>,
    /// Initial status.
    pub status: Option<ProjectStatus>,
    /// Brief description.
    pub description: Option<String>,
    /// Reference to the area.
    pub area: Option<FileReference>,
    /// Start date.
    pub start_date: Option<NaiveDate>,
    /// End date.
    pub end_date: Option<NaiveDate>,
    /// Markdown body.
    pub body: String,
    /// Additional frontmatter fields.
    pub extra: HashMap<String, serde_yaml::Value>,
}

impl NewProject {
    /// Create a new project with the given title.
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            ..Default::default()
        }
    }

    /// Set a custom filename.
    #[must_use]
    pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
        self.filename = Some(filename.into());
        self
    }

    /// Set the status.
    #[must_use]
    pub fn with_status(mut self, status: ProjectStatus) -> Self {
        self.status = Some(status);
        self
    }

    /// Set the description.
    #[must_use]
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Assign to an area.
    #[must_use]
    pub fn in_area(mut self, area: impl Into<FileReference>) -> Self {
        self.area = Some(area.into());
        self
    }

    /// Set the start date.
    #[must_use]
    pub fn with_start_date(mut self, date: NaiveDate) -> Self {
        self.start_date = Some(date);
        self
    }

    /// Set the end date.
    #[must_use]
    pub fn with_end_date(mut self, date: NaiveDate) -> Self {
        self.end_date = Some(date);
        self
    }

    /// Set the body content.
    #[must_use]
    pub fn with_body(mut self, body: impl Into<String>) -> Self {
        self.body = body.into();
        self
    }
}

/// Partial updates for a project.
#[derive(Debug, Clone, Default)]
pub struct ProjectUpdates {
    /// New title.
    pub title: Option<String>,
    /// New status.
    pub status: Option<Option<ProjectStatus>>,
    /// New description.
    pub description: Option<Option<String>>,
    /// New area reference.
    pub area: Option<Option<FileReference>>,
    /// New start date.
    pub start_date: Option<Option<NaiveDate>>,
    /// New end date.
    pub end_date: Option<Option<NaiveDate>>,
}

impl ProjectUpdates {
    /// Create a new empty updates struct.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a new title.
    #[must_use]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Set a new status.
    #[must_use]
    pub fn status(mut self, status: ProjectStatus) -> Self {
        self.status = Some(Some(status));
        self
    }

    /// Clear the status.
    #[must_use]
    pub fn clear_status(mut self) -> Self {
        self.status = Some(None);
        self
    }

    /// Set a new description.
    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(Some(description.into()));
        self
    }

    /// Clear the description.
    #[must_use]
    pub fn clear_description(mut self) -> Self {
        self.description = Some(None);
        self
    }

    /// Set a new area reference.
    #[must_use]
    pub fn area(mut self, area: impl Into<FileReference>) -> Self {
        self.area = Some(Some(area.into()));
        self
    }

    /// Clear the area reference.
    #[must_use]
    pub fn clear_area(mut self) -> Self {
        self.area = Some(None);
        self
    }

    /// Set a new start date.
    #[must_use]
    pub fn start_date(mut self, date: NaiveDate) -> Self {
        self.start_date = Some(Some(date));
        self
    }

    /// Clear the start date.
    #[must_use]
    pub fn clear_start_date(mut self) -> Self {
        self.start_date = Some(None);
        self
    }

    /// Set a new end date.
    #[must_use]
    pub fn end_date(mut self, date: NaiveDate) -> Self {
        self.end_date = Some(Some(date));
        self
    }

    /// Clear the end date.
    #[must_use]
    pub fn clear_end_date(mut self) -> Self {
        self.end_date = Some(None);
        self
    }

    /// Returns true if no updates are specified.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.title.is_none()
            && self.status.is_none()
            && self.description.is_none()
            && self.area.is_none()
            && self.start_date.is_none()
            && self.end_date.is_none()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod project_status {
        use super::*;

        #[test]
        fn parse_all_statuses() {
            assert_eq!(
                "planning".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::Planning
            );
            assert_eq!(
                "ready".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::Ready
            );
            assert_eq!(
                "blocked".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::Blocked
            );
            assert_eq!(
                "in-progress".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::InProgress
            );
            assert_eq!(
                "paused".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::Paused
            );
            assert_eq!(
                "done".parse::<ProjectStatus>().unwrap(),
                ProjectStatus::Done
            );
        }

        #[test]
        fn is_completed() {
            assert!(ProjectStatus::Done.is_completed());
            assert!(!ProjectStatus::Planning.is_completed());
            assert!(!ProjectStatus::InProgress.is_completed());
        }

        #[test]
        fn is_active() {
            assert!(ProjectStatus::Ready.is_active());
            assert!(ProjectStatus::InProgress.is_active());
            assert!(ProjectStatus::Blocked.is_active());
            assert!(!ProjectStatus::Planning.is_active());
            assert!(!ProjectStatus::Done.is_active());
        }
    }

    mod project {
        use super::*;
        use std::path::Path;

        fn sample_project(path: impl AsRef<Path>) -> Project {
            Project {
                path: path.as_ref().to_path_buf(),
                title: "Test Project".to_string(),
                unique_id: None,
                status: None,
                description: None,
                area: None,
                start_date: None,
                end_date: None,
                blocked_by: Vec::new(),
                body: String::new(),
                extra: HashMap::new(),
            }
        }

        #[test]
        fn filename_extracts_correctly() {
            let project = sample_project("/path/to/projects/my-project.md");
            assert_eq!(project.filename(), "my-project.md");
        }
    }

    mod new_project {
        use super::*;

        #[test]
        fn builder_pattern() {
            let project = NewProject::new("My Project")
                .with_status(ProjectStatus::Planning)
                .in_area("[[Work]]")
                .with_description("A test project");

            assert_eq!(project.title, "My Project");
            assert_eq!(project.status, Some(ProjectStatus::Planning));
            assert!(project.area.is_some());
            assert_eq!(project.description, Some("A test project".to_string()));
        }
    }

    mod project_updates {
        use super::*;

        #[test]
        fn empty_updates() {
            let updates = ProjectUpdates::new();
            assert!(updates.is_empty());
        }

        #[test]
        fn clear_area() {
            let updates = ProjectUpdates::new().clear_area();
            assert_eq!(updates.area, Some(None));
        }
    }
}