sokf-core 0.8.0

sokf-core holds the logic of the sokf command-line tool: it loads and validates Superdev Open Knowledge Format (SOKF) knowledge stores, and runs each sokf command.
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
//! This module indexes the captured keys by scope, and resolves `sokf:`
//! labels and their fragments as the format specification defines.

use std::collections::{BTreeMap, BTreeSet, HashMap};

use super::{Model, Query};
use crate::{
    RepoPath,
    grammar::KeyScope,
    markdown::{Body, link_headings, slug},
};

/// A `KeyEntry` is one captured key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct KeyEntry {
    /// The key.
    pub key: String,
    /// The document that captures it.
    pub path: RepoPath,
    /// The line that captures the key.
    pub line: usize,
    /// The slug of the heading, when a heading captures the key.
    pub heading_slug: Option<String>,
    /// The scope that the schema declares for the key.
    pub scope: KeyScope,
    /// The knowledge of the document that captures the key.
    pub knowledge: usize,
    /// The `rule-id` of the key. It is the nearest `rule-id` around the rule
    /// that captures the key. `for-each-key-of` and `cited-by` refer
    /// to this `rule-id`.
    pub rule_id: Option<String>,
}

/// A `KeyIndex` holds the keys by the scope in which each key must be
/// unique (the grammar's `unique`). It stores each key under its slug. The
/// slug is the id of the key's anchor, and a fragment refers to a key by
/// that slug (`SOKF_FORMAT_72`).
#[derive(Debug, Clone, Default)]
pub struct KeyIndex {
    /// Every key of every document, by path and by slug.
    pub document: HashMap<(RepoPath, String), Vec<KeyEntry>>,
    /// The keys of knowledge scope, by knowledge and by slug.
    pub knowledge: HashMap<(usize, String), Vec<KeyEntry>>,
    /// The keys of repository scope, by slug.
    pub repository: HashMap<String, Vec<KeyEntry>>,
    /// The slugs of the anchors that the engine keeps for the units that
    /// break their rules, by path. Such a unit captures no key, but a
    /// fragment can name its anchor (`SOKF_FORMAT_71`).
    pub anchors: BTreeMap<RepoPath, BTreeSet<String>>,
}

impl KeyIndex {
    /// `add` records a captured key under its document and its scope.
    pub fn add(&mut self, entry: KeyEntry) {
        let s = slug(&entry.key);
        match &entry.scope {
            KeyScope::Host(scope) if scope == "knowledge" => self
                .knowledge
                .entry((entry.knowledge, s.clone()))
                .or_default()
                .push(entry.clone()),
            KeyScope::Host(scope) if scope == "repository" => self
                .repository
                .entry(s.clone())
                .or_default()
                .push(entry.clone()),
            _ => {}
        }
        self.document
            .entry((entry.path.clone(), s))
            .or_default()
            .push(entry);
    }

    /// `add_anchor` records an anchor that the engine keeps in `path` for a
    /// unit that breaks its rule.
    pub fn add_anchor(&mut self, path: RepoPath, slug: String) {
        self.anchors.entry(path).or_default().insert(slug);
    }

    /// `anchors_of` returns the slugs of the anchors that the engine keeps
    /// in `path`.
    pub fn anchors_of(&self, path: &RepoPath) -> impl Iterator<Item = &str> {
        self.anchors
            .get(path)
            .into_iter()
            .flatten()
            .map(String::as_str)
    }

    /// `merge` adds the keys and the kept anchors of `other`.
    pub fn merge(&mut self, other: KeyIndex) {
        self.extend(other.document.into_values().flatten());
        for (path, slugs) in other.anchors {
            self.anchors.entry(path).or_default().extend(slugs);
        }
    }

    /// `of_document` returns the keys that `path` captures.
    pub fn of_document<'a>(&'a self, path: &'a RepoPath) -> impl Iterator<Item = &'a KeyEntry> {
        self.document
            .iter()
            .filter(move |((p, _), _)| p == path)
            .flat_map(|(_, entries)| entries.iter())
    }
}

impl Extend<KeyEntry> for KeyIndex {
    fn extend<I: IntoIterator<Item = KeyEntry>>(&mut self, entries: I) {
        for entry in entries {
            self.add(entry);
        }
    }
}

/// A `Label` holds the three parts of a `sokf:` label: the store
/// (optional), the id (absent when the label is a bare fragment), and the
/// fragment.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Label<'a> {
    /// The store, written as `name` or `scope/name`.
    pub store: Option<&'a str>,
    /// The document id.
    pub id: Option<&'a str>,
    /// The key or heading slug.
    pub fragment: Option<&'a str>,
}

