hyalo-cli 0.14.0

CLI for exploring and managing Markdown knowledge bases with YAML frontmatter
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
#![allow(clippy::missing_errors_doc)]
use anyhow::{Context, Result};
use std::path::Path;

use crate::commands::{FilesOrOutcome, collect_files};
use crate::output::{CommandOutcome, Format};
use hyalo_core::filter::extract_tags;
use hyalo_core::frontmatter;
use hyalo_core::index::{SnapshotIndex, VaultIndex, format_modified};
use hyalo_core::types::PropertySummaryEntry;
use serde::Serialize;

/// Aggregate property summary using pre-scanned index data.
///
/// `file_filter` is an optional list of vault-relative paths to include.
/// When `None` (or an empty slice), all index entries are used.
/// `limit` caps how many entries are returned (`None` = no cap).
pub fn properties_summary(
    index: &dyn VaultIndex,
    file_filter: Option<&[String]>,
    format: Format,
    limit: Option<usize>,
) -> Result<CommandOutcome> {
    let mut agg: std::collections::BTreeMap<(String, String), usize> =
        std::collections::BTreeMap::new();

    for entry in index.entries() {
        // Apply optional file-level filter
        if let Some(filter) = file_filter
            && !filter.is_empty()
            && !filter.iter().any(|f| f == &entry.rel_path)
        {
            continue;
        }
        for (key, value) in entry
            .properties
            .iter()
            .filter(|(k, _)| k.as_str() != "tags")
        {
            let prop_type = frontmatter::infer_type(value).to_owned();
            *agg.entry((key.clone(), prop_type)).or_insert(0) += 1;
        }
    }

    let mut result: Vec<PropertySummaryEntry> = agg
        .into_iter()
        .map(|((name, prop_type), count)| PropertySummaryEntry {
            name,
            prop_type,
            count,
        })
        .collect();
    result.sort_by(|a, b| a.name.cmp(&b.name).then(a.prop_type.cmp(&b.prop_type)));

    let total = result.len() as u64;
    if let Some(n) = limit.filter(|n| *n > 0) {
        result.truncate(n);
    }
    let _ = format;
    Ok(CommandOutcome::success_with_total(
        serde_json::to_string_pretty(&result).context("failed to serialize")?,
        total,
    ))
}

/// Result of a `properties rename` operation.
#[derive(Debug, Serialize)]
pub struct RenamePropertyResult {
    pub from: String,
    pub to: String,
    pub dry_run: bool,
    pub modified: Vec<String>,
    pub skipped_count: usize,
    pub conflicts: Vec<String>,
    pub total: usize,
    pub scanned: usize,
}

/// Rename a property key across all matched files.
///
/// - Preserves value and type (moves the `Value` in the IndexMap)
/// - Skips files where the source key is absent (reported in `skipped`)
/// - Skips files where the target key already exists (reported in `conflicts`)
#[allow(clippy::too_many_arguments)]
pub fn properties_rename(
    dir: &Path,
    from: &str,
    to: &str,
    globs: &[String],
    dry_run: bool,
    format: Format,
    snapshot_index: &mut Option<SnapshotIndex>,
    index_path: Option<&Path>,
) -> Result<CommandOutcome> {
    if from == to {
        let out = crate::output::format_error(
            format,
            "source and target property names are identical",
            None,
            None,
            None,
        );
        return Ok(CommandOutcome::UserError(out));
    }

    let files = collect_files(dir, &[], globs, format)?;
    let files = match files {
        FilesOrOutcome::Files(f) => f,
        FilesOrOutcome::Outcome(o) => return Ok(o),
    };
    let scanned = files.len();

    let mut modified = Vec::new();
    let mut skipped_count: usize = 0;
    let mut conflicts = Vec::new();
    let mut index_dirty = false;

    for (full_path, rel_path) in &files {
        let mut props = match frontmatter::read_frontmatter(full_path) {
            Ok(p) => p,
            Err(e) if frontmatter::is_parse_error(&e) => {
                crate::warn::warn(format!("skipping {rel_path}: {e}"));
                continue;
            }
            Err(e) => return Err(e),
        };

        // Source key not present -- skip
        let Some(value) = props.shift_remove(from) else {
            skipped_count += 1;
            continue;
        };

        // Target key already exists -- conflict, put the source back
        if props.contains_key(to) {
            props.insert(from.to_owned(), value);
            conflicts.push(rel_path.clone());
            continue;
        }

        props.insert(to.to_owned(), value);
        if !dry_run {
            frontmatter::write_frontmatter(full_path, &props)?;
            if let Some(idx) = snapshot_index.as_mut()
                && let Some(entry) = idx.get_mut(rel_path)
            {
                let new_tags = extract_tags(&props);
                entry.properties = props;
                entry.tags = new_tags;
                entry.modified = format_modified(full_path)?;
                index_dirty = true;
            }
        }
        modified.push(rel_path.clone());
    }

    if !dry_run
        && index_dirty
        && let (Some(idx), Some(idx_path)) = (snapshot_index.as_mut(), index_path)
    {
        idx.save_to(idx_path)?;
    }

    let total = modified.len() + skipped_count + conflicts.len();
    let result = RenamePropertyResult {
        from: from.to_owned(),
        to: to.to_owned(),
        dry_run,
        modified,
        skipped_count,
        conflicts,
        total,
        scanned,
    };

    let _ = format;
    Ok(CommandOutcome::success(
        serde_json::to_string_pretty(&result).context("failed to serialize")?,
    ))
}

