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
//! Shared helpers for mutation commands (`set`, `remove`, `append`, `mv`).
//!
//! These are pure structural helpers that encapsulate the patterns mutation
//! commands share:
//!
//! 1. Patching a snapshot-index entry in memory after a file is mutated.
//! 2. Renaming an index entry after a file move (key change + link graph update).
//! 3. Flushing the index to disk when it has been dirtied.
//! 4. Collapsing a single-element `results` vec to a bare JSON object (vs. an array).
use Result;
use IndexMap;
use Value;
use Path;
use extract_tags;
use ;
// ---------------------------------------------------------------------------
// Index-entry patch
// ---------------------------------------------------------------------------
/// Patch the snapshot-index entry for `rel_path` after its frontmatter was mutated.
///
/// Extracts the new tag set from the already-updated `props`, writes
/// `properties`, `tags`, and `modified` back into the in-memory entry, then
/// marks `index_dirty` so the caller knows a save is needed.
///
/// This is a no-op when `snapshot_index` is `None` or when the entry for
/// `rel_path` is not present in the index (e.g. a newly created file that
/// hasn't been indexed yet).
// ---------------------------------------------------------------------------
// Index-entry rename (for `mv`)
// ---------------------------------------------------------------------------
/// Rename an index entry after a file move, updating the link graph and
/// re-scanning affected files so that backlink/link queries remain accurate.
///
/// `rewritten_files` lists the vault-relative paths of files whose links were
/// rewritten by the move — their index entries are re-scanned to pick up
/// updated outbound links.
///
/// This is a no-op when `snapshot_index` is `None`.
// ---------------------------------------------------------------------------
// Index flush
// ---------------------------------------------------------------------------
/// Flush the snapshot index to `index_path` when `index_dirty` is `true`.
///
/// This is a no-op when `index_dirty` is `false`, or when either `snapshot_index`
/// or `index_path` is `None`.
// ---------------------------------------------------------------------------
// Output shaping
// ---------------------------------------------------------------------------
/// Collapse a `results` vec to a bare JSON object when it contains exactly one
/// entry, or return it as a JSON array otherwise.
///
/// All three mutation commands (`set`, `remove`, `append`) use this pattern:
/// a single mutation produces a plain object; multiple mutations produce an array.