grepdown-lib 0.1.0

Core library for grepdown: SQLite-backed Markdown indexing, FTS5 search, and link-graph traversal
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
use crate::error::Result;
use rusqlite::Connection;
use serde::Serialize;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub enum LintId {
    StaleRef,
    Orphan,
    BrokenLink,
    BrokenAnchor,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum Severity {
    Error,
    Warning,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum LintData {
    StaleRef {
        pinned_version: i64,
        current_version: i64,
    },
    Orphan,
    BrokenLink {
        raw_target: String,
    },
    BrokenAnchor {
        anchor: String,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Diagnostic {
    pub lint_id: LintId,
    pub severity: Severity,
    pub from_path: String,
    pub to_path: String,
    pub data: LintData,
}

impl LintId {
    pub fn title(self) -> &'static str {
        match self {
            LintId::StaleRef => "STALE REFERENCES DETECTED",
            LintId::Orphan => "ORPHAN DOCUMENTS DETECTED",
            LintId::BrokenLink => "BROKEN LINKS DETECTED",
            LintId::BrokenAnchor => "BROKEN ANCHORS DETECTED",
        }
    }

    pub fn suggestions(self) -> &'static str {
        match self {
            LintId::StaleRef => {
                "💡 Suggested actions:\n    1. Update them if needed\n    2. Run `grepdown approve-edits <filenames>` to mark them as reviewed"
            }
            LintId::Orphan => {
                "💡 These documents have no links. Consider:\n   \
                 1. Adding links to related documents\n   \
                 2. Linking from other documents to these\n   \
                 3. Deleting if they're no longer needed"
            }
            LintId::BrokenLink => {
                "💡 These links point to documents that don't exist. Consider:\n   \
                 1. Creating the missing documents\n   \
                 2. Fixing the link targets\n   \
                 3. Removing the broken links"
            }
            LintId::BrokenAnchor => {
                "💡 These anchors don't exist in the target document. Consider:\n   \
                 1. Adding the missing heading to the target document\n   \
                 2. Fixing the anchor to match an existing heading\n   \
                 3. Removing the anchor from the link"
            }
        }
    }

    pub fn format_group(self, diags: &[&Diagnostic]) -> String {
        match self {
            LintId::StaleRef => format_stale_ref(diags),
            LintId::Orphan => format_orphan(diags),
            LintId::BrokenLink => format_broken_link(diags),
            LintId::BrokenAnchor => format_broken_anchor(diags),
        }
    }
}

fn format_stale_ref(diags: &[&Diagnostic]) -> String {
    let mut out = String::new();
    out.push_str("The following files were updated, but their dependents may need review:\n\n");

    let mut by_updated: std::collections::HashMap<&str, Vec<&&Diagnostic>> =
        std::collections::HashMap::new();
    for d in diags {
        by_updated.entry(d.to_path.as_str()).or_default().push(d);
    }

    for (updated_file, deps) in &by_updated {
        let current_version = match &deps[0].data {
            LintData::StaleRef {
                current_version, ..
            } => *current_version,
            _ => unreachable!(),
        };
        out.push_str(&format!(
            "📄 {} (version {})\n",
            updated_file, current_version
        ));
        out.push_str("   └─ Referenced by:\n");
        for dep in deps {
            let pinned_version = match &dep.data {
                LintData::StaleRef { pinned_version, .. } => *pinned_version,
                _ => unreachable!(),
            };
            out.push_str(&format!(
                "      • {} (pinned at version {})\n",
                dep.from_path, pinned_version
            ));
        }
        out.push('\n');
    }

    out
}

fn format_orphan(diags: &[&Diagnostic]) -> String {
    let mut out = String::new();
    for d in diags {
        out.push_str(&format!("  - {}\n", d.from_path));
    }
    out
}

fn format_broken_link(diags: &[&Diagnostic]) -> String {
    let mut out = String::new();

    let mut by_source: std::collections::HashMap<&str, Vec<&&Diagnostic>> =
        std::collections::HashMap::new();
    for d in diags {
        by_source.entry(d.from_path.as_str()).or_default().push(d);
    }

    for (source, deps) in &by_source {
        out.push_str(&format!("📄 {}\n", source));
        out.push_str("   └─ Broken links:\n");
        for dep in deps {
            match &dep.data {
                LintData::BrokenLink { raw_target } => {
                    out.push_str(&format!("      • {} → {}\n", dep.from_path, raw_target));
                }
                _ => unreachable!(),
            }
        }
        out.push('\n');
    }

    out
}

fn format_broken_anchor(diags: &[&Diagnostic]) -> String {
    let mut out = String::new();

    let mut by_source: std::collections::HashMap<&str, Vec<&&Diagnostic>> =
        std::collections::HashMap::new();
    for d in diags {
        by_source.entry(d.from_path.as_str()).or_default().push(d);
    }

    for (source, deps) in &by_source {
        out.push_str(&format!("📄 {}\n", source));
        out.push_str("   └─ Broken anchors:\n");
        for dep in deps {
            match &dep.data {
                LintData::BrokenAnchor { anchor } => {
                    out.push_str(&format!("      • {} → #{}\n", dep.from_path, anchor));
                }
                _ => unreachable!(),
            }
        }
        out.push('\n');
    }

    out
}

fn check_stale_ref(conn: &Connection) -> Result<Vec<Diagnostic>> {
    let mut stmt = conn.prepare(
        "SELECT l.from_id, l.to_id, l.pinned_version, d.version
         FROM links l
         JOIN documents d ON l.to_id = d.path
         WHERE l.pinned_version < d.version",
    )?;

    stmt.query_map([], |row| {
        Ok(Diagnostic {
            lint_id: LintId::StaleRef,
            severity: Severity::Warning,
            from_path: row.get(0)?,
            to_path: row.get(1)?,
            data: LintData::StaleRef {
                pinned_version: row.get(2)?,
                current_version: row.get(3)?,
            },
        })
    })?
    .map(|r| r.map_err(Into::into))
    .collect()
}

fn check_orphan(conn: &Connection) -> Result<Vec<Diagnostic>> {
    let mut stmt = conn.prepare(
        "SELECT d.path
         FROM documents d
         WHERE NOT EXISTS (SELECT 1 FROM links l WHERE l.from_id = d.path)
           AND NOT EXISTS (SELECT 1 FROM links l WHERE l.to_id = d.path)",
    )?;

    stmt.query_map([], |row| {
        let path: String = row.get(0)?;
        Ok(Diagnostic {
            lint_id: LintId::Orphan,
            severity: Severity::Warning,
            from_path: path.clone(),
            to_path: path,
            data: LintData::Orphan,
        })
    })?
    .map(|r| r.map_err(Into::into))
    .collect()
}

fn check_broken_link(conn: &Connection) -> Result<Vec<Diagnostic>> {
    let mut stmt = conn.prepare("SELECT from_id, raw_target FROM broken_links")?;

    stmt.query_map([], |row| {
        let raw_target: String = row.get(1)?;
        Ok(Diagnostic {
            lint_id: LintId::BrokenLink,
            severity: Severity::Error,
            from_path: row.get(0)?,
            to_path: raw_target.clone(),
            data: LintData::BrokenLink { raw_target },
        })
    })?
    .map(|r| r.map_err(Into::into))
    .collect()
}

fn check_broken_anchor(conn: &Connection) -> Result<Vec<Diagnostic>> {
    let mut stmt = conn.prepare(
        "SELECT l.from_id, l.to_id, l.anchor
         FROM links l
         WHERE l.anchor IS NOT NULL
           AND NOT EXISTS (
               SELECT 1 FROM headings h
               WHERE h.path = l.to_id AND h.anchor = l.anchor
           )",
    )?;

    stmt.query_map([], |row| {
        let anchor: String = row.get(2)?;
        Ok(Diagnostic {
            lint_id: LintId::BrokenAnchor,
            severity: Severity::Error,
            from_path: row.get(0)?,
            to_path: row.get(1)?,
            data: LintData::BrokenAnchor { anchor },
        })
    })?
    .map(|r| r.map_err(Into::into))
    .collect()
}

pub fn run_lints(conn: &Connection) -> Result<Vec<Diagnostic>> {
    let mut all = Vec::new();
    all.extend(check_stale_ref(conn)?);
    all.extend(check_orphan(conn)?);
    all.extend(check_broken_link(conn)?);
    all.extend(check_broken_anchor(conn)?);
    Ok(all)
}

pub fn approve_edits(conn: &Connection, paths: &[String]) -> Result<usize> {
    let rows = if paths.is_empty() {
        conn.execute(
            "WITH stale AS (
                SELECT l.rowid as link_rowid, d.version as current_version
                FROM links l
                JOIN documents d ON l.to_id = d.path
                WHERE l.pinned_version < d.version
            )
            UPDATE links SET pinned_version = (SELECT current_version FROM stale WHERE stale.link_rowid = links.rowid)
            WHERE rowid IN (SELECT link_rowid FROM stale)",
            []
        )?
    } else {
        let placeholders: Vec<String> = paths
            .iter()
            .enumerate()
            .map(|(i, _)| format!("?{}", i + 1))
            .collect();
        let sql = format!(
            "WITH stale AS (
                SELECT l.rowid as link_rowid, d.version as current_version
                FROM links l
                JOIN documents d ON l.to_id = d.path
                WHERE l.pinned_version < d.version
                AND l.to_id IN ({})
            )
            UPDATE links SET pinned_version = (SELECT current_version FROM stale WHERE stale.link_rowid = links.rowid)
            WHERE rowid IN (SELECT link_rowid FROM stale)",
            placeholders.join(", ")
        );
        let params: Vec<&dyn rusqlite::ToSql> =
            paths.iter().map(|p| p as &dyn rusqlite::ToSql).collect();
        conn.execute(&sql, params.as_slice())?
    };
    Ok(rows)
}

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

    fn setup_test_db() -> Connection {
        let conn = Connection::open_in_memory().unwrap();
        bootstrap(&conn).unwrap();
        conn
    }

    fn insert_test_document(conn: &Connection, path: &str, version: i64) {
        conn.execute(
            "INSERT INTO documents (path, mtime, content_hash, version) VALUES (?1, 0, X'00', ?2)",
            rusqlite::params![path, version],
        )
        .unwrap();
    }

    fn insert_test_link(conn: &Connection, from_path: &str, to_path: &str, pinned_version: i64) {
        conn.execute(
            "INSERT INTO links (from_id, to_id, pinned_version) VALUES (?1, ?2, ?3)",
            rusqlite::params![from_path, to_path, pinned_version],
        )
        .unwrap();
    }

    fn insert_broken_link(conn: &Connection, from_path: &str, raw_target: &str) {
        conn.execute(
            "INSERT INTO broken_links (from_id, raw_target) VALUES (?1, ?2)",
            rusqlite::params![from_path, raw_target],
        )
        .unwrap();
    }

    fn diags_for(diags: &[Diagnostic], id: LintId) -> Vec<&Diagnostic> {
        diags.iter().filter(|d| d.lint_id == id).collect()
    }

    #[test]
    fn test_stale_ref_detection() {
        let conn = setup_test_db();
        insert_test_document(&conn, "/a.md", 1);
        insert_test_document(&conn, "/b.md", 2);
        insert_test_link(&conn, "/a.md", "/b.md", 1);

        let diags = check_stale_ref(&conn).unwrap();
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].from_path, "/a.md");
        assert_eq!(diags[0].to_path, "/b.md");
        match &diags[0].data {
            LintData::StaleRef {
                pinned_version,
                current_version,
            } => {
                assert_eq!(*pinned_version, 1);
                assert_eq!(*current_version, 2);
            }
            _ => unreachable!(),
        }
    }

    #[test]
    fn test_no_stale_refs_when_up_to_date() {
        let conn = setup_test_db();
        insert_test_document(&conn, "/a.md", 1);
        insert_test_document(&conn, "/b.md", 2);
        insert_test_link(&conn, "/a.md", "/b.md", 2);

        let diags = check_stale_ref(&conn).unwrap();
        assert_eq!(diags.len(), 0);
    }

    #[test]
    fn test_approve_edits_all() {
        let conn = setup_test_db();
        insert_test_document(&conn, "/a.md", 1);
        insert_test_document(&conn, "/b.md", 2);
        insert_test_link(&conn, "/a.md", "/b.md", 1);

        let rows = approve_edits(&conn, &[]).unwrap();
        assert_eq!(rows, 1);

        let pinned: i64 = conn
            .query_row(
                "SELECT pinned_version FROM links WHERE from_id = '/a.md'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(pinned, 2);
    }

    #[test]
    fn test_approve_edits_specific_path() {
        let conn = setup_test_db();
        insert_test_document(&conn, "/a.md", 1);
        insert_test_document(&conn, "/b.md", 2);
        insert_test_document(&conn, "/c.md", 3);
        insert_test_link(&conn, "/a.md", "/b.md", 1);
        insert_test_link(&conn, "/a.md", "/c.md", 1);

        let paths = vec!["/b.md".to_string()];
        let rows = approve_edits(&conn, &paths).unwrap();
        assert_eq!(rows, 1);

        let pinned_b: i64 = conn
            .query_row(
                "SELECT pinned_version FROM links WHERE to_id = '/b.md'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(pinned_b, 2);

        let pinned_c: i64 = conn
            .query_row(
                "SELECT pinned_version FROM links WHERE to_id = '/c.md'",
                [],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(pinned_c, 1);
    }

    #[test]
    fn orphan_detection() {
        let conn = setup_test_db();
        insert_test_document(&conn, "orphan.md", 1);
        insert_test_document(&conn, "another-orphan.md", 1);

        let diags = run_lints(&conn).unwrap();
        let orphans = diags_for(&diags, LintId::Orphan);
        assert_eq!(orphans.len(), 2);
        let mut paths: Vec<&str> = orphans.iter().map(|d| d.from_path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["another-orphan.md", "orphan.md"]);
        match &orphans[0].data {
            LintData::Orphan => {}
            _ => panic!("expected Orphan data"),
        }
    }

    #[test]
    fn non_orphan_with_outgoing_link() {
        let conn = setup_test_db();
        insert_test_document(&conn, "doc-a.md", 1);
        insert_test_document(&conn, "doc-b.md", 1);
        insert_test_link(&conn, "doc-a.md", "doc-b.md", 1);

        let diags = run_lints(&conn).unwrap();
        assert!(diags_for(&diags, LintId::Orphan).is_empty());
    }

    #[test]
    fn non_orphan_with_incoming_link() {
        let conn = setup_test_db();
        insert_test_document(&conn, "source.md", 1);
        insert_test_document(&conn, "target.md", 1);
        insert_test_link(&conn, "source.md", "target.md", 1);

        let diags = run_lints(&conn).unwrap();
        assert!(diags_for(&diags, LintId::Orphan).is_empty());
    }

    #[test]
    fn broken_link_detection() {
        let conn = setup_test_db();
        insert_test_document(&conn, "doc-a.md", 1);
        insert_broken_link(&conn, "doc-a.md", "nonexistent.md");

        let diags = check_broken_link(&conn).unwrap();
        assert_eq!(diags.len(), 1);
        assert_eq!(diags[0].from_path, "doc-a.md");
        assert_eq!(diags[0].severity, Severity::Error);
        match &diags[0].data {
            LintData::BrokenLink { raw_target } => {
                assert_eq!(raw_target, "nonexistent.md");
            }
            _ => panic!("expected BrokenLink data"),
        }
    }
}