turbovault-core 1.5.0

Core data models and types for TurboVault Server
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! Winnow parsers for Obsidian task metadata.
//!
//! Supports the Obsidian Tasks emoji format and Dataview inline field format.
//! Metadata is parsed only when it appears in the trailing metadata section of
//! the task, matching the Tasks plugin behavior described in its task-format
//! docs.
//!
//! This module intentionally exposes two entry points:
//!
//! - [`parse_task_content`] parses the text after the checkbox marker. This is
//!   the path used by the pulldown-cmark parser, because pulldown-cmark already
//!   recognizes Markdown task list markers and reports the task body separately.
//! - [`parse_task_line`] parses a raw Markdown task line, including its checkbox.
//!   It is useful for standalone line parsing, parser tests, and future
//!   task-focused APIs that do not need to run the full Markdown parser first.
//!
//! Keeping both entry points in one module ensures every caller shares the same
//! metadata parser instead of reimplementing task metadata extraction.

use std::collections::HashMap;

use winnow::{
    ModalResult, Parser,
    ascii::space0,
    combinator::alt,
    error::{ContextError, ErrMode},
    token::{literal, one_of, take_while},
};

const DUE_EMOJI: &str = "📅";
const SCHEDULED_EMOJI: &str = "";
const START_EMOJI: &str = "🛫";
const DONE_EMOJI: &str = "";
const CANCELLED_EMOJI: &str = "";
const CREATED_EMOJI: &str = "";
const RECURRENCE_EMOJI: &str = "🔁";
const ON_COMPLETION_EMOJI: &str = "🏁";
const ID_EMOJI: &str = "🆔";
const DEPENDS_ON_EMOJI: &str = "";
const PRIORITY_HIGHEST_EMOJI: &str = "🔺";
const PRIORITY_HIGH_EMOJI: &str = "";
const PRIORITY_MEDIUM_EMOJI: &str = "🔼";
const PRIORITY_LOW_EMOJI: &str = "🔽";
const PRIORITY_LOWEST_EMOJI: &str = "";

/// Parsed content and trailing metadata for an Obsidian task.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedTaskMetadata {
    pub description: String,
    pub due: Option<String>,
    pub scheduled: Option<String>,
    pub start: Option<String>,
    pub done: Option<String>,
    pub cancelled: Option<String>,
    pub created: Option<String>,
    pub priority: Option<char>,
    pub recurrence: Option<String>,
    pub on_completion: Option<String>,
    pub id: Option<String>,
    pub depends_on: Vec<String>,
    pub tags: Vec<String>,
    pub block_ref: Option<String>,
    pub metadata: HashMap<String, String>,
}

/// A parsed Obsidian task line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Task {
    /// Status character inside the checkbox.
    pub status: char,
    /// Human-readable description with trailing metadata removed.
    pub description: String,
    pub due: Option<String>,
    pub scheduled: Option<String>,
    pub start: Option<String>,
    pub done: Option<String>,
    pub cancelled: Option<String>,
    pub created: Option<String>,
    pub priority: Option<char>,
    pub recurrence: Option<String>,
    pub on_completion: Option<String>,
    pub id: Option<String>,
    pub depends_on: Vec<String>,
    pub tags: Vec<String>,
    pub block_ref: Option<String>,
    /// Dataview inline fields, including custom fields.
    pub metadata: HashMap<String, String>,
}

/// Parse a complete Obsidian task line, including the checkbox marker.
///
/// This is not used by the pulldown-cmark integration path, which receives task
/// content after Markdown has already identified the checkbox. It exists as a
/// small standalone API for callers that have a raw task line and want the same
/// metadata parsing behavior as the full document parser.
///
/// # Errors
///
/// Returns `Err` when the line does not start with a supported task checkbox.
pub fn parse_task_line(input: &str) -> Result<Task, String> {
    let mut input = input.trim();
    let status = parse_checkbox(&mut input).map_err(|e| format!("expected checkbox: {e}"))?;
    let parsed = parse_task_content(input);

    Ok(Task {
        status,
        description: parsed.description,
        due: parsed.due,
        scheduled: parsed.scheduled,
        start: parsed.start,
        done: parsed.done,
        cancelled: parsed.cancelled,
        created: parsed.created,
        priority: parsed.priority,
        recurrence: parsed.recurrence,
        on_completion: parsed.on_completion,
        id: parsed.id,
        depends_on: parsed.depends_on,
        tags: parsed.tags,
        block_ref: parsed.block_ref,
        metadata: parsed.metadata,
    })
}

