sokf-core 0.9.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. Unstable: its interface can change in any release.
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
//! This module reads the arguments that a user gives the graph commands,
//! `guide`, and `locate`: `resolve_start` reads the `START` of a graph command
//! (CLI-7), `resolve_document` reads the document that `guide` takes (CLI-17)
//! in the same way, and `resolve_id` reads the id that `locate` takes (CLI-8).

use std::path::Path;

use super::{Graph, NodeKey, NodeKind};
use crate::{
    Error, FileKind, Model, PathArg, RepoPath, Result,
    grammar::KeyScope,
    model::{KeyEntry, KeyIndex, split_store},
};

/// `Start` is what a `START` resolves to.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Start {
    /// A knowledge root, or a directory in one, whose layout the command
    /// prints.
    Container(RepoPath),
    /// A document, an additional file, or a repository file, for which the
    /// command prints one row.
    Node(NodeKey),
    /// A directory outside every knowledge store, for which the command
    /// prints the roots and the files under it.
    Outside(RepoPath),
}

impl Start {
    /// `key` returns the key of the start's graph node.
    pub fn key(&self) -> NodeKey {
        match self {
            Start::Container(p) | Start::Outside(p) => NodeKey::Path(p.clone()),
            Start::Node(k) => k.clone(),
        }
    }

    /// `path` returns the start as a path, for the `start` field of the
    /// JSON output, or `None` for a node that has no path.
    pub fn path(&self) -> Option<&RepoPath> {
        match self {
            Start::Container(p) | Start::Outside(p) => Some(p),
            Start::Node(k) => k.path(),
        }
    }
}

/// `is_key_reference` reports whether `arg` is a reference to a key alone,
/// `#key` or `<store>:#key`, which the caller can resolve only with the
/// index of captured keys.
pub fn is_key_reference(arg: &str) -> bool {
    let arg = arg.strip_prefix("sokf:").unwrap_or(arg);
    arg.split_once('#')
        .is_some_and(|(head, _)| split_store(head).1.is_empty())
}

/// `key_entry` returns the key that a reference identifies, with the key's
/// slug. A reference to a key alone identifies the key when exactly one
/// document captures it (CLI-7_AC-74), at repository or knowledge scope or,
/// failing those, at document scope (CLI-7_AC-98). A reference
/// `<id>#<key>` identifies the key when the document with that id captures
/// it (CLI-7_AC-97).
pub fn key_entry(model: &Model, keys: &KeyIndex, arg: &str) -> Option<(KeyEntry, String)> {
    let written = arg.split_once('#')?.1;
    if written.is_empty() {
        return None;
    }
    let (path, slug) = match paths_of_reference(model, Some(keys), arg).as_slice() {
        [one] => (one.clone(), None),
        [] => match document_scoped(model, keys, arg).as_slice() {
            [(one, slug)] => (one.clone(), Some(slug.clone())),
            _ => return None,
        },
        _ => return None,
    };
    // A key alone that names a key of repository or knowledge scope names
    // it by the first id that such a key has, as `paths_of_reference` found
    // the document, so a key of document scope under an earlier id of the
    // same document is not the one it names.
    let wide = slug.is_none() && is_key_reference(arg);
    slug.into_iter()
        .chain(std::iter::once(written.to_string()))
        .chain(crate::markdown::key_ids(written))
        .find_map(|slug| {
            let entry = keys
                .document
                .get(&(path.clone(), slug.clone()))?
                .iter()
                .find(|e| !wide || !matches!(e.scope, KeyScope::Document))?;
            Some((entry.clone(), slug))
        })
}

/// `document_scoped` returns each document that captures the key of `arg`,
/// with the key's slug, in the order of the documents' paths, when `arg` is
/// a reference to a key alone that matches no key of repository or knowledge
/// scope (CLI-7_AC-98 to CLI-7_AC-100). The documents lie in the stores
/// that the reference gives, or in every store when it gives none.
fn document_scoped(model: &Model, keys: &KeyIndex, arg: &str) -> Vec<(RepoPath, String)> {
    if !is_key_reference(arg) || !paths_of_reference(model, Some(keys), arg).is_empty() {
        return Vec::new();
    }
    let reference = arg.strip_prefix("sokf:").unwrap_or(arg);
    let Some((head, written)) = reference.split_once('#') else {
        return Vec::new();
    };
    let stores = stores_of(model, split_store(head).0);
    // The lookup tries the key as written and then each id that it can
    // name, and keeps the documents of the first that some document has.
    std::iter::once(written.to_string())
        .chain(crate::markdown::key_ids(written))
        .map(|slug| {
            let mut found: Vec<(RepoPath, String)> = keys
                .document
                .keys()
                .filter(|(p, s)| {
                    *s == slug && model.knowledge_of(p).is_some_and(|k| stores.contains(&k))
                })
                .cloned()
                .collect();
            found.sort();
            found
        })
        .find(|found| !found.is_empty())
        .unwrap_or_default()
}

