link-cli 0.2.12

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
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
//! The links interface (`ILinks` in C#, [`doublets::Links`] in Rust) as LiNo
//! documents, so a store behind a [`LinksServer`](super::LinksServer) is used
//! exactly like a local one through [`RemoteLinks`](super::RemoteLinks).
//!
//! Every request is one top-level link named after the operation and holding
//! exactly one value. The substitution query language ignores that shape (a
//! query needs a restriction *and* a substitution), so an operation never
//! collides with a query sent to the same server.
//!
//! | Request                          | Reply                                  |
//! |----------------------------------|----------------------------------------|
//! | `(count: (index source target))` | `(count: N)`                           |
//! | `(each: (index source target))`  | one `(index: source target)` per match |
//! | `(create: (source target))`      | `() ((index: source target))`          |
//! | `(update: (index source target))`| `((index: s t)) ((index: source target))` per changed link |
//! | `(delete: index)`                | `((index: source target)) ()` per removed link |
//! | `(get-name: link)`               | `(name: 'text')`, or nothing           |
//! | `(set-name: (link 'text'))`      | `(link: N)`                            |
//! | `(get-by-name: 'text')`          | `(link: N)`, or nothing                |
//! | `(remove-name: link)`            | nothing                                |
//!
//! A restriction holds up to three parts, matched like the raw links
//! interface matches a query: none matches every link, `(index)` one link,
//! `(index value)` links of that index whose source or target is `value`,
//! and `(index source target)` matches part by part. `*` matches anything.
//! Every reference in a reply is a number; failures are `(error: 'message')`.
//!
//! The changes of a `create`, `update` or `delete` are the net change of every
//! link it touched, the way `clink --changes` reports a query: a cascading
//! delete is one `((index: source target)) ()` per removed link, without the
//! intermediate steps by which the store behind the server got there, which
//! differ between the C# and the Rust stores.

use super::error::{ProtocolError, ProtocolResult};
use super::format::{format_document, format_link};
use super::mapping::LinoDocument;
use crate::changes_simplifier::simplify_changes;
use crate::link::Link;
use crate::named_type_links::NamedTypeLinks;
use links_notation::LiNo;

/// The wire spelling of "any value" in a restriction.
pub const ANY: &str = "*";

/// One part of a restriction: `None` matches any value.
pub type Part = Option<u32>;

/// One call of the links interface.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LinksOperation {
    /// Number of links matching the restriction.
    Count(Vec<Part>),
    /// Every link matching the restriction, ordered by index.
    Each(Vec<Part>),
    /// Creates a link.
    Create { source: u32, target: u32 },
    /// Points an existing link at a new source and target; an update that
    /// would duplicate an existing link merges into it instead.
    Update {
        index: u32,
        source: u32,
        target: u32,
    },
    /// Deletes a link, and every link that still refers to it.
    Delete(u32),
    /// The name of a link.
    GetName(u32),
    /// Names a link.
    SetName(u32, String),
    /// The link with a name.
    GetByName(String),
    /// Removes the name of a link.
    RemoveName(u32),
}

/// A `(before, after)` change; a missing side is [`Link::null`].
pub type Change = (Link, Link);

impl LinksOperation {
    /// The request document for this operation.
    pub fn to_document(&self) -> LinoDocument {
        let (name, argument) = match self {
            Self::Count(restriction) => ("count", restriction_lino(restriction)),
            Self::Each(restriction) => ("each", restriction_lino(restriction)),
            Self::Create { source, target } => ("create", numbers_lino(&[*source, *target])),
            Self::Update {
                index,
                source,
                target,
            } => ("update", numbers_lino(&[*index, *source, *target])),
            Self::Delete(index) => ("delete", number(*index)),
            Self::GetName(index) => ("get-name", number(*index)),
            Self::SetName(index, name) => (
                "set-name",
                group(vec![number(*index), LiNo::Ref(name.clone())]),
            ),
            Self::GetByName(name) => ("get-by-name", LiNo::Ref(name.clone())),
            Self::RemoveName(index) => ("remove-name", number(*index)),
        };
        vec![named(name, vec![argument])]
    }

    /// Recognises an operation request; `Ok(None)` for any other document,
    /// such as a substitution query.
    pub fn from_document(document: &[LiNo<String>]) -> ProtocolResult<Option<Self>> {
        let [LiNo::Link {
            id: Some(name),
            values,
        }] = document
        else {
            return Ok(None);
        };
        let [argument] = values.as_slice() else {
            return Ok(None);
        };
        let operation = match name.as_str() {
            "count" => Self::Count(parse_restriction(argument)?),
            "each" => Self::Each(parse_restriction(argument)?),
            "create" => {
                let [source, target] = parse_numbers::<2>(argument)?;
                Self::Create { source, target }
            }
            "update" => {
                let [index, source, target] = parse_numbers::<3>(argument)?;
                Self::Update {
                    index,
                    source,
                    target,
                }
            }
            "delete" => Self::Delete(parse_number(argument)?),
            "get-name" => Self::GetName(parse_number(argument)?),
            "set-name" => match parts(argument) {
                [index, LiNo::Ref(name)] => Self::SetName(parse_number(index)?, name.clone()),
                _ => return Err(malformed_argument("set-name", argument)),
            },
            "get-by-name" => match argument {
                LiNo::Ref(name) => Self::GetByName(name.clone()),
                _ => return Err(malformed_argument("get-by-name", argument)),
            },
            "remove-name" => Self::RemoveName(parse_number(argument)?),
            _ => return Ok(None),
        };
        Ok(Some(operation))
    }