/// Parse task text after the checkbox marker.
///
/// The returned description is the original content before the trailing metadata
/// section. Metadata-like text in the middle of the description is preserved.
#[must_use]
pub fn parse_task_content(content: &str) -> ParsedTaskMetadata {
    let meta_start = find_metadata_start(content);
    let mut parsed = parse_metadata_section(content[meta_start..].trim_start());
    parsed.description = content[..meta_start].trim_end().to_string();
    parsed
}

fn parse_checkbox(input: &mut &str) -> ModalResult<char> {
    let _ = space0.parse_next(input)?;
    let _ = one_of(['-', '*', '+']).parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let _ = literal("[").parse_next(input)?;
    let status = one_of([' ', 'x', 'X', '-', '>', '<', '/']).parse_next(input)?;
    let _ = literal("]").parse_next(input)?;
    let _ = space0.parse_next(input)?;
    Ok(status)
}

fn find_metadata_start(content: &str) -> usize {
    let mut in_word = false;
    for (i, c) in content.char_indices() {
        if c.is_whitespace() {
            in_word = false;
        } else if !in_word {
            in_word = true;
            let rest = &content[i..];
            if starts_metadata_token(rest)
                && !previous_token_is_priority(&content[..i])
                && is_pure_metadata(rest)
            {
                return i;
            }
        }
    }
    content.len()
}

fn previous_token_is_priority(prefix: &str) -> bool {
    prefix
        .split_whitespace()
        .next_back()
        .is_some_and(is_priority_emoji)
}

fn is_pure_metadata(s: &str) -> bool {
    let mut input = s;
    let mut seen_priority = false;
    let mut seen_item = false;
    loop {
        if input.is_empty() {
            return seen_item;
        }

        if consume_metadata_separator(&mut input).is_err() {
            return false;
        }

        match parse_one_metadata_item(&mut input) {
            Ok(item) => {
                seen_item = true;
                if item.priority.is_some() {
                    if seen_priority {
                        return false;
                    }
                    seen_priority = true;
                }
            }
            Err(_) => return false,
        }
    }
}

fn parse_metadata_section(meta_str: &str) -> ParsedTaskMetadata {
    let mut result = ParsedTaskMetadata::default();
    let mut input = meta_str;

    loop {
        if input.is_empty() {
            break;
        }

        if consume_metadata_separator(&mut input).is_err() {
            break;
        }

        match parse_one_metadata_item(&mut input) {
            Ok(item) => merge_metadata(&mut result, item),
            Err(_) => break,
        }
    }

    result
}

fn consume_metadata_separator(input: &mut &str) -> ModalResult<()> {
    let _ = space0.parse_next(input)?;

    if input.starts_with(',') {
        let _ = literal(",").parse_next(input)?;
        let _ = space0.parse_next(input)?;
    }

    Ok(())
}

fn parse_one_metadata_item(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    alt((
        parse_emoji_date_field,
        parse_recurrence_field,
        parse_on_completion_field,
        parse_id_field,
        parse_depends_on_field,
        parse_dataview_field,
        parse_priority_emoji,
        parse_tag_field,
        parse_block_ref_field,
    ))
    .parse_next(input)
}

fn parse_emoji_date_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let key = alt((
        literal(DUE_EMOJI).value("due"),
        literal(SCHEDULED_EMOJI).value("scheduled"),
        literal(START_EMOJI).value("start"),
        literal(DONE_EMOJI).value("done"),
        literal(CANCELLED_EMOJI).value("cancelled"),
        literal(CREATED_EMOJI).value("created"),
    ))
    .parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let value: &str = take_while(1.., |c: char| !c.is_whitespace()).parse_next(input)?;

    let mut meta = ParsedTaskMetadata::default();
    set_standard_field(&mut meta, key, value.trim());
    Ok(meta)
}