#[cfg(test)]
mod tests {
    use super::*;
    use hyalo_core::index::{ScanOptions, ScannedIndex};
    use std::fs;

    macro_rules! md {
        ($s:expr) => {
            $s.strip_prefix('\n').unwrap_or($s)
        };
    }

    /// Build a `ScannedIndex` from `dir` and call `properties_summary`.
    /// Mirrors the old disk-scan helper signature used in pre-Phase-5 tests.
    fn run_properties_summary(
        dir: &std::path::Path,
        file: Option<&str>,
        format: Format,
    ) -> anyhow::Result<CommandOutcome> {
        let all = hyalo_core::discovery::discover_files(dir)?;
        let file_pairs: Vec<(std::path::PathBuf, String)> = all
            .into_iter()
            .map(|p| {
                let rel = hyalo_core::discovery::relative_path(dir, &p);
                (p, rel)
            })
            .collect();
        let build = ScannedIndex::build(
            &file_pairs,
            None,
            &ScanOptions {
                scan_body: false,
                bm25_tokenize: false,
                default_language: None,
                frontmatter_link_props: None,
            },
        )?;
        let file_filter: Option<Vec<String>> = file.map(|f| vec![f.to_owned()]);
        properties_summary(&build.index, file_filter.as_deref(), format, None)
    }