/// `stores_of` returns the stores that a reference searches: each store of
/// the name it gives, or every store when it gives none.
fn stores_of(model: &Model, store: Option<&str>) -> Vec<usize> {
    match store {
        None => (0..model.knowledges.len()).collect(),
        Some(s) => model.stores_named(s),
    }
}

/// `note` returns the note that `graph` prints for `arg`, when `arg` is a
/// reference to a key alone that one document captures at document scope
/// only. The note gives the reference that names the key in a link
/// (CLI-7_AC-98).
pub(super) fn note(model: &Model, keys: Option<&KeyIndex>, arg: &str) -> Option<String> {
    let [(path, slug)] = document_scoped(model, keys?, arg).try_into().ok()?;
    let reference = key_reference(model, &path, &slug)?;
    let id = reference.split_once('#')?.0;
    Some(format!(
        "`{arg}` is unique only within `{id}`, so link it as `{reference}`"
    ))
}

/// `key_reference` returns the reference `<id>#<slug>` to the key `slug` of
/// the document at `path`. The id has its store before it when another
/// store has a document with the same id. `key_reference` returns `None`
/// when another document of the same store has the id, because no reference
/// then names the document.
fn key_reference(model: &Model, path: &RepoPath, slug: &str) -> Option<String> {
    let (k, id) = id_of(model, path)?;
    let mut elsewhere = false;
    for o in 0..model.knowledges.len() {
        if model.ids(o, &id).into_iter().any(|p| &p != path) {
            if o == k {
                return None;
            }
            elsewhere = true;
        }
    }
    Some(if elsewhere {
        format!("{}:{id}#{slug}", model.store_name(k)?)
    } else {
        format!("{id}#{slug}")
    })
}

/// `id_of` returns the store and the id of the document at `path`.
fn id_of(model: &Model, path: &RepoPath) -> Option<(usize, String)> {
    if let Some(found) = model.id_at(path) {
        Some(found)
    } else {
        // The run does not hold this document, so the id comes from the
        // store's row.
        let k = model.knowledge_of(path)?;
        let id = model.fallback.as_ref()?.fields(path)?.id?;
        Some((k, id))
    }
}

/// `paths_of_reference` returns the paths of every document and schema that
/// a reference identifies, when it is written outside every store, such as
/// on the command line. The reference
/// `[sokf:][<store>:]<id>[#<fragment>]` identifies the document with that
/// id, whatever its fragment. The reference `[sokf:][<store>:]#<key>`
/// identifies the document that captures the key, when only one document
/// captures it (CLI-7_AC-29 to CLI-7_AC-32). A reference with no store
/// searches every root.
fn paths_of_reference(model: &Model, keys: Option<&KeyIndex>, arg: &str) -> Vec<RepoPath> {
    // sokf-implements: CLI-7_AC-60
    let reference = arg.strip_prefix("sokf:").unwrap_or(arg);
    let (head, fragment) = match reference.split_once('#') {
        Some((h, f)) => (h, Some(f)),
        None => (reference, None),
    };
    let (store, id) = split_store(head);
    let stores = stores_of(model, store);
    let mut paths: Vec<RepoPath> =
        if id.is_empty() {
            let (Some(keys), Some(key)) = (keys, fragment.filter(|f| !f.is_empty())) else {
                return Vec::new();
            };
            // The index holds each key under its id, so the lookup tries the
            // key as written and then each id that it can name, and stops at
            // the first that some key has.
            std::iter::once(key.to_string())
                .chain(crate::markdown::key_ids(key))
                .map(|key| {
                    keys.repository
                        .get(&key)
                        .into_iter()
                        .flatten()
                        .chain(stores.iter().flat_map(|&k| {
                            keys.knowledge.get(&(k, key.clone())).into_iter().flatten()
                        }))
                        .filter(|e| stores.contains(&e.knowledge))
                        .map(|e| e.path.clone())
                        .collect::<Vec<_>>()
                })
                .find(|paths| !paths.is_empty())
                .unwrap_or_default()
        } else {
            // The lookup searches the run's documents and the store's rows
            // (CACHE-1_AC-21).
            stores.iter().flat_map(|&k| model.ids(k, id)).collect()
        };
    paths.sort();
    paths.dedup();
    paths
}