impl<'a> Label<'a> {
    /// `with_fragment` returns the label with `fragment` in place of its own.
    #[must_use]
    pub fn with_fragment(self, fragment: Option<&'a str>) -> Self {
        Label { fragment, ..self }
    }

    /// `without_store` returns the label with no store, so that it resolves
    /// in the store that the caller chooses.
    #[must_use]
    pub fn without_store(self) -> Self {
        Label {
            store: None,
            ..self
        }
    }
}

/// A `Resolved` is the target of a `sokf:` label.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolved {
    /// The knowledge of the target.
    pub knowledge: usize,
    /// The target document.
    pub path: RepoPath,
    /// The fragment that the definition block appends to the path as
    /// `#slug`. It is the slug of the heading or key that the label refers to.
    pub slug: Option<String>,
}

/// An `Unresolved` states the reason that a `sokf:` label is a broken edge
/// (`SOKF_FORMAT_62`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Unresolved {
    /// The store matches no knowledge root, current or former. The variant
    /// holds the store as written in the label.
    NoStore(String),
    /// The id matches no document in the knowledge.
    NoDocument,
    /// The id matches more than one document.
    DuplicateId,
    /// The fragment matches no key and no heading.
    NoFragment,
    /// A bare fragment matches no key at repository or knowledge scope.
    BareNotFound,
    /// A bare fragment matches keys in more than one document.
    BareAmbiguous,
    /// A bare fragment matches only a key of document scope.
    BareDocumentScope,
    /// The fragment is the text of a key or a heading, not its slug. The
    /// variant holds the slug.
    KeyForm(String),
}

impl Unresolved {
    /// `message` returns the error message for `label`.
    pub fn message(&self, label: &str) -> String {
        match self {
            Unresolved::NoStore(store) => format!(
                "`[{label}]` refers to the store `{store}`, which is not a knowledge root of this repository"
            ),
            Unresolved::NoDocument => format!("`[{label}]` resolves to no document"),
            Unresolved::DuplicateId => {
                format!("more than one document has the id in `[{label}]`")
            }
            Unresolved::NoFragment => {
                format!(
                    "`[{label}]` refers to a fragment that is neither a captured key nor a heading"
                )
            }
            Unresolved::BareNotFound => format!(
                "`[{label}]` refers to a key that no document of its store captures at repository or knowledge scope"
            ),
            Unresolved::BareAmbiguous => {
                format!("`[{label}]` refers to a key that more than one document captures")
            }
            Unresolved::BareDocumentScope => {
                format!("`[{label}]` refers to a key of document scope only")
            }
            Unresolved::KeyForm(slug) => format!(
                "`[{label}]` gives the fragment as text, not as a slug. Use `#{slug}`, which `--fix` writes for you"
            ),
        }
    }
}

/// `resolve_in` resolves a fragment against the headings, the keys, and the
/// kept anchors of one document, by slug (`SOKF_FORMAT_71`). The headings
/// include each heading inside a quote or a list item, and each heading has
/// the slug that the host gives it, so no two headings share one
/// (`SOKF_FORMAT_155`). A kept anchor is one that the engine keeps for a
/// unit that breaks its rule. It returns the fragment itself, which the
/// definition block appends. When the fragment matches only by its text,
/// `resolve_in` returns `Unresolved::KeyForm` with the slug.
// sokf-implements: SOKF_FORMAT_71
// sokf-implements: SOKF_FORMAT_72
// sokf-implements: SOKF_FORMAT_73
pub fn resolve_in(
    keys: &KeyIndex,
    path: &RepoPath,
    body: &Body,
    fragment: &str,
) -> Result<String, Unresolved> {
    let headings = link_headings(body);
    let found = |f: &str| {
        keys.document.contains_key(&(path.clone(), f.to_string()))
            || keys.anchors.get(path).is_some_and(|a| a.contains(f))
            || headings.iter().any(|h| h.slug == f)
    };
    if found(fragment) {
        return Ok(fragment.to_string());
    }
    let s = slug(fragment);
    if s != fragment && found(&s) {
        Err(Unresolved::KeyForm(s))
    } else {
        Err(Unresolved::NoFragment)
    }
}

