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
//! This module defines the identities of stores and the qualified ids that
//! refer to them (SOKF_FORMAT_10, SOKF_FORMAT_64). The identity of a store is
//! `<scope>/<name>`. To refer to another store, a document puts that store's
//! identity in front of the id it would use inside that store. Within one
//! scope, the name of the store is enough.
use std::fmt;
use super::{Document, Knowledge, Located, Model};
use crate::RepoPath;
use crate::markdown::is_slug;
/// `Identity` is the identity of a store, `<scope>/<name>` (SOKF_FORMAT_10).
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Identity {
/// `scope` identifies the owner of the store.
pub scope: String,
/// `name` is the name of the store within its scope.
pub name: String,
}
impl Identity {
/// `parse` reads `<scope>/<name>`, and returns `None` unless both parts
/// are slugs.
pub fn parse(text: &str) -> Option<Self> {
let (scope, name) = text.split_once('/')?;
Identity::new(scope, name)
}
/// `new` returns the identity `scope/name`, or `None` unless both parts
/// are slugs.
fn new(scope: &str, name: &str) -> Option<Self> {
(is_slug(scope) && is_slug(name)).then(|| Identity {
scope: scope.to_string(),
name: name.to_string(),
})
}
}
impl fmt::Display for Identity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.scope, self.name)
}
}
impl Knowledge {
/// `identity` returns the identity of the store, when its manifest
/// declares a `scope` and a `name` that are both slugs.
pub fn identity(&self) -> Option<Identity> {
let m = self.manifest.as_ref()?;
Identity::new(m.scope.as_deref()?, m.name.as_deref()?)
}
/// `former` returns the former identities of the store, which its
/// manifest lists in `renamed-from`. It skips an entry that is not a
/// valid identity.
pub fn former(&self) -> Vec<Identity> {
self.manifest
.iter()
.flat_map(|m| &m.renamed_from)
.filter_map(|(old, _)| Identity::parse(old))
.collect()
}
}
/// `StoreHit` is the store that a qualified id refers to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StoreHit {
/// `knowledge` is the index of the knowledge store.
pub knowledge: usize,
/// `former` is true when the id uses a former identity of the store
/// (`renamed-from`).
pub former: bool,
}
/// `IdentityRow` holds the current and former identities of one store as
/// text, in the form that the cache keeps (CACHE-1_AC-16).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct IdentityRow {
/// `knowledge` is the index of the knowledge store.
pub knowledge: usize,
/// `identity` is the identity, `scope/name`, or an empty string when the
/// manifest declares none.
pub identity: String,
/// `former` holds the former identities, one `scope/name` on each line.
pub former: String,
}
impl Model {
/// `identity_rows` returns the current and former identities of every
/// store, in the form that the cache compares (CACHE-1_AC-16).
pub fn identity_rows(&self) -> Vec<IdentityRow> {
self.knowledges
.iter()
.enumerate()
.map(|(k, kn)| IdentityRow {
knowledge: k,
identity: kn.identity().map(|i| i.to_string()).unwrap_or_default(),
former: kn
.former()
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n"),
})
.collect()
}
/// `store` finds the store that `written` refers to from a document of
/// store `citing` (SOKF_FORMAT_64). It looks up a `name` among the
/// stores in the scope of the citing store, and a `scope/name` among
/// every store. It matches the current identities first, then the
/// former ones. When two stores share an identity, `store` returns
/// `None`, because validation reports that clash on their manifests.
///
/// `store` records a single dependency on the identities of all stores
/// (CACHE-1_AC-16), not one for each store it reads. A finer dependency
/// would save little, for two reasons. Renaming a store changes every
/// qualified id that refers to the store, and few documents need `store`.
// sokf-implements: SOKF_FORMAT_64
// sokf-implements: CACHE-1_AC-16
// sokf-implements: CONF-2_AC-3
pub fn store(&self, citing: usize, written: &str) -> Option<StoreHit> {
self.note(super::Query::Identities);
let wanted = match written.split_once('/') {
Some(_) => Identity::parse(written)?,
None => Identity {
scope: self.knowledges.get(citing)?.identity()?.scope,
name: written.to_string(),
},
};
let (hits, former) = self.matching(|i| *i == wanted);
match hits.as_slice() {
[knowledge] => Some(StoreHit {
knowledge: *knowledge,
former,
}),
_ => None,
}
}
/// `matching` returns the stores whose current identity `wanted`
/// accepts. When there are none, it returns the stores with a former
/// identity that `wanted` accepts, and says so with `true`.
fn matching(&self, wanted: impl Fn(&Identity) -> bool) -> (Vec<usize>, bool) {
let n = self.knowledges.len();
let current: Vec<usize> = (0..n)
.filter(|&k| self.knowledges[k].identity().is_some_and(|i| wanted(&i)))
.collect();
if !current.is_empty() {
return (current, false);
}
let former = (0..n)
.filter(|&k| self.knowledges[k].former().iter().any(&wanted))
.collect();
(former, true)
}
/// `stores_named` returns the stores that `written` names on the command
/// line, where no citing store gives a scope. It matches a `scope/name`
/// against the whole identity and a `name` against the name of every
/// store. It matches the current identities first, then the former ones.
// sokf-implements: CLI-7_AC-64
pub fn stores_named(&self, written: &str) -> Vec<usize> {
let matches = |identity: &Identity| match written.split_once('/') {
Some(_) => identity.to_string() == written,
None => identity.name == written,
};
self.matching(matches).0
}
/// `store_name` returns the shortest name of store `k` on the command
/// line: its `name`, or its whole identity when another store shares
/// that name. It returns `None` when the store has no identity.
pub fn store_name(&self, k: usize) -> Option<String> {
let identity = self.knowledges.get(k)?.identity()?;
let shared = self
.knowledges
.iter()
.filter(|o| o.identity().is_some_and(|i| i.name == identity.name))
.count()
> 1;
Some(if shared {
identity.to_string()
} else {
identity.name
})
}
/// `document_in` finds the document that `id` refers to in `store`.
/// `store` is a store prefix as a document of store `citing` wrote it. It
/// returns the document with the index of the knowledge store that
/// holds it. When `store` is `None`, it looks in `citing`.
pub fn document_in(
&self,
citing: usize,
store: Option<&str>,
id: &str,
) -> Option<(usize, &Document)> {
let k = match store {
None => citing,
Some(s) => self.store(citing, s)?.knowledge,
};
self.document(k, id).map(|d| (k, d))
}
/// `id_at` returns the id of the document or schema at `path`, in any
/// store, with the index of its knowledge store.
pub fn id_at(&self, path: &RepoPath) -> Option<(usize, String)> {
match self.by_path(path)? {
Located::Document(k, d) => Some((k, d.id()?.to_string())),
Located::Schema(k, s) => Some((k, s.id())),
_ => None,
}
}
/// `path_label` returns the canonical `sokf:` label that a document in
/// store `citing` should use for a link to `path` with `fragment`
/// (SOKF_FORMAT_65). The label depends on the file at `path`:
///
/// - For a document, the label is its id with the fragment.
/// - For a sidecar, the label is its id with the fragment, and with an
/// empty fragment when the link has none, because the path refers to
/// the sidecar file and not to the asset.
/// - For an asset, the label is the id of its sidecar, which refers to
/// the asset.
///
/// `path_label` returns `None` for any other file, and for an asset
/// path with a fragment.
pub fn path_label(
&self,
citing: usize,
path: &RepoPath,
fragment: Option<&str>,
) -> Option<String> {
if let Some((k, id)) = self.id_at(path) {
let fragment = match self.asset_of(path) {
Some(_) => Some(fragment.unwrap_or("")),
None => fragment,
};
return self.canonical_label(citing, k, Some(&id), fragment);
}
let (k, asset) = self.asset_at(path)?;
if fragment.is_some() {
return None;
}
self.note(super::Query::Path(asset.sidecar_path()));
let id = self.knowledges[k].documents[asset.sidecar?].id()?;
self.canonical_label(citing, k, Some(id), None)
}
/// `prefix` returns the store prefix that a document in store `citing`
/// should put before an id in store `target`. It is the shortest
/// unambiguous prefix (SOKF_FORMAT_65). The prefix is empty within one store,
/// `name:` within one scope, and `scope/name:` otherwise. `prefix`
/// returns `None` when the two stores differ and either has no
/// identity.
pub fn prefix(&self, citing: usize, target: usize) -> Option<String> {
if citing == target {
return Some(String::new());
}
self.note(super::Query::Identities);
let from = self.knowledges.get(citing)?.identity()?;
let to = self.knowledges.get(target)?.identity()?;
Some(if from.scope == to.scope {
format!("{}:", to.name)
} else {
format!("{to}:")
})
}
/// `canonical_label` returns the canonical `sokf:` label that a document
/// in store `citing` should use for `id` and `fragment` in store `target`
/// (SOKF_FORMAT_65).
pub fn canonical_label(
&self,
citing: usize,
target: usize,
id: Option<&str>,
fragment: Option<&str>,
) -> Option<String> {
self.canonical_include(citing, target, id.unwrap_or(""), fragment)
.map(|target| format!("sokf:{target}"))
}
/// `canonical_include` returns the canonical target that an include
/// marker in store `citing` should use for `id` and `fragment` in store
/// `target` (SOKF_FORMAT_65).
pub fn canonical_include(
&self,
citing: usize,
target: usize,
id: &str,
fragment: Option<&str>,
) -> Option<String> {
let prefix = self.prefix(citing, target)?;
Some(format!(
"{prefix}{id}{}",
fragment.map(|f| format!("#{f}")).unwrap_or_default()
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::markdown::split_store;
#[test]
fn an_identity_is_two_slugs() {
assert_eq!(
Identity::parse("ncoderz/sokf-spec").map(|i| i.to_string()),
Some("ncoderz/sokf-spec".into())
);
assert_eq!(Identity::parse("sokf-spec"), None);
assert_eq!(Identity::parse("Ncoderz/x"), None);
assert_eq!(Identity::parse("a/b/c"), None);
}
#[test]
fn a_qualified_id_splits_at_its_store() {
assert_eq!(split_store("glossary"), (None, "glossary"));
assert_eq!(
split_store("sokf-spec:glossary"),
(Some("sokf-spec"), "glossary")
);
assert_eq!(split_store("acme/common:"), (Some("acme/common"), ""));
}
}