/// `qualified` returns the qualified form of `arg` for each document in
/// `paths`, as advice for an ambiguous reference. Each form starts with the
/// store's name, or with its whole identity when another root shares that
/// name. The document's id follows, or `#key` for a reference to a key
/// alone. `qualified` returns an empty string when it cannot qualify every
/// document.
fn qualified(model: &Model, arg: &str, paths: &[RepoPath]) -> String {
    let key = is_key_reference(arg)
        .then(|| arg.split_once('#').map(|(_, k)| k))
        .flatten();
    let forms: Vec<String> = paths
        .iter()
        .filter_map(|p| {
            let (k, id) = id_of(model, p)?;
            let store = model.store_name(k)?;
            Some(match key {
                Some(key) => format!("`{store}:#{key}`"),
                None => format!("`{store}:{id}`"),
            })
        })
        .collect();
    if forms.len() == paths.len() {
        format!(". Write {}", forms.join(" or "))
    } else {
        String::new()
    }
}

fn join(paths: &[RepoPath]) -> String {
    paths
        .iter()
        .map(RepoPath::as_str)
        .collect::<Vec<_>>()
        .join(", ")
}

/// `resolve_id` returns the one document that a reference identifies across
/// every root, or refuses a reference that identifies no document or more
/// than one (CLI-8_AC-9).
// sokf-implements: CLI-8_AC-9
// sokf-implements: CLI-8_AC-10
// sokf-implements: CLI-8_AC-11
// sokf-implements: CLI-8_AC-6
// sokf-implements: FORMAT-2_AC-1
pub fn resolve_id(model: &Model, keys: Option<&KeyIndex>, id: &str) -> Result<RepoPath> {
    match paths_of_reference(model, keys, id).as_slice() {
        [] => Err(Error::Locate(format!("`{id}` identifies no document"))),
        [one] => Ok(one.clone()),
        many => Err(Error::Locate(format!(
            "`{id}` identifies more than one document: {}{}",
            join(many),
            qualified(model, id, many)
        ))),
    }
}

/// `Reading` is what an argument names when the tool reads it both as an id
/// and as a path, and exactly one reading resolves.
enum Reading {
    /// The document or schema that the argument identifies as an id, or as a
    /// reference to a key that the document captures.
    Id(RepoPath),
    /// The file or directory that the argument names as a path.
    Path(RepoPath),
}

/// `read` reads `arg` both as an `id` and as a path, and refuses it unless
/// exactly one reading resolves (CLI-7_AC-19, CLI-7_AC-20). The message of
/// a refusal says why, for the command to name.
// sokf-implements: CLI-7_AC-29
// sokf-implements: CLI-7_AC-30
// sokf-implements: CLI-7_AC-31
// sokf-implements: CLI-7_AC-32
// sokf-implements: CLI-7_AC-33
// sokf-implements: CLI-1_AC-7
// sokf-implements: CLI-1_AC-8
// sokf-implements: CLI-7_AC-4
// sokf-implements: CLI-7_AC-19
// sokf-implements: CLI-7_AC-20
#[expect(
    clippy::case_sensitive_file_extension_comparisons,
    reason = "a START that ends in a lowercase `.md` is path-shaped, as CLI-7_AC-28 says, so the comparison is case-sensitive on purpose"
)]
fn read(
    model: &Model,
    keys: Option<&KeyIndex>,
    cwd: &Path,
    arg: &str,
) -> std::result::Result<Reading, String> {
    // The function reads a word shaped as a path only as a path, even when
    // a document has that word as its id.
    // sokf-implements: CLI-7_AC-28
    let path_shaped = arg.starts_with('.') || arg.ends_with('/') || arg.ends_with(".md");
    let ids = if path_shaped {
        Vec::new()
    } else {
        paths_of_reference(model, keys, arg)
    };
    let path = PathArg(arg).resolve(&model.tree.root, cwd);
    match (ids.as_slice(), path) {
        ([_, ..], Some(p)) => Err(format!(
            "`{arg}` is both an id ({}) and a path ({p})",
            join(&ids)
        )),
        ([_, _, ..], None) => Err(format!(
            "`{arg}` identifies more than one document: {}{}",
            join(&ids),
            qualified(model, arg, &ids)
        )),
        ([one], None) => Ok(Reading::Id(one.clone())),
        ([], Some(p)) => Ok(Reading::Path(p)),
        ([], None) => {
            // A key that no document captures at repository or knowledge
            // scope can still be a key of document scope.
            let scoped = keys.map_or_else(Vec::new, |k| document_scoped(model, k, arg));
            match scoped.as_slice() {
                // sokf-implements: CLI-7_AC-98
                [(one, _)] => Ok(Reading::Id(one.clone())),
                // sokf-implements: CLI-7_AC-100
                [] => Err(format!(
                    "`{arg}` is neither an id nor a path in the repository"
                )),
                // sokf-implements: CLI-7_AC-99
                many => {
                    let forms: Vec<String> = many
                        .iter()
                        .map(|(p, slug)| match key_reference(model, p, slug) {
                            Some(r) => format!("`{r}`"),
                            None => p.to_string(),
                        })
                        .collect();
                    Err(format!(
                        "`{arg}` is a key of document scope that more than one document captures. Write {}",
                        capped(&forms)
                    ))
                }
            }
        }
    }
}

