tilo-actions 0.5.1

The Tilo action model: resources, the actions on them, and their registry, with the plain resource vocabulary that needs no store.
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! The directory model an address is read through: one resource as a caller
//! names it, and the resources and actions one listing holds under a parent.

use std::collections::{HashMap, HashSet};

use crate::ActionDescriptor;
use crate::api::ActionAddress;
use crate::remote::CatalogueRow;
use serde_json::{Value, json};

use super::names::{bound_key, path_name};

impl Node {
    /// This resource as a row of a listing under `parent`: the name its line
    /// prints, the actions it offers by their ids, and whether a read answers
    /// it as a resource of its own (`reads`), or it is only a place inside
    /// what it is listed under (an option, a choice): those are part of their
    /// parent's read and name no resource, so a walk that reads resources one
    /// by one reads only the rows that say so.
    pub fn listing_row(&self, parent: &str, name: &str, resource_type: &str, reads: bool) -> Value {
        json!({
            "path": format!("{parent}/{name}"),
            "name": name,
            "id": self.id,
            "key": self.key,
            "label": self.label,
            "kind": resource_type,
            "has_children": self.container,
            "reads": reads,
            "actions": self
                .listed_actions()
                .map(|action| action.address.action.clone())
                .collect::<Vec<_>>(),
        })
    }

    /// The actions a listing shows it offering: what acts on an item it
    /// holds is listed on that item, bound to it, and found here only by a
    /// call that names it.
    pub fn listed_actions(&self) -> impl Iterator<Item = &ActionDescriptor> {
        self.actions
            .iter()
            .filter(|action| !action.contract.needs_an_item())
    }
}

/// The names `rows` share, lowercased, each named among `siblings`
/// ([`Node::name`]): a line named by one of them is told apart by its id
/// beside it. Two titles used once each per parent are told apart by their
/// places instead, so they are not shared.
pub fn shared_names(rows: &[Node], siblings: &Names<'_>) -> std::collections::HashSet<String> {
    let mut seen = std::collections::HashSet::new();
    rows.iter()
        .map(|row| row.name_in(siblings).trim().to_lowercase())
        .filter(|name| !seen.insert(name.clone()))
        .collect()
}

/// Siblings indexed by their titles: what tells whether a name sets one of
/// them apart ([`Node::name_in`]) without reading every other sibling, so a
/// listing of hundreds names each of its rows at the cost of one.
pub struct Names<'a> {
    siblings: Vec<&'a Node>,
    /// Each sibling's index by its title, trimmed and lowercased, in order.
    titled: HashMap<String, Vec<usize>>,
}

impl<'a> Names<'a> {
    pub fn of(siblings: impl IntoIterator<Item = &'a Node>) -> Self {
        let siblings: Vec<&'a Node> = siblings.into_iter().collect();
        let mut titled: HashMap<String, Vec<usize>> = HashMap::new();
        for (at, node) in siblings.iter().enumerate() {
            titled
                .entry(node.label.trim().to_ascii_lowercase())
                .or_default()
                .push(at);
        }
        Self { siblings, titled }
    }

    /// The siblings titled `title`, trimmed, whatever its case.
    fn titled(&self, title: &str) -> impl Iterator<Item = &'a Node> + '_ {
        self.titled
            .get(&title.trim().to_ascii_lowercase())
            .into_iter()
            .flatten()
            .filter_map(|at| self.siblings.get(*at).copied())
    }

    /// The siblings `name` names first ([`Node::answers_to`] ranks them `0`):
    /// the one a title names, or the one whose place among those of its
    /// title `name` gives (`TODO 2`). Any other answers to it after them, if
    /// at all.
    fn first_named(&self, name: &str) -> Vec<&'a Node> {
        let mut named: Vec<&'a Node> = Vec::new();
        let mut by_place: Option<&str> = None;
        if let Some((base, place)) = name.rsplit_once(' ')
            && !base.trim().is_empty()
            && place.chars().all(|character| character.is_ascii_digit())
            && let Ok(place) = place.parse::<usize>()
        {
            by_place = Some(base.trim());
            named.extend(
                self.titled(base)
                    .filter(|node| place == node.ordinal && place >= 2),
            );
        }
        // A name that is the title itself names the first of that title; a
        // title its place names above answers only to that place.
        named.extend(self.titled(name).filter(|node| {
            node.ordinal <= 1
                && node.label.trim().eq_ignore_ascii_case(name)
                && by_place.is_none_or(|base| !node.label.trim().eq_ignore_ascii_case(base))
        }));
        named
    }
}