    /// Runs the operation against `storage` and returns the reply document.
    pub fn execute<S: NamedTypeLinks + ?Sized>(
        &self,
        storage: &mut S,
    ) -> anyhow::Result<LinoDocument> {
        Ok(match self {
            Self::Count(restriction) => {
                let count = matching(storage, restriction).len();
                vec![named("count", vec![LiNo::Ref(count.to_string())])]
            }
            Self::Each(restriction) => matching(storage, restriction)
                .iter()
                .map(link_lino)
                .collect(),
            Self::Create { source, target } => {
                // `create` makes exactly `(index: source target)`.
                let created = Link::new(storage.create(*source, *target), *source, *target);
                storage.save()?;
                changes_document(&[(Link::null(), created)])
            }
            Self::Update {
                index,
                source,
                target,
            } => {
                let mut changes = Vec::new();
                storage.update_observed(*index, *source, *target, &mut |before, after| {
                    changes.push((before, after))
                })?;
                storage.save()?;
                changes_document(&simplify_changes(changes))
            }
            Self::Delete(index) => {
                let mut changes = Vec::new();
                storage
                    .delete_observed(*index, &mut |before, after| changes.push((before, after)))?;
                storage.save()?;
                changes_document(&simplify_changes(changes))
            }
            Self::GetName(index) => match storage.get_name(*index)? {
                Some(name) => vec![named("name", vec![LiNo::Ref(name)])],
                None => Vec::new(),
            },
            Self::SetName(index, name) => {
                let link = storage.set_name(*index, name)?;
                storage.save()?;
                link_reply(Some(link))
            }
            Self::GetByName(name) => link_reply(storage.get_by_name(name)?),
            Self::RemoveName(index) => {
                storage.remove_name(*index)?;
                storage.save()?;
                Vec::new()
            }
        })
    }
}

/// Whether `link` matches `restriction`, with the shapes of the raw links
/// interface: see the [module documentation](self).
pub fn matches(link: &Link, restriction: &[Part]) -> bool {
    let is = |part: Part, value: u32| part.is_none_or(|part| part == value);
    match *restriction {
        [] => true,
        [index] => is(index, link.index),
        [index, value] => {
            is(index, link.index) && (is(value, link.source) || is(value, link.target))
        }
        [index, source, target] => {
            is(index, link.index) && is(source, link.source) && is(target, link.target)
        }
        _ => false,
    }
}

fn matching<S: NamedTypeLinks + ?Sized>(storage: &mut S, restriction: &[Part]) -> Vec<Link> {
    let mut links = match restriction.first() {
        Some(Some(index)) => storage.get_link(*index).into_iter().collect(),
        _ => storage.all_links(),
    };
    links.retain(|link| matches(link, restriction));
    links.sort_by_key(|link| link.index);
    links
}

/// The `count` of a `(count: N)` reply.
pub fn parse_count(document: &[LiNo<String>]) -> ProtocolResult<u32> {
    match document {
        [LiNo::Link {
            id: Some(id),
            values,
        }] if id == "count" => match values.as_slice() {
            [count] => parse_number(count),
            _ => Err(malformed_reply("count", document)),
        },
        _ => Err(malformed_reply("count", document)),
    }
}

/// The links of an `each` reply.
pub fn parse_links(document: &[LiNo<String>]) -> ProtocolResult<Vec<Link>> {
    document.iter().map(parse_link).collect()
}

/// The `(before) (after)` pairs of a `create`, `update` or `delete` reply.
pub fn parse_changes(document: &[LiNo<String>]) -> ProtocolResult<Vec<Change>> {
    document
        .iter()
        .map(|change| match parts(change) {
            [before, after] => Ok((parse_change_side(before)?, parse_change_side(after)?)),
            _ => Err(malformed_reply("change", document)),
        })
        .collect()
}

/// The name of a `get-name` reply.
pub fn parse_name(document: &[LiNo<String>]) -> ProtocolResult<Option<String>> {
    match document {
        [] => Ok(None),
        [LiNo::Link {
            id: Some(id),
            values,
        }] if id == "name" => match values.as_slice() {
            [LiNo::Ref(name)] => Ok(Some(name.clone())),
            _ => Err(malformed_reply("name", document)),
        },
        _ => Err(malformed_reply("name", document)),
    }
}