fn parse_recurrence_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal(RECURRENCE_EMOJI).parse_next(input)?;
    let _ = space0.parse_next(input)?;

    let mut words = Vec::new();
    loop {
        let trimmed = input.trim_start();
        if trimmed.is_empty() || starts_metadata_token(trimmed) {
            break;
        }
        let word: &str = take_while(1.., |c: char| !c.is_whitespace()).parse_next(input)?;
        words.push(word);
        let _ = space0.parse_next(input)?;
    }

    if words.is_empty() {
        return Err(ErrMode::Backtrack(ContextError::new()));
    }

    Ok(ParsedTaskMetadata {
        recurrence: Some(words.join(" ")),
        ..Default::default()
    })
}

fn parse_on_completion_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal(ON_COMPLETION_EMOJI).parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let value: &str = take_while(1.., |c: char| !c.is_whitespace()).parse_next(input)?;

    Ok(ParsedTaskMetadata {
        on_completion: Some(value.trim().to_ascii_lowercase()),
        ..Default::default()
    })
}

fn parse_id_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal(ID_EMOJI).parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let id = parse_task_id(input)?;

    Ok(ParsedTaskMetadata {
        id: Some(id.to_string()),
        ..Default::default()
    })
}

fn parse_depends_on_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal(DEPENDS_ON_EMOJI).parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let value: &str = take_while(1.., |c: char| !c.is_whitespace()).parse_next(input)?;

    Ok(ParsedTaskMetadata {
        depends_on: split_dependency_ids(value),
        ..Default::default()
    })
}

fn parse_task_id<'i>(input: &mut &'i str) -> ModalResult<&'i str> {
    take_while(1.., |c: char| c.is_alphanumeric() || c == '-' || c == '_').parse_next(input)
}

fn parse_dataview_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let closing = parse_dataview_open(input)?;
    let key: &str = take_while(1.., is_dataview_key_char).parse_next(input)?;
    let _ = literal("::").parse_next(input)?;
    let _ = space0.parse_next(input)?;
    let value = parse_balanced_inline_value(input, closing)?;
    let _ = one_of([closing]).parse_next(input)?;

    let key = key.trim().to_ascii_lowercase();
    let value = value.trim().to_string();
    let mut meta = ParsedTaskMetadata::default();
    set_standard_field(&mut meta, &key, &value);
    meta.metadata.insert(key, value);
    Ok(meta)
}

fn parse_dataview_open(input: &mut &str) -> ModalResult<char> {
    let opening = one_of(['[', '(']).parse_next(input)?;
    Ok(match opening {
        '[' => ']',
        '(' => ')',
        _ => unreachable!(),
    })
}

fn is_dataview_key_char(c: char) -> bool {
    c.is_alphanumeric() || c == '_' || c == '-'
}

fn parse_balanced_inline_value<'i>(input: &mut &'i str, closing: char) -> ModalResult<&'i str> {
    let start = *input;
    let mut square_depth = 0usize;
    let mut paren_depth = 0usize;
    let mut consumed = 0usize;
    let mut found_close = false;

    for c in start.chars() {
        match c {
            '[' => {
                square_depth += 1;
                consumed += c.len_utf8();
            }
            ']' if closing == ']' && square_depth == 0 && paren_depth == 0 => {
                found_close = true;
                break;
            }
            ']' => {
                square_depth = square_depth.saturating_sub(1);
                consumed += c.len_utf8();
            }
            '(' => {
                paren_depth += 1;
                consumed += c.len_utf8();
            }
            ')' if closing == ')' && square_depth == 0 && paren_depth == 0 => {
                found_close = true;
                break;
            }
            ')' => {
                paren_depth = paren_depth.saturating_sub(1);
                consumed += c.len_utf8();
            }
            _ => consumed += c.len_utf8(),
        }
    }

    if !found_close {
        return Err(ErrMode::Backtrack(ContextError::new()));
    }

    let value = &start[..consumed];
    *input = &start[consumed..];
    Ok(value)
}