/// One resource of an app's directory, as an agent names it.
#[derive(Clone, Debug)]
pub struct Node {
    /// The broker's id: never shown, only sent back.
    pub id: String,
    pub resource_type: String,
    pub label: String,
    /// The app's own stable name for it: its semantic key, else the logical
    /// id inside a bound id, else the id.
    pub key: String,
    pub parent: String,
    /// Its place among the siblings that share its title, from 1: what tells
    /// two pages titled alike apart in a path (`TODO`, `TODO 2`).
    pub ordinal: usize,
    pub container: bool,
    pub actions: Vec<ActionDescriptor>,
}

impl Node {
    /// The part of the key that tells two resources of one type apart
    /// (`notes.page:3fa2…` → `3fa2…`).
    pub fn token(&self) -> &str {
        self.key
            .rsplit([':', '/'])
            .find(|part| !part.is_empty())
            .unwrap_or(&self.key)
    }

    /// Whether `other`'s key names this one's distinguishing part.
    pub fn owns(&self, other: &Node) -> bool {
        let token = self.token();
        token.len() >= 8 && other.key != self.key && other.key.contains(token)
    }

    /// Whether `other` is this resource's own and not one of its children.
    pub fn beside(&self, other: &Node) -> bool {
        self.owns(other) && !other.key.starts_with(&self.key)
    }

    /// Whether this resource is one of `other`'s: it sits under the other's id.
    pub fn within(&self, other: &Node) -> bool {
        matches!(self.id.strip_prefix(other.id.as_str()), Some(rest) if rest.starts_with('/'))
    }

    /// How well `name` names this node: 0 its label, 1 its key (whole or last
    /// part), 2 its resource type, 3 the start of either; `None` when it does not.
    pub fn answers_to(&self, name: &str) -> Option<u8> {
        let same = |text: &str| text.trim().eq_ignore_ascii_case(name);
        // A name that is a title and its place among the siblings of that
        // title (`TODO 2`) names the one whose place it is, and no other:
        // the title alone names the first.
        if let Some((base, place)) = name.rsplit_once(' ')
            && !base.trim().is_empty()
            && place.chars().all(|character| character.is_ascii_digit())
            && let Ok(place) = place.parse::<usize>()
            && self.label.trim().eq_ignore_ascii_case(base.trim())
        {
            return (place == self.ordinal && place >= 2).then_some(0);
        }
        if same(self.label.trim()) && self.ordinal > 1 {
            return None;
        }
        let tail = |text: &str| {
            text.rsplit(['.', ':', '/'])
                .next()
                .is_some_and(|tail| tail.eq_ignore_ascii_case(name))
        };
        if same(&self.label) {
            return Some(0);
        }
        if self.names_id(name) {
            return Some(1);
        }
        if same(&self.resource_type) || tail(&self.resource_type) {
            return Some(2);
        }
        let lower = name.to_lowercase();
        let starts = |text: &str| text.to_lowercase().starts_with(&lower);
        (lower.chars().count() >= 3
            && (starts(&self.label) || (lower.len() >= 6 && starts(self.token()))))
        .then_some(3)
    }

    /// Whether `name` is this resource's own id, or the item part of it
    /// (`<kind>/<field>/<item>`): an id names the resource before any label
    /// does, whatever the label says.
    pub fn names_id(&self, name: &str) -> bool {
        let same = |text: &str| text.trim().eq_ignore_ascii_case(name);
        let key_tail = self.key.rsplit('/').next().unwrap_or_default();
        same(&self.key) || same(&self.id) || same(key_tail) || same(self.token())
    }

    /// The name that goes in a path: its label when it tells it apart from
    /// `siblings`, else its key's last part (`body`), else its key.
    pub fn name(&self, siblings: &[Node]) -> String {
        self.name_in(&Names::of(siblings))
    }