/// The link of a `set-name` or `get-by-name` reply.
pub fn parse_link_reply(document: &[LiNo<String>]) -> ProtocolResult<Option<u32>> {
    match document {
        [] => Ok(None),
        [LiNo::Link {
            id: Some(id),
            values,
        }] if id == "link" => match values.as_slice() {
            [link] => parse_number(link).map(Some),
            _ => Err(malformed_reply("link", document)),
        },
        _ => Err(malformed_reply("link", document)),
    }
}

/// A reply listing `changes`, one `(before) (after)` line each.
pub fn changes_document(changes: &[Change]) -> LinoDocument {
    changes
        .iter()
        .map(|(before, after)| group(vec![change_side(before), change_side(after)]))
        .collect()
}

/// `(index: source target)` with plain numbers.
pub fn link_lino(link: &Link) -> LiNo<String> {
    named(
        &link.index.to_string(),
        vec![number(link.source), number(link.target)],
    )
}

fn link_reply(link: Option<u32>) -> LinoDocument {
    link.map(|link| vec![named("link", vec![number(link)])])
        .unwrap_or_default()
}

fn change_side(link: &Link) -> LiNo<String> {
    if link.is_null() {
        group(Vec::new())
    } else {
        group(vec![link_lino(link)])
    }
}

fn parse_change_side(side: &LiNo<String>) -> ProtocolResult<Link> {
    match side {
        LiNo::Link { id: None, values } if values.is_empty() => Ok(Link::null()),
        LiNo::Link { id: None, values } if values.len() == 1 => parse_link(&values[0]),
        // `((index: source target))` loses its wrapper when the side is the
        // single named link itself.
        link @ LiNo::Link { id: Some(_), .. } => parse_link(link),
        _ => Err(ProtocolError::malformed(format!(
            "expected a change side, found {}",
            format_link(side)
        ))),
    }
}

fn parse_link(link: &LiNo<String>) -> ProtocolResult<Link> {
    match link {
        LiNo::Link {
            id: Some(index),
            values,
        } => match values.as_slice() {
            [source, target] => Ok(Link::new(
                parse_text_number(index)?,
                parse_number(source)?,
                parse_number(target)?,
            )),
            _ => Err(ProtocolError::malformed(format!(
                "expected (index: source target), found {}",
                format_link(link)
            ))),
        },
        _ => Err(ProtocolError::malformed(format!(
            "expected (index: source target), found {}",
            format_link(link)
        ))),
    }
}

fn restriction_lino(restriction: &[Part]) -> LiNo<String> {
    group(
        restriction
            .iter()
            .map(|part| part.map_or_else(|| LiNo::Ref(ANY.to_string()), number))
            .collect(),
    )
}

fn parse_restriction(argument: &LiNo<String>) -> ProtocolResult<Vec<Part>> {
    let parts = parts(argument);
    if parts.len() > 3 {
        return Err(malformed_argument("restriction", argument));
    }
    parts
        .iter()
        .map(|part| match part {
            LiNo::Ref(text) if text == ANY => Ok(None),
            part => parse_number(part).map(Some),
        })
        .collect()
}

fn numbers_lino(numbers: &[u32]) -> LiNo<String> {
    group(numbers.iter().copied().map(number).collect())
}

fn parse_numbers<const N: usize>(argument: &LiNo<String>) -> ProtocolResult<[u32; N]> {
    let numbers = parts(argument)
        .iter()
        .map(parse_number)
        .collect::<ProtocolResult<Vec<_>>>()?;
    numbers
        .try_into()
        .map_err(|_| malformed_argument(&format!("{N} numbers"), argument))
}

/// The values of an unnamed group; a lone reference is a group of one, since
/// the canonical document model unwraps `(x)` to `x`.
fn parts(argument: &LiNo<String>) -> &[LiNo<String>] {
    match argument {
        LiNo::Link { id: None, values } => values,
        reference => std::slice::from_ref(reference),
    }
}

fn parse_number(value: &LiNo<String>) -> ProtocolResult<u32> {
    match value {
        LiNo::Ref(text) => parse_text_number(text),
        _ => Err(ProtocolError::malformed(format!(
            "expected a number, found {}",
            format_link(value)
        ))),
    }
}

fn parse_text_number(text: &str) -> ProtocolResult<u32> {
    text.parse()
        .map_err(|_| ProtocolError::malformed(format!("expected a number, found '{text}'")))
}

fn number(value: u32) -> LiNo<String> {
    LiNo::Ref(value.to_string())
}

fn named(id: &str, values: Vec<LiNo<String>>) -> LiNo<String> {
    LiNo::Link {
        id: Some(id.to_string()),
        values,
    }
}

fn group(values: Vec<LiNo<String>>) -> LiNo<String> {
    LiNo::Link { id: None, values }
}

fn malformed_argument(expected: &str, argument: &LiNo<String>) -> ProtocolError {
    ProtocolError::malformed(format!(
        "expected {expected}, found {}",
        format_link(argument)
    ))
}

fn malformed_reply(expected: &str, document: &[LiNo<String>]) -> ProtocolError {
    ProtocolError::malformed(format!(
        "expected a {expected} reply, found {}",
        format_document(document)
    ))
}