    fn setup_dir() -> tempfile::TempDir {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(
            tmp.path().join("note.md"),
            md!(r"
---
title: Test
status: draft
priority: 3
tags:
  - rust
  - cli
---
# Hello
"),
        )
        .unwrap();
        fs::write(tmp.path().join("empty.md"), "No frontmatter here.\n").unwrap();
        tmp
    }

    /// Extract the output string from a `CommandOutcome`.
    fn unwrap_output(outcome: CommandOutcome) -> (String, bool) {
        match outcome {
            CommandOutcome::Success { output: s, .. } | CommandOutcome::RawOutput(s) => (s, true),
            CommandOutcome::UserError(s) => (s, false),
        }
    }

    #[test]
    fn properties_summary_aggregates() {
        let tmp = setup_dir();
        let (out, ok) =
            unwrap_output(run_properties_summary(tmp.path(), None, Format::Json).unwrap());
        assert!(ok);
        let parsed: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
        assert!(!parsed.is_empty());
        let names: Vec<&str> = parsed.iter().map(|v| v["name"].as_str().unwrap()).collect();
        assert!(names.contains(&"title"));
        assert!(names.contains(&"status"));
    }

    #[test]
    fn properties_rename_basic() {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(
            tmp.path().join("note.md"),
            md!(r"
---
title: Note
keywords: test
---
"),
        )
        .unwrap();

        let outcome = properties_rename(
            tmp.path(),
            "keywords",
            "Keywords",
            &[],
            false,
            Format::Json,
            &mut None,
            None,
        )
        .unwrap();
        let CommandOutcome::Success { output: out, .. } = outcome else {
            panic!("expected success")
        };
        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert_eq!(parsed["from"], "keywords");
        assert_eq!(parsed["to"], "Keywords");
        assert_eq!(parsed["modified"].as_array().unwrap().len(), 1);

        let content = fs::read_to_string(tmp.path().join("note.md")).unwrap();
        assert!(content.contains("Keywords:"));
        assert!(!content.contains("keywords:"));
    }

    #[test]
    fn properties_rename_skips_missing() {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(
            tmp.path().join("note.md"),
            md!(r"
---
title: Note
---
"),
        )
        .unwrap();

        let outcome = properties_rename(
            tmp.path(),
            "keywords",
            "Keywords",
            &[],
            false,
            Format::Json,
            &mut None,
            None,
        )
        .unwrap();
        let CommandOutcome::Success { output: out, .. } = outcome else {
            panic!("expected success")
        };
        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert_eq!(parsed["skipped_count"].as_u64().unwrap(), 1);
        assert_eq!(parsed["modified"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn properties_rename_conflict() {
        let tmp = tempfile::tempdir().unwrap();
        fs::write(
            tmp.path().join("note.md"),
            md!(r"
---
title: Note
keywords: test
Keywords: other
---
"),
        )
        .unwrap();

        let outcome = properties_rename(
            tmp.path(),
            "keywords",
            "Keywords",
            &[],
            false,
            Format::Json,
            &mut None,
            None,
        )
        .unwrap();
        let CommandOutcome::Success { output: out, .. } = outcome else {
            panic!("expected success")
        };
        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
        assert_eq!(parsed["conflicts"].as_array().unwrap().len(), 1);
        assert_eq!(parsed["modified"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn properties_rename_same_name_error() {
        let tmp = tempfile::tempdir().unwrap();
        let outcome = properties_rename(
            tmp.path(),
            "foo",
            "foo",
            &[],
            false,
            Format::Json,
            &mut None,
            None,
        )
        .unwrap();
        assert!(matches!(outcome, CommandOutcome::UserError(_)));
    }

    #[test]
    fn properties_summary_distinguishes_types() {
        // Same property name with different types should produce separate entries
        // (consistent with summary command's (name, type) keying)
        let tmp = tempfile::tempdir().unwrap();
        fs::write(tmp.path().join("text.md"), "---\npriority: high\n---\n").unwrap();
        fs::write(tmp.path().join("number.md"), "---\npriority: 3\n---\n").unwrap();

        let (out, ok) =
            unwrap_output(run_properties_summary(tmp.path(), None, Format::Json).unwrap());
        assert!(ok);
        let parsed: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
        let priority_entries: Vec<&serde_json::Value> =
            parsed.iter().filter(|p| p["name"] == "priority").collect();
        // Two entries: one text, one number -- not collapsed into a single entry
        assert_eq!(
            priority_entries.len(),
            2,
            "expected 2 entries for 'priority', got: {priority_entries:?}"
        );
        assert_eq!(priority_entries[0]["count"], 1);
        assert_eq!(priority_entries[1]["count"], 1);
    }

    #[test]
    fn properties_summary_skips_malformed_yaml() {
        let tmp = tempfile::tempdir().unwrap();
        // Valid file with a known property.
        fs::write(
            tmp.path().join("good.md"),
            md!(r"
---
title: Good Note
---
# Hello
"),
        )
        .unwrap();
        // Malformed YAML: a bare colon key is rejected by serde_saphyr.
        fs::write(
            tmp.path().join("bad.md"),
            "---\n: invalid yaml [[[{\n---\n# Bad\n",
        )
        .unwrap();

        let outcome = run_properties_summary(tmp.path(), None, Format::Json).unwrap();
        let (out, ok) = unwrap_output(outcome);
        assert!(ok, "expected Success, got UserError: {out}");

        let parsed: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
        let names: Vec<&str> = parsed.iter().map(|v| v["name"].as_str().unwrap()).collect();
        // The valid file's property must appear.
        assert!(names.contains(&"title"), "missing 'title' in {names:?}");
    }

    /// A file whose entire content is a bare `---` (no closing delimiter) must be
    /// skipped by `properties_summary`, matching the behaviour of `summary`.
    ///
    /// Before the fix, `read_frontmatter` returned `Ok(empty)` for this edge case
    /// while `scan_file_multi` correctly returned an "unclosed frontmatter" error,
    /// causing the two commands to diverge on total file counts.
    #[test]
    fn properties_summary_skips_bare_opening_delimiter() {
        let tmp = tempfile::tempdir().unwrap();
        // File with a valid title — must be counted.
        fs::write(tmp.path().join("good.md"), "---\ntitle: Present\n---\n").unwrap();
        // File that is just `---` — no closing delimiter, no content.
        // scan_file_multi treats this as an unclosed-frontmatter parse error;
        // properties_summary must do the same (not silently count it as empty).
        fs::write(tmp.path().join("bare.md"), "---\n").unwrap();

        let outcome = run_properties_summary(tmp.path(), None, Format::Json).unwrap();
        let (out, ok) = unwrap_output(outcome);
        assert!(ok, "expected Success: {out}");

        let parsed: Vec<serde_json::Value> = serde_json::from_str(&out).unwrap();
        let title_entry = parsed.iter().find(|p| p["name"] == "title").unwrap();
        // Only the good file contributes — count must be 1.
        assert_eq!(
            title_entry["count"], 1,
            "bare `---` file must not inflate the count: {parsed:?}"
        );
    }
}