fn parse_priority_emoji(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let priority = alt((
        literal(PRIORITY_HIGH_EMOJI).value(''),
        literal(PRIORITY_HIGHEST_EMOJI).value('🔺'),
        literal(PRIORITY_MEDIUM_EMOJI).value('🔼'),
        literal(PRIORITY_LOW_EMOJI).value('🔽'),
        literal(PRIORITY_LOWEST_EMOJI).value(''),
    ))
    .parse_next(input)?;

    Ok(ParsedTaskMetadata {
        priority: Some(priority),
        ..Default::default()
    })
}

fn parse_tag_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal("#").parse_next(input)?;
    let name: &str = take_while(1.., |c: char| {
        c.is_alphanumeric() || c == '-' || c == '_' || c == '/'
    })
    .parse_next(input)?;

    if !name.chars().any(|c| c.is_alphabetic()) {
        return Err(ErrMode::Backtrack(ContextError::new()));
    }

    let mut meta = ParsedTaskMetadata::default();
    meta.tags.push(name.to_string());
    Ok(meta)
}

fn parse_block_ref_field(input: &mut &str) -> ModalResult<ParsedTaskMetadata> {
    let _ = literal("^").parse_next(input)?;
    let id: &str =
        take_while(1.., |c: char| c.is_alphanumeric() || c == '-' || c == '_').parse_next(input)?;

    Ok(ParsedTaskMetadata {
        block_ref: Some(id.to_string()),
        ..Default::default()
    })
}

fn starts_metadata_token(s: &str) -> bool {
    s.starts_with(DUE_EMOJI)
        || s.starts_with(SCHEDULED_EMOJI)
        || s.starts_with(START_EMOJI)
        || s.starts_with(DONE_EMOJI)
        || s.starts_with(CANCELLED_EMOJI)
        || s.starts_with(CREATED_EMOJI)
        || s.starts_with(RECURRENCE_EMOJI)
        || s.starts_with(ON_COMPLETION_EMOJI)
        || s.starts_with(ID_EMOJI)
        || s.starts_with(DEPENDS_ON_EMOJI)
        || is_priority_metadata_start(s)
        || s.starts_with('#')
        || s.starts_with('^')
        || ((s.starts_with('[') || s.starts_with('(')) && s.contains("::"))
}

fn is_priority_metadata_start(s: &str) -> bool {
    s.starts_with(PRIORITY_HIGH_EMOJI)
        || s.starts_with(PRIORITY_HIGHEST_EMOJI)
        || s.starts_with(PRIORITY_MEDIUM_EMOJI)
        || s.starts_with(PRIORITY_LOW_EMOJI)
        || s.starts_with(PRIORITY_LOWEST_EMOJI)
}

fn is_priority_emoji(s: &str) -> bool {
    matches!(
        s,
        PRIORITY_HIGH_EMOJI
            | PRIORITY_HIGHEST_EMOJI
            | PRIORITY_MEDIUM_EMOJI
            | PRIORITY_LOW_EMOJI
            | PRIORITY_LOWEST_EMOJI
    )
}

fn set_standard_field(meta: &mut ParsedTaskMetadata, key: &str, value: &str) {
    match key {
        "due" => meta.due = Some(value.to_string()),
        "scheduled" => meta.scheduled = Some(value.to_string()),
        "start" => meta.start = Some(value.to_string()),
        "done" | "completion" => meta.done = Some(value.to_string()),
        "cancelled" | "canceled" => meta.cancelled = Some(value.to_string()),
        "created" => meta.created = Some(value.to_string()),
        "recurrence" | "repeat" => meta.recurrence = Some(value.to_string()),
        "oncompletion" => meta.on_completion = Some(value.to_ascii_lowercase()),
        "id" => meta.id = Some(value.to_string()),
        "dependson" => meta.depends_on = split_dependency_ids(value),
        "priority" => meta.priority = priority_from_dataview_value(value),
        _ => {}
    }
}