/// `resolve` resolves a `sokf:` label from a document of `knowledge`: first
/// the store, then the id in that store, then the fragment. A bare fragment
/// with no store refers to a key in the store of the document that holds
/// the label. If no key there matches, but a key of repository scope in
/// another store does, the label resolves to that key, because that key is
/// unique. The canonical form of the label then includes that other store
/// (`SOKF_FORMAT_65`).
// sokf-implements: SOKF_FORMAT_61
// sokf-implements: SOKF_FORMAT_63
// sokf-implements: SOKF_FORMAT_64
// sokf-implements: SOKF_FORMAT_66
// sokf-implements: SOKF_FORMAT_70
// sokf-implements: FORMAT-2_AC-1
// sokf-implements: FORMAT-2_AC-3
pub fn resolve(
    model: &Model,
    keys: &KeyIndex,
    knowledge: usize,
    label: Label<'_>,
) -> Result<Resolved, Unresolved> {
    let target = match label.store {
        None => knowledge,
        Some(store) => {
            model
                .store(knowledge, store)
                .ok_or_else(|| Unresolved::NoStore(store.to_string()))?
                .knowledge
        }
    };
    if model.knowledges.get(target).is_none() {
        return Err(Unresolved::NoDocument);
    }
    match (label.id.filter(|i| !i.is_empty()), label.fragment) {
        // `sokf:#`, with no id and an empty fragment, refers to nothing (SOKF_FORMAT_66).
        (None, None | Some("")) => Err(Unresolved::NoDocument),
        (Some(id), fragment) => {
            let doc = match model.ids(target, id).len() {
                0 => return Err(Unresolved::NoDocument),
                1 => model.document(target, id).ok_or(Unresolved::NoDocument)?,
                _ => return Err(Unresolved::DuplicateId),
            };
            // An empty fragment refers to the whole document (FORMAT-2_AC-3).
            let Some(fragment) = fragment.filter(|f| !f.is_empty()) else {
                return Ok(Resolved {
                    knowledge: target,
                    path: doc.path.clone(),
                    slug: None,
                });
            };
            resolve_in(keys, &doc.path, &doc.body, fragment).map(|slug| Resolved {
                knowledge: target,
                path: doc.path.clone(),
                slug: Some(slug),
            })
        }
        (None, Some(fragment)) => {
            let elsewhere = label.store.is_none();
            match bare(model, keys, target, fragment, elsewhere) {
                Err(Unresolved::BareNotFound) => {
                    let s = slug(fragment);
                    if s != fragment && bare(model, keys, target, &s, elsewhere).is_ok() {
                        Err(Unresolved::KeyForm(s))
                    } else {
                        Err(Unresolved::BareNotFound)
                    }
                }
                other => other,
            }
        }
    }
}

/// `resolve_slugged` resolves a label as `resolve` does. When the label
/// gives its fragment as text, not as a slug, `resolve_slugged` resolves
/// the label again with the fragment's slug, and returns that slug too.
pub fn resolve_slugged(
    model: &Model,
    keys: &KeyIndex,
    knowledge: usize,
    label: Label<'_>,
) -> (Result<Resolved, Unresolved>, Option<String>) {
    match resolve(model, keys, knowledge, label) {
        Err(Unresolved::KeyForm(s)) => {
            let resolved = resolve(model, keys, knowledge, label.with_fragment(Some(&s)));
            (resolved, Some(s))
        }
        other => (other, None),
    }
}