    /// [`Self::name`] among siblings already indexed by their titles.
    pub fn name_in(&self, siblings: &Names<'_>) -> String {
        // Neither what belongs to this resource nor what it is a part of
        // competes with it: a note titled like its notebook keeps its title.
        let apart = |name: &str| {
            !name.trim().is_empty()
                && siblings
                    .first_named(name)
                    .into_iter()
                    .filter(|other| other.id != self.id)
                    .all(|other| self.owns(other) || self.within(other))
        };
        let label = self.label.trim();
        // A page and the body published beside it share a title but not a
        // level: only what sits beside this one takes its title.
        let twin = !label.is_empty()
            && siblings.titled(label).any(|other| {
                other.id != self.id
                    && other.parent == self.parent
                    && other.label.trim().eq_ignore_ascii_case(label)
            });
        if !label.is_empty() && (apart(label) || twin) {
            // The one a title names first keeps it; the others wear their
            // place among them, which the path accepts back.
            if twin && self.ordinal > 1 {
                return path_name(&format!("{label} {}", self.ordinal));
            }
            return path_name(label);
        }
        let tail = self.key.rsplit('/').next().unwrap_or_default();
        if tail != self.key && apart(tail) {
            return path_name(tail);
        }
        path_name(&self.key)
    }
}

/// The resources and actions one directory listing holds under `parent`.
#[derive(Clone, Default)]
pub struct Level {
    /// Actions of the parent itself.
    pub own: Vec<ActionDescriptor>,
    pub children: Vec<Node>,
    /// The directory revision it was read at.
    pub revision: String,
    /// The pages it was read from ([`LevelPages`]).
    pub pages: std::sync::Arc<LevelPages>,
}

/// The level under `parent` that `rows`, one listing's rows, hold: its own
/// actions and its children, each with the actions placed on it and its place
/// among the resources beside it that share its title.
#[must_use]
pub fn level_of(rows: &[CatalogueRow], parent: &str) -> Level {
    let mut level = Level::default();
    let mut at: HashMap<&str, usize> = HashMap::new();
    // The actions a page of compact rows described whole, by provider, kind and id: each row
    // after names them by id ([`tilo_actions::remote::compact`]), and its node takes each
    // placed on it straight from here, never a row per action first.
    let mut described: HashMap<(&str, &str, &str), &ActionDescriptor> = HashMap::new();
    let mut placed: Vec<ActionDescriptor> = Vec::new();
    for row in rows {
        placed.clear();
        let mut identity = None;
        match &row.offered {
            Some(offered) => {
                for action in &offered.described {
                    described.insert(
                        (
                            row.provider.as_str(),
                            action.resource_kind.as_str(),
                            action.address.action.as_str(),
                        ),
                        action,
                    );
                }
                identity = offered.semantic_identity.as_ref();
                for id in &offered.actions {
                    let key = (
                        row.provider.as_str(),
                        row.resource.kind.as_str(),
                        id.as_str(),
                    );
                    let Some(like) = described.get(&key) else {
                        continue;
                    };
                    let mut action = (*like).clone();
                    action.address = ActionAddress::new(row.resource.id.as_str(), id);
                    action.resource_label.clone_from(&row.resource.label);
                    action
                        .semantic_identity
                        .clone_from(&offered.semantic_identity);
                    placed.push(action);
                }
            }
            None => {
                if let Some(action) = &row.action {
                    identity = action.semantic_identity.as_ref();
                    placed.push(action.clone());
                }
            }
        }
        if !parent.is_empty() && row.resource.id == parent {
            level.own.append(&mut placed);
            continue;
        }
        let key = identity
            .map(|identity| identity.key.clone())
            .or_else(|| bound_key(&row.resource.id));
        let index = *at.entry(row.resource.id.as_str()).or_insert_with(|| {
            level.children.push(Node {
                id: row.resource.id.clone(),
                resource_type: row.resource.kind.clone(),
                label: row.resource.label.clone(),
                key: key.clone().unwrap_or_else(|| row.resource.id.clone()),
                parent: row.parent_resource.clone(),
                ordinal: 1,
                container: false,
                actions: Vec::new(),
            });
            level.children.len().saturating_sub(1)
        });
        if let Some(node) = level.children.get_mut(index) {
            node.container |= row.has_children;
            if let Some(key) = key.filter(|_| node.key == node.id) {
                node.key = key;
            }
            node.actions.append(&mut placed);
        }
    }
    // Where each item stands among the siblings sharing its title: what a
    // path names it by when its title alone would name several.
    let mut seen: HashMap<String, usize> = HashMap::new();
    for node in &mut level.children {
        let title = node.label.trim().to_lowercase();
        let count = seen.entry(title).or_insert(0);
        *count = count.saturating_add(1);
        node.ordinal = *count;
    }
    level
}