fn split_dependency_ids(value: &str) -> Vec<String> {
    value
        .split(',')
        .map(str::trim)
        .filter(|id| !id.is_empty())
        .map(ToString::to_string)
        .collect()
}

fn priority_from_dataview_value(value: &str) -> Option<char> {
    match value.trim().to_ascii_lowercase().as_str() {
        "highest" => Some('🔺'),
        "high" => Some(''),
        "medium" => Some('🔼'),
        "low" => Some('🔽'),
        "lowest" => Some(''),
        "normal" | "none" => None,
        _ => None,
    }
}

fn merge_metadata(dst: &mut ParsedTaskMetadata, src: ParsedTaskMetadata) {
    if src.due.is_some() {
        dst.due = src.due;
    }
    if src.scheduled.is_some() {
        dst.scheduled = src.scheduled;
    }
    if src.start.is_some() {
        dst.start = src.start;
    }
    if src.done.is_some() {
        dst.done = src.done;
    }
    if src.cancelled.is_some() {
        dst.cancelled = src.cancelled;
    }
    if src.created.is_some() {
        dst.created = src.created;
    }
    if src.priority.is_some() {
        dst.priority = src.priority;
    }
    if src.recurrence.is_some() {
        dst.recurrence = src.recurrence;
    }
    if src.on_completion.is_some() {
        dst.on_completion = src.on_completion;
    }
    if src.id.is_some() {
        dst.id = src.id;
    }
    if !src.depends_on.is_empty() {
        dst.depends_on = src.depends_on;
    }
    if src.block_ref.is_some() {
        dst.block_ref = src.block_ref;
    }
    dst.tags.extend(src.tags);
    dst.metadata.extend(src.metadata);
}

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

    #[test]
    fn parses_basic_emoji_metadata() {
        let task = parse_task_line("- [ ] Buy milk 📅 2025-05-15").unwrap();
        assert_eq!(task.description, "Buy milk");
        assert_eq!(task.due.as_deref(), Some("2025-05-15"));
    }

    #[test]
    fn parses_dataview_metadata() {
        let task = parse_task_line("- [ ] Finish report [due:: 2025-06-01]").unwrap();
        assert_eq!(task.description, "Finish report");
        assert_eq!(task.due.as_deref(), Some("2025-06-01"));
        assert_eq!(
            task.metadata.get("due").map(String::as_str),
            Some("2025-06-01")
        );
    }

    #[test]
    fn parses_mixed_emoji_and_dataview() {
        let task = parse_task_line(
            "- [x] Do the thing 📅 2025-05-01 [scheduled:: 2025-05-03] 🔁 every weekday",
        )
        .unwrap();
        assert_eq!(task.description, "Do the thing");
        assert_eq!(task.due.as_deref(), Some("2025-05-01"));
        assert_eq!(task.scheduled.as_deref(), Some("2025-05-03"));
        assert_eq!(task.recurrence.as_deref(), Some("every weekday"));
        assert_eq!(task.status, 'x');
    }

    #[test]
    fn parses_comma_separated_dataview_fields() {
        let task = parse_task_line(
            "- [ ] This is a task [priority:: high], [start:: 2023-04-24], [due:: 2023-05-01]",
        )
        .unwrap();

        assert_eq!(task.description, "This is a task");
        assert_eq!(task.priority, Some(''));
        assert_eq!(task.start.as_deref(), Some("2023-04-24"));
        assert_eq!(task.due.as_deref(), Some("2023-05-01"));
    }

    #[test]
    fn parses_dataview_completion_repeat_and_priority() {
        let task = parse_task_line(
            "- [ ] Ship it [completion:: 2025-07-01] [repeat:: every month] [priority:: high]",
        )
        .unwrap();
        assert_eq!(task.description, "Ship it");
        assert_eq!(task.done.as_deref(), Some("2025-07-01"));
        assert_eq!(task.recurrence.as_deref(), Some("every month"));
        assert_eq!(task.priority, Some(''));
    }

    #[test]
    fn parses_dataview_on_completion_and_dependencies() {
        let task = parse_task_line(
            "- [ ] Blocked work [repeat:: every day], [onCompletion:: delete], [id:: dcf64c], [dependsOn:: abc123,def456]",
        )
        .unwrap();

        assert_eq!(task.description, "Blocked work");
        assert_eq!(task.recurrence.as_deref(), Some("every day"));
        assert_eq!(task.on_completion.as_deref(), Some("delete"));
        assert_eq!(task.id.as_deref(), Some("dcf64c"));
        assert_eq!(
            task.depends_on,
            vec!["abc123".to_string(), "def456".to_string()]
        );
    }

    #[test]
    fn parses_emoji_on_completion_and_dependencies() {
        let task =
            parse_task_line("- [ ] Blocked work 🏁 keep 🆔 dcf64c ⛔ abc123,def456").unwrap();

        assert_eq!(task.description, "Blocked work");
        assert_eq!(task.on_completion.as_deref(), Some("keep"));
        assert_eq!(task.id.as_deref(), Some("dcf64c"));
        assert_eq!(
            task.depends_on,
            vec!["abc123".to_string(), "def456".to_string()]
        );
    }

    #[test]
    fn parses_tags_and_block_ref_after_metadata() {
        let task = parse_task_line("- [ ] Task description 📅 2025-05-10 #urgent #work ^task-123")
            .unwrap();
        assert_eq!(task.description, "Task description");
        assert_eq!(task.due.as_deref(), Some("2025-05-10"));
        assert_eq!(task.tags, vec!["urgent".to_string(), "work".to_string()]);
        assert_eq!(task.block_ref.as_deref(), Some("task-123"));
    }

    #[test]
    fn ignores_false_positive_in_description() {
        let task =
            parse_task_line("- [ ] Review the [due date] section and 📅 2025-04-30").unwrap();
        assert_eq!(task.description, "Review the [due date] section and");
        assert_eq!(task.due.as_deref(), Some("2025-04-30"));
    }

    #[test]
    fn duplicate_fields_rightmost_wins() {
        let task = parse_task_line("- [ ] Task 📅 2025-05-01 📅 2025-06-15").unwrap();
        assert_eq!(task.due.as_deref(), Some("2025-06-15"));
    }

    #[test]
    fn parses_priority() {
        let task = parse_task_line("- [ ] High priority task ⏫").unwrap();
        assert_eq!(task.priority, Some(''));
        assert_eq!(task.description, "High priority task");
    }

    #[test]
    fn keeps_middle_metadata_in_description() {
        let task = parse_task_line(
            "- [ ] 📅 2025-05-01 This date in middle should be ignored ⏳ 2025-05-10",
        )
        .unwrap();
        assert_eq!(
            task.description,
            "📅 2025-05-01 This date in middle should be ignored"
        );
        assert_eq!(task.scheduled.as_deref(), Some("2025-05-10"));
        assert!(task.due.is_none());
    }

    #[test]
    fn parses_dataview_value_with_wikilink() {
        let task = parse_task_line(
            "- [ ] Review PR #123 [project:: [[Team Work]]] 📅 2025-05-20 🔼 #review ^pr-123",
        )
        .unwrap();
        assert_eq!(task.description, "Review PR #123");
        assert_eq!(task.due.as_deref(), Some("2025-05-20"));
        assert_eq!(task.priority, Some('🔼'));
        assert_eq!(task.tags, vec!["review".to_string()]);
        assert_eq!(task.block_ref.as_deref(), Some("pr-123"));
        assert_eq!(
            task.metadata.get("project").map(String::as_str),
            Some("[[Team Work]]")
        );
    }

    #[test]
    fn supports_parenthesized_dataview_fields() {
        let task = parse_task_line("- [ ] Call Alex (due:: 2025-06-01)").unwrap();
        assert_eq!(task.description, "Call Alex");
        assert_eq!(task.due.as_deref(), Some("2025-06-01"));
    }

    #[test]
    fn keeps_bare_recurrence_marker_in_description() {
        let task = parse_task_line("- [ ] Decide recurrence 🔁").unwrap();
        assert_eq!(task.description, "Decide recurrence 🔁");
        assert!(task.recurrence.is_none());
    }
}