/// `resolve_start` reads `arg` as `read` does. When the start is a directory
/// or a repository file, the function adds it to the graph as a node.
pub fn resolve_start(
    graph: &mut Graph,
    model: &Model,
    keys: Option<&KeyIndex>,
    cwd: &Path,
    arg: &str,
) -> Result<Start> {
    match read(model, keys, cwd, arg).map_err(Error::Start)? {
        // For a reference to a key alone, and for `<id>#<key>` when the
        // document captures the key, the start is the key's node when the
        // graph has one.
        // sokf-implements: CLI-7_AC-32
        // sokf-implements: CLI-7_AC-97
        Reading::Id(one) => Ok(key_start(graph, model, keys, arg, &one)),
        Reading::Path(p) => Ok(path_start(graph, model, p)),
    }
}

/// `resolve_document` reads `arg` as `resolve_start` reads a `START`, and
/// returns the document or schema that it names. It refuses an argument
/// that names no document of a knowledge store, such as a directory, an
/// asset, or an additional file.
// sokf-implements: CLI-17_AC-1
// sokf-implements: CLI-17_AC-2
// sokf-implements: CLI-17_AC-3
pub fn resolve_document(
    model: &Model,
    keys: Option<&KeyIndex>,
    cwd: &Path,
    arg: &str,
) -> Result<RepoPath> {
    match read(model, keys, cwd, arg).map_err(Error::Guide)? {
        Reading::Id(path) => Ok(path),
        Reading::Path(path) => match model.tree.file(&path).map(|f| f.kind) {
            Some(FileKind::Document | FileKind::Schema) => Ok(path),
            _ => Err(Error::Guide(format!(
                "`{arg}` names {path}, which is no document of a knowledge store"
            ))),
        },
    }
}

/// `LISTED` is the number of references that the error for a key of
/// document scope lists before it counts the rest (CLI-7_AC-99).
const LISTED: usize = 10;

/// `capped` joins the first `LISTED` of `forms` with "or", in their order,
/// and follows them with "and N more" when `forms` holds more.
// sokf-implements: CLI-7_AC-99
fn capped(forms: &[String]) -> String {
    let listed = forms[..forms.len().min(LISTED)].join(" or ");
    match forms.len().saturating_sub(LISTED) {
        0 => listed,
        more => format!("{listed}, and {more} more"),
    }
}

/// `key_start` returns the start of `arg` in the document at `path`: the
/// node of the key that `arg` names, when the graph has one, and the
/// document otherwise.
fn key_start(
    graph: &Graph,
    model: &Model,
    keys: Option<&KeyIndex>,
    arg: &str,
    path: &RepoPath,
) -> Start {
    Start::Node(
        keys.and_then(|k| key_entry(model, k, arg))
            .map(|(_, slug)| NodeKey::Key(format!("{path}#{slug}")))
            .filter(|k| graph.node(k).is_some())
            .unwrap_or_else(|| NodeKey::Path(path.clone())),
    )
}

/// `path_start` reads a path as a start. It tries each reading in order: a
/// root, a directory, a node that the graph has, and then a repository
/// file.
fn path_start(graph: &mut Graph, model: &Model, path: RepoPath) -> Start {
    if model.tree.roots.contains(&path) {
        return Start::Container(path);
    }
    if path.to_fs(&model.tree.root).is_dir() {
        graph.ensure(path.clone(), NodeKind::Directory);
        return if model.knowledge_of(&path).is_some() {
            Start::Container(path)
        } else {
            Start::Outside(path)
        };
    }
    let key = NodeKey::Path(path.clone());
    if graph.node(&key).is_none() {
        graph.ensure(path, NodeKind::File);
    }
    Start::Node(key)
}

#[cfg(test)]
mod tests;