/// `bare` resolves a bare fragment, by its slug, to a key of `knowledge`
/// whose scope is the knowledge or the repository. When no such key exists
/// and `elsewhere` is set, it
/// resolves the fragment to a key of repository scope in another store.
fn bare(
    model: &Model,
    keys: &KeyIndex,
    knowledge: usize,
    fragment: &str,
    elsewhere: bool,
) -> Result<Resolved, Unresolved> {
    let found = |one: &KeyEntry| Resolved {
        knowledge: one.knowledge,
        path: one.path.clone(),
        slug: Some(fragment.to_string()),
    };
    model.note(Query::KeyRepository(fragment.to_string()));
    model.note(Query::KeyKnowledge {
        knowledge,
        slug: fragment.to_string(),
    });
    let repo = keys
        .repository
        .get(fragment)
        .map(Vec::as_slice)
        .unwrap_or_default();
    let own: Vec<&KeyEntry> = repo.iter().filter(|e| e.knowledge == knowledge).collect();
    match own.as_slice() {
        [one] => return Ok(found(one)),
        [] => {}
        _ => return Err(Unresolved::BareAmbiguous),
    }
    let local = keys
        .knowledge
        .get(&(knowledge, fragment.to_string()))
        .map(Vec::as_slice)
        .unwrap_or_default();
    match local {
        [one] => Ok(found(one)),
        [] => {
            if elsewhere {
                match repo {
                    [one] => return Ok(found(one)),
                    [] => {}
                    _ => return Err(Unresolved::BareAmbiguous),
                }
            }
            model.note(Query::KeyDocument {
                knowledge,
                slug: fragment.to_string(),
            });
            let document_only = keys
                .document
                .keys()
                .any(|(p, k)| k == fragment && model.knowledge_of(p) == Some(knowledge));
            if document_only {
                Err(Unresolved::BareDocumentScope)
            } else {
                Err(Unresolved::BareNotFound)
            }
        }
        _ => Err(Unresolved::BareAmbiguous),
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    fn p(s: &str) -> RepoPath {
        RepoPath::parse(s).unwrap()
    }

    fn entry(key: &str, path: &str, scope: KeyScope, slug: Option<&str>) -> KeyEntry {
        KeyEntry {
            key: key.into(),
            path: p(path),
            line: 1,
            heading_slug: slug.map(str::to_string),
            scope,
            knowledge: 0,
            rule_id: None,
        }
    }

    #[test]
    fn indexes_by_scope() {
        let mut index = KeyIndex::default();
        index.add(entry("D", "/a.md", KeyScope::Document, None));
        index.add(entry(
            "K",
            "/a.md",
            KeyScope::Host("knowledge".into()),
            Some("k-heading"),
        ));
        index.add(entry(
            "R",
            "/b.md",
            KeyScope::Host("repository".into()),
            None,
        ));
        assert_eq!(index.document.len(), 3);
        assert_eq!(index.knowledge.len(), 1);
        assert_eq!(index.repository.len(), 1);
        assert_eq!(index.of_document(&p("/a.md")).count(), 2);
        assert_eq!(index.of_document(&p("/c.md")).count(), 0);
        for u in [
            Unresolved::NoStore("s".into()),
            Unresolved::NoDocument,
            Unresolved::DuplicateId,
            Unresolved::NoFragment,
            Unresolved::BareNotFound,
            Unresolved::BareAmbiguous,
            Unresolved::BareDocumentScope,
            Unresolved::KeyForm("k".into()),
        ] {
            assert!(u.message("sokf:x").contains("[sokf:x]"));
        }
    }

    // sokf-tests: SOKF_FORMAT_71
    #[test]
    fn a_kept_anchor_resolves_a_fragment_of_its_document_and_is_no_key() {
        let mut index = KeyIndex::default();
        let mut kept = KeyIndex::default();
        kept.add_anchor(p("/a.md"), "d-5".into());
        index.merge(kept);
        assert_eq!(index.anchors_of(&p("/a.md")).collect::<Vec<_>>(), ["d-5"]);
        assert_eq!(index.of_document(&p("/a.md")).count(), 0);
        let body = Body::default();
        assert_eq!(
            resolve_in(&index, &p("/a.md"), &body, "d-5"),
            Ok("d-5".into())
        );
        assert_eq!(
            resolve_in(&index, &p("/a.md"), &body, "D-5"),
            Err(Unresolved::KeyForm("d-5".into()))
        );
        assert_eq!(
            resolve_in(&index, &p("/b.md"), &body, "d-5"),
            Err(Unresolved::NoFragment)
        );
    }
}

#[cfg(test)]
mod fragment_tests {
    use std::collections::BTreeMap;

    use super::*;
    use crate::{Config, FsLoader, RepoRoot, collect, config::MarkerCheck};

    // sokf-tests: FORMAT-2_AC-3
    #[test]
    fn an_empty_fragment_names_the_whole_document_and_a_bare_one_nothing() {
        let dir = crate::test_support::fresh_dir("keys-empty");
        std::fs::create_dir_all(dir.join("knowledge")).unwrap();
        std::fs::write(
            dir.join("knowledge/a.md"),
            "---\ntype: Note\nid: a\n---\n\n# A\n\n## Part\n",
        )
        .unwrap();
        let root = RepoRoot::new(dir.clone());
        let tree = collect(
            &root,
            &Config {
                roots: vec![RepoPath::parse("/knowledge").unwrap()],
                registered_count: 0,
                exclude: vec![],
                added_exclude: vec![],
                declined: BTreeMap::default(),
                markers: MarkerCheck::default(),
                format: crate::config::FormatConfig::default(),
            },
        )
        .unwrap();
        let model = crate::model::build(tree, &mut FsLoader::new(&root)).unwrap();
        let keys = KeyIndex::default();
        let label = |id, fragment| Label {
            store: None,
            id,
            fragment,
        };
        let path = RepoPath::parse("/knowledge/a.md").unwrap();
        assert_eq!(
            resolve(&model, &keys, 0, label(Some("a"), Some(""))),
            Ok(Resolved {
                knowledge: 0,
                path: path.clone(),
                slug: None
            })
        );
        assert_eq!(
            resolve(&model, &keys, 0, label(Some("a"), Some("part"))),
            Ok(Resolved {
                knowledge: 0,
                path,
                slug: Some("part".into())
            })
        );
        assert_eq!(
            resolve(&model, &keys, 0, label(None, Some(""))),
            Err(Unresolved::NoDocument)
        );
        std::fs::remove_dir_all(&dir).unwrap();
    }
}