impl Node {
    /// [`Self::name`] as a search result names it: a name that would be
    /// written with escapes (a file's title, `files/legal/contract.txt`)
    /// gives way to its key's last part, which an address takes as well, so
    /// the path reads plainly and the title is shown beside it whole.
    pub fn found_name(&self, among: &[Node]) -> String {
        let name = self.name(among);
        if !name.contains('\\') {
            return name;
        }
        let tail = self.key.rsplit('/').next().unwrap_or_default();
        for plain in [tail, self.token()] {
            if !plain.is_empty() && !plain.contains(['/', '#', '\\']) {
                return plain.to_owned();
            }
        }
        name
    }

    /// Whether `address` names this resource whole, as an action answers it
    /// (`tasks.tracker/issues/7`): the id it is published under, or its key.
    pub fn address_is(&self, address: &str) -> bool {
        self.id == address || self.key == address
    }
}

/// The rows a [`Level`] was read from, where each of its pages after the
/// first started, and, when the read stopped at its bound before the last
/// page, where the directory goes on: a listing paging past what was read
/// reads on from there (the walk that reads a level on), and the next page of
/// a listing starts at the page its first resource was read in
/// ([`LsCursor`]).
#[derive(Clone, Default)]
pub struct LevelPages {
    pub rows: Vec<CatalogueRow>,
    /// The page the rows start in, when the level was read on from a mark: a
    /// page whose first resources are in it starts there. `None` when the level
    /// was read from the directory's start, whose page is asked for with no
    /// cursor.
    pub first: Option<PageMark>,
    pub marks: Vec<PageMark>,
    pub rest: Option<PageMark>,
}

/// Where one page of a directory starts: the row it starts at among those
/// read, and the cursor and revision that ask for it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PageMark {
    pub row: usize,
    pub cursor: Vec<u8>,
    pub revision: String,
}

impl Level {
    /// Whether the directory goes on past what was read.
    pub fn unread(&self) -> bool {
        self.pages.rest.is_some()
    }

    /// The resources of the level every row of which was read. A read that stopped at its
    /// bound may have stopped inside its last resource, whose other rows (the actions it
    /// offers, the one that reads it among them) are on the page after: that one is no
    /// resource to name or act on until the level is read on.
    pub fn whole_children(&self) -> &[Node] {
        match self.children.split_last() {
            Some((_, whole)) if self.unread() => whole,
            _ => &self.children,
        }
    }

    /// Whether the provider's cursors the level was read by are row offsets ([`row_offset`]),
    /// as far as a cursor it handed out says.
    fn pages_by_offset(&self) -> bool {
        self.pages
            .first
            .iter()
            .chain(self.pages.rest.iter())
            .chain(&self.pages.marks)
            .next()
            .is_some_and(|mark| mark.cursor.len() == size_of::<u64>())
    }

    /// The provider's cursor for the row the resource at `child` among the
    /// level's rows starts at, the revision it was read at, and how many
    /// resources that cursor's page holds before it: where a listing's next
    /// page asks for its first resource, so that page reads from there and not
    /// from the level's first one. A provider that names its rows by offset is
    /// asked at the row itself, so nothing of its page comes before it; one
    /// whose cursors are its own names the page the row is in, and the
    /// resources before it that page holds. A resource past the rows read
    /// starts at the page the level goes on from; `None` only when the rows
    /// read cover the directory whole, so there is nowhere to go on from.
    pub fn row_mark(&self, parent: &str, child: usize) -> Option<(Vec<u8>, String, usize)> {
        let rows = &self.pages.rows;
        let mut seen: HashSet<&str> = HashSet::new();
        let mut at = None;
        for (index, row) in rows.iter().enumerate() {
            let id = row.resource.id.as_str();
            if (!parent.is_empty() && id == parent) || !seen.insert(id) {
                continue;
            }
            if seen.len() == child.saturating_add(1) {
                at = Some(index);
                break;
            }
        }
        let by_offset = self.pages_by_offset();
        let Some(at) = at else {
            // The page asks for a resource past those read: it stands on the
            // page the directory goes on from, if it does, and else after
            // every row read, where the directory holds nothing more.
            if let Some(rest) = self.pages.rest.as_ref() {
                return Some((rest.cursor.clone(), rest.revision.clone(), 0));
            }
            if !by_offset {
                // A provider whose cursor is its own cannot be asked for a row
                // past the page it last handed out: the page after reads the
                // level again from its first page.
                return None;
            }
            let mark = self.pages.marks.last()?;
            let offset = row_offset(&mark.cursor)
                .saturating_add(spanned(rows.get(mark.row..).unwrap_or_default()));
            return Some((
                u64::try_from(offset)
                    .unwrap_or(u64::MAX)
                    .to_le_bytes()
                    .to_vec(),
                mark.revision.clone(),
                0,
            ));
        };
        // The marks a level leaves are pages of it, each with the provider's
        // cursor it started at. A provider counting rows by offset is asked at
        // the wanted row itself: the rows since the mark move that on by as many as each
        // stands for. One whose cursors are its own names the page the row is in, and the
        // resources before it that page holds. A row of the page the level started in,
        // which leaves no mark, is that many resources into that page, asked for by the
        // cursor the level was read on from, or by none when it was read from the start.
        let Some(mark) = self.pages.marks.iter().rev().find(|mark| mark.row <= at) else {
            let skip = distinct_children(rows.get(..at).unwrap_or_default(), parent);
            if let Some(first) = self.pages.first.as_ref() {
                // The level was read on from a mark, and the row is still in that mark's
                // page: the page the level started in asks for it again.
                if by_offset {
                    let offset = row_offset(&first.cursor)
                        .saturating_add(spanned(rows.get(..at).unwrap_or_default()));
                    return Some((
                        u64::try_from(offset)
                            .unwrap_or(u64::MAX)
                            .to_le_bytes()
                            .to_vec(),
                        first.revision.clone(),
                        0,
                    ));
                }
                return (!first.revision.is_empty()).then_some((
                    first.cursor.clone(),
                    first.revision.clone(),
                    skip,
                ));
            }
            if !by_offset {
                // The directory's start is asked for with no cursor, and its revision names
                // the page read: a page that reads on from it is refused as stale once the
                // directory moves on.
                return (!self.revision.is_empty())
                    .then(|| (Vec::new(), self.revision.clone(), skip));
            }
            if self.revision.is_empty() {
                return None;
            }
            let offset = spanned(rows.get(..at).unwrap_or_default());
            return Some((
                u64::try_from(offset)
                    .unwrap_or(u64::MAX)
                    .to_le_bytes()
                    .to_vec(),
                self.revision.clone(),
                0,
            ));
        };
        if !by_offset {
            return Some((
                mark.cursor.clone(),
                mark.revision.clone(),
                distinct_children(rows.get(mark.row..at).unwrap_or_default(), parent),
            ));
        }
        let offset = row_offset(&mark.cursor)
            .saturating_add(spanned(rows.get(mark.row..at).unwrap_or_default()));
        Some((
            u64::try_from(offset)
                .unwrap_or(u64::MAX)
                .to_le_bytes()
                .to_vec(),
            mark.revision.clone(),
            0,
        ))
    }
}

/// The row a provider's cursor names: how many rows of the directory it
/// stands at.
#[must_use]
pub fn row_offset(cursor: &[u8]) -> usize {
    <[u8; 8]>::try_from(cursor)
        .map(u64::from_le_bytes)
        .ok()
        .and_then(|offset| usize::try_from(offset).ok())
        .unwrap_or(0)
}

/// How many of the provider's rows `rows` stand for: a resource's actions sent as one row
/// ([`CatalogueQuery::by_resource`]) are a row each where the provider counts its cursor.
#[must_use]
pub fn spanned(rows: &[CatalogueRow]) -> usize {
    rows.iter()
        .map(|row| {
            row.offered
                .as_ref()
                .map_or(1, |offered| offered.actions.len().max(1))
        })
        .sum()
}

/// How many resources of the level under `parent` `rows` name: each once,
/// and never `parent` itself, whose own actions are the level's own.
#[must_use]
pub fn distinct_children(rows: &[CatalogueRow], parent: &str) -> usize {
    let mut seen = HashSet::new();
    for row in rows {
        if row.resource.id != parent {
            seen.insert(row.resource.id.as_str());
        }
    }
    seen.len()
}