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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! What a person has selected, carried to whoever is asked to act on it.
//!
//! A person selects a sentence in a document, a range of cells or a row in a
//! list, and asks an agent about it. The agent must act at exactly that place
//! through the actions the resource publishes, so the selection travels in
//! the resource's own terms: a [`SelectionPlace`] is the same value an action
//! takes as its place argument (`{"item": id, "start": n, "end": n}`, in
//! characters), an [`InteractionAnchor`] names selections that are not text
//! (cells, objects, rows), and a short excerpt lets a reader see what was
//! selected without opening the resource. None of it grants authority: the
//! agent still reaches the resource only through permission-filtered actions.

use serde::{Deserialize, Serialize};

use crate::InteractionAnchor;

/// What a resource id starts with when a window publishes it under the
/// toolkit's transport alias: `<prefix><host>:<publication>:<hex of the
/// app's own id>`. The window's actions are addressed by the alias; the app
/// names the resource by its own id (a selection's subject does), so a
/// reader matching the two decodes the alias ([`logical_resource_id`]).
pub const BOUND_RESOURCE_PREFIX: &str = "tilo-ui-bound:v1:";

/// The app's own id for the published resource `id`: the id inside a
/// [`BOUND_RESOURCE_PREFIX`] alias, or `id` itself when it is no alias (or a
/// malformed one).
#[must_use]
pub fn logical_resource_id(id: &str) -> std::borrow::Cow<'_, str> {
    let decoded = id.strip_prefix(BOUND_RESOURCE_PREFIX).and_then(|rest| {
        let hex = rest.rsplit(':').next()?;
        if hex.is_empty() || hex.len() % 2 != 0 {
            return None;
        }
        let bytes = (0..hex.len())
            .step_by(2)
            .map(|at| u8::from_str_radix(hex.get(at..at.checked_add(2)?)?, 16).ok())
            .collect::<Option<Vec<u8>>>()?;
        String::from_utf8(bytes)
            .ok()
            .filter(|key| !key.is_empty() && !key.chars().any(char::is_control))
    });
    decoded.map_or(std::borrow::Cow::Borrowed(id), std::borrow::Cow::Owned)
}

/// The most characters an excerpt keeps. A selection is context for a
/// request, not its payload: a longer one is cut and ends in `…`.
pub const MAX_EXCERPT_CHARS: usize = 1000;
/// The most bytes an excerpt may take at a trust boundary.
pub const MAX_EXCERPT_BYTES: usize = 4 * 1024;
/// The most bytes a place's item id may take.
pub const MAX_PLACE_ITEM_BYTES: usize = 512;
/// The most bytes an interaction anchor may take, encoded.
pub const MAX_SELECTION_ANCHOR_BYTES: usize = 8 * 1024;
/// The most items a selection of several items (files, rows, cards) names
/// one by one; the excerpt counts the rest.
pub const MAX_SELECTED_ITEMS: usize = 64;

/// Where a selection is inside a resource: an item, a caret or a character
/// range inside one item. Its JSON form is exactly what an action's place
/// parameter takes (`tilo_resource::Place`), so an agent hands it back
/// unchanged.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectionPlace {
    pub item: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub at: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start: Option<usize>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end: Option<usize>,
}

/// The validated position inside a [`SelectionPlace`]'s item.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SelectionSpan {
    Item,
    Caret(usize),
    Range { start: usize, end: usize },
}

impl SelectionPlace {
    /// The whole of `item`.
    #[must_use]
    pub fn item(item: impl Into<String>) -> Self {
        Self {
            item: item.into(),
            at: None,
            start: None,
            end: None,
        }
    }

    #[must_use]
    pub fn caret(item: impl Into<String>, at: usize) -> Self {
        Self {
            at: Some(at),
            ..Self::item(item)
        }
    }

    /// Characters `start..end` of `item`; the bounds are put in order.
    #[must_use]
    pub fn range(item: impl Into<String>, start: usize, end: usize) -> Self {
        Self {
            start: Some(start.min(end)),
            end: Some(start.max(end)),
            ..Self::item(item)
        }
    }

    /// Validate this place and return its position inside [`Self::item`]:
    /// a whole item, a caret `at`, or an ordered range `start..end`.
    ///
    /// # Errors
    /// Returns a reason when `item` is empty, oversized, or holds control
    /// characters, or when `at`, `start`, and `end` do not name one item,
    /// caret, or ordered range.
    pub fn validated_span(&self) -> Result<SelectionSpan, String> {
        if self.item.is_empty() || self.item.len() > MAX_PLACE_ITEM_BYTES {
            return Err("a selection place names one item of bounded length".into());
        }
        if self.item.chars().any(char::is_control) {
            return Err("a selection place item contains control characters".into());
        }
        match (self.at, self.start, self.end) {
            (None, None, None) => Ok(SelectionSpan::Item),
            (Some(at), None, None) => Ok(SelectionSpan::Caret(at)),
            (None, Some(start), Some(end)) if start <= end => {
                Ok(SelectionSpan::Range { start, end })
            }
            _ => Err(
                "a place is an item, a caret `at` in it, or an ordered range `start..end` of it"
                    .into(),
            ),
        }
    }

    /// Whether this is a well-formed place, the shape an action's place
    /// parameter accepts.
    pub fn validate(&self) -> Result<(), String> {
        self.validated_span().map(|_| ())
    }
}

/// What was selected: where (a place, an app-native anchor, or both) and a
/// short excerpt of it. Every part is optional; an empty anchor says
/// nothing and is dropped by [`Self::non_empty`].
///
/// Encoded as a JSON object in JSON, and as that object's JSON text in a
/// binary format, because an [`InteractionAnchor`] reads only from JSON.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SelectionAnchor {
    pub place: Option<SelectionPlace>,
    /// Every item of a selection of several (files in a folder, rows of a
    /// list), each the whole of one item, in the order shown. Empty for a
    /// selection inside one item, which [`Self::place`] names.
    pub items: Vec<SelectionPlace>,
    pub interaction: Option<InteractionAnchor>,
    pub excerpt: Option<String>,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct SelectionAnchorWire {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    place: Option<SelectionPlace>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    items: Vec<SelectionPlace>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    interaction: Option<InteractionAnchor>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    excerpt: Option<String>,
}

impl Serialize for SelectionAnchor {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let wire = SelectionAnchorWire {
            place: self.place.clone(),
            items: self.items.clone(),
            interaction: self.interaction.clone(),
            excerpt: self.excerpt.clone(),
        };
        if serializer.is_human_readable() {
            wire.serialize(serializer)
        } else {
            serde_json::to_string(&wire)
                .map_err(serde::ser::Error::custom)?
                .serialize(serializer)
        }
    }
}

impl<'de> Deserialize<'de> for SelectionAnchor {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let wire = if deserializer.is_human_readable() {
            SelectionAnchorWire::deserialize(deserializer)?
        } else {
            let text = String::deserialize(deserializer)?;
            serde_json::from_str(&text).map_err(serde::de::Error::custom)?
        };
        Ok(Self {
            place: wire.place,
            items: wire.items,
            interaction: wire.interaction,
            excerpt: wire.excerpt,
        })
    }
}

impl SelectionAnchor {
    /// An anchor of `excerpt` alone: what is known of a selection in an app
    /// that publishes no resources, read from the primary selection.
    #[must_use]
    pub fn of_text(excerpt: &str) -> Self {
        Self {
            excerpt: excerpt_of(excerpt),
            ..Self::default()
        }
    }

    /// A selection of several whole items, each `(id, label)` in the order
    /// shown: every item named as a place (at most [`MAX_SELECTED_ITEMS`]),
    /// and an excerpt listing their labels, counting any beyond that.
    #[must_use]
    pub fn of_items<I, L>(items: impl IntoIterator<Item = (I, L)>) -> Self
    where
        I: Into<String>,
        L: AsRef<str>,
    {
        let mut places = Vec::new();
        let mut labels = Vec::new();
        let mut total = 0_usize;
        for (id, label) in items {
            total = total.saturating_add(1);
            if places.len() == MAX_SELECTED_ITEMS {
                continue;
            }
            let id = id.into();
            let label = label.as_ref().trim();
            labels.push(if label.is_empty() {
                id.clone()
            } else {
                label.to_owned()
            });
            places.push(SelectionPlace::item(id));
        }
        // List names while they fit the excerpt, leaving room to count the
        // rest, so the excerpt never ends cut mid-name.
        let budget = MAX_EXCERPT_CHARS.saturating_sub(32);
        let mut listed = String::new();
        let mut shown = 0_usize;
        for label in &labels {
            let line = format!("- {label}");
            let used = listed
                .chars()
                .count()
                .saturating_add(line.chars().count())
                .saturating_add(1);
            if used > budget {
                break;
            }
            if !listed.is_empty() {
                listed.push('\n');
            }
            listed.push_str(&line);
            shown = shown.saturating_add(1);
        }
        let more = total.saturating_sub(shown);
        if more > 0 {
            if !listed.is_empty() {
                listed.push('\n');
            }
            listed.push_str("- and ");
            listed.push_str(&more.to_string());
            listed.push_str(" more");
        }
        Self {
            items: places,
            excerpt: excerpt_of(&listed),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn with_excerpt(mut self, excerpt: &str) -> Self {
        self.excerpt = excerpt_of(excerpt);
        self
    }

    #[must_use]
    pub fn at(mut self, place: SelectionPlace) -> Self {
        self.place = Some(place);
        self
    }

    /// The same anchor noting the whole of a selection that spans several
    /// blocks of a text, `ranges` in document order, of which [`Self::place`]
    /// is one block's part (an action's place names one block). One range
    /// says nothing more than the place, so it is left out; a long
    /// selection keeps its first [`crate::interaction::MAX_ANCHOR_ITEMS`].
    #[must_use]
    pub fn spanning(mut self, region: &str, mut ranges: Vec<crate::InteractionTextRange>) -> Self {
        if ranges.len() > 1 {
            ranges.truncate(crate::interaction::MAX_ANCHOR_ITEMS);
            self.interaction = Some(InteractionAnchor::Text {
                region: region.to_owned(),
                ranges,
            });
        }
        self
    }

    /// The block ranges of a selection spanning several blocks
    /// ([`Self::spanning`]).
    #[must_use]
    pub fn spanned_ranges(&self) -> Option<&[crate::InteractionTextRange]> {
        match &self.interaction {
            Some(InteractionAnchor::Text { ranges, .. }) if ranges.len() > 1 => Some(ranges),
            _ => None,
        }
    }

    /// Whether the anchor says nothing at all.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.place.is_none()
            && self.items.is_empty()
            && self.interaction.is_none()
            && self.excerpt.is_none()
    }

    /// The anchor, unless it says nothing.
    #[must_use]
    pub fn non_empty(self) -> Option<Self> {
        (!self.is_empty()).then_some(self)
    }

    /// Whether the anchor is within bounds, so a receiver may keep it.
    pub fn validate(&self) -> Result<(), String> {
        if let Some(place) = &self.place {
            place.validate()?;
        }
        if self.items.len() > MAX_SELECTED_ITEMS {
            return Err("a selection names too many items".into());
        }
        for item in &self.items {
            item.validate()?;
        }
        if let Some(excerpt) = &self.excerpt
            && (excerpt.len() > MAX_EXCERPT_BYTES
                || excerpt.chars().count() > MAX_EXCERPT_CHARS + 1
                || excerpt
                    .chars()
                    .any(|c| c.is_control() && !matches!(c, '\n' | '\t')))
        {
            return Err("a selection excerpt is oversized or contains control characters".into());
        }
        if let Some(interaction) = &self.interaction {
            let encoded = serde_json::to_vec(interaction).map_err(|error| error.to_string())?;
            if encoded.len() > MAX_SELECTION_ANCHOR_BYTES {
                return Err("a selection's interaction anchor is oversized".into());
            }
        }
        Ok(())
    }
}

/// What a request is about: the resource a person had selected something in,
/// and the selection. Only the subject of the request: it carries no
/// authority, so a run about a selection may act anywhere its starter and
/// its agent may (other resources, other apps), as any task may.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SelectionSubject {
    /// The application the resource is in, by package id.
    pub app_id: String,
    pub app_label: String,
    pub resource: crate::ActionResource,
    pub anchor: SelectionAnchor,
}

impl SelectionSubject {
    /// Whether the subject is within bounds, so a receiver may keep it.
    pub fn validate(&self) -> Result<(), String> {
        for (value, maximum, name) in [
            (self.app_id.as_str(), 256, "app"),
            (self.app_label.as_str(), 512, "app label"),
            (self.resource.id.as_str(), 2048, "resource id"),
            (self.resource.kind.as_str(), 128, "resource kind"),
            (self.resource.label.as_str(), 512, "resource label"),
        ] {
            if value.len() > maximum || value.chars().any(char::is_control) {
                return Err(format!("a selection's {name} is oversized or malformed"));
            }
        }
        if self.resource.id.trim().is_empty() || self.app_id.trim().is_empty() {
            return Err("a selection names its application and resource".into());
        }
        if self.anchor.is_empty() {
            return Err("a selection says what was selected".into());
        }
        self.anchor.validate()
    }

    /// Whether the published resource `id` is the one this selection is in:
    /// the same id, or a window's transport alias of it
    /// ([`logical_resource_id`]).
    #[must_use]
    pub fn is_in(&self, id: &str) -> bool {
        id == self.resource.id || logical_resource_id(id) == self.resource.id
    }
}

/// `text` as a selection excerpt: trimmed, at most [`MAX_EXCERPT_CHARS`]
/// characters (a longer one is cut and ends in `…`), without control
/// characters other than line breaks and tabs. `None` when nothing is left.
#[must_use]
pub fn excerpt_of(text: &str) -> Option<String> {
    let text = text.trim();
    if text.is_empty() {
        return None;
    }
    let mut kept = text
        .chars()
        .filter(|c| !c.is_control() || matches!(c, '\n' | '\t'));
    let mut excerpt: String = kept.by_ref().take(MAX_EXCERPT_CHARS).collect();
    let mut cut = kept.next().is_some();
    while excerpt.len() > MAX_EXCERPT_BYTES.saturating_sub('…'.len_utf8()) {
        excerpt.pop();
        cut = true;
    }
    if cut {
        excerpt.push('…');
    }
    Some(excerpt)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn anchor() -> SelectionAnchor {
        SelectionAnchor {
            place: Some(SelectionPlace::range("block-1", 4, 9)),
            items: Vec::new(),
            interaction: Some(InteractionAnchor::Objects {
                ids: vec!["block-1".into()],
            }),
            excerpt: Some("quick".into()),
        }
    }

    tilo_test_env::scenario! {
        fn a_selection_names_its_place_and_travels_bounded;

        phase a_selection_is_in_its_resource_under_a_window_alias_too;
        {
            let body = "notes.notebook/pages/p1/body";
            let alias = format!(
                "{BOUND_RESOURCE_PREFIX}2a4120d7-2f63-4578-aed8-11cb91ed3628:\
             c0fe4ad7-b7d0-46c3-ab7e-78d12d5d86f4:6e6f7465732e6e6f7465626f6f6b2f70616765732f70312f626f6479"
            );
            assert_eq!(logical_resource_id(&alias), body);
            assert_eq!(logical_resource_id(body), body);
            assert_eq!(
                logical_resource_id(&format!("{BOUND_RESOURCE_PREFIX}a:b:zz")),
                format!("{BOUND_RESOURCE_PREFIX}a:b:zz"),
                "a malformed alias is its own id"
            );
            let subject = SelectionSubject {
                app_id: "so.tilo.notes".into(),
                app_label: "Notes".into(),
                resource: crate::ActionResource {
                    id: body.into(),
                    kind: "tilo.rich-text".into(),
                    label: "Links".into(),
                },
                anchor: anchor(),
            };
            assert!(subject.is_in(body));
            assert!(subject.is_in(&alias));
            assert!(!subject.is_in("notes.notebook/pages/p2/body"));
        }

        phase a_selection_of_several_items_names_each_and_lists_them;
        {
            let anchor = SelectionAnchor::of_items(
                (0..MAX_SELECTED_ITEMS + 2).map(|n| (format!("file:{n}"), format!("report-{n}.pdf"))),
            );
            assert_eq!(anchor.items.len(), MAX_SELECTED_ITEMS);
            assert_eq!(anchor.items[1], SelectionPlace::item("file:1"));
            let excerpt = anchor.excerpt.as_deref().unwrap();
            assert!(excerpt.starts_with("- report-0.pdf\n- report-1.pdf\n"));
            assert!(excerpt.ends_with(" more"), "{excerpt}");
            assert!(!excerpt.contains('…'), "the list is never cut mid-name");
            let listed = excerpt
                .lines()
                .filter(|line| line.starts_with("- report-"))
                .count();
            assert!(
                excerpt.ends_with(&format!("- and {} more", MAX_SELECTED_ITEMS + 2 - listed)),
                "{excerpt}"
            );
            assert!(anchor.validate().is_ok());
            let json = serde_json::to_value(&anchor).unwrap();
            assert_eq!(json["items"][0], serde_json::json!({"item": "file:0"}));
            assert_eq!(
                serde_json::from_value::<SelectionAnchor>(json).unwrap(),
                anchor
            );
            let encoded = postcard::to_allocvec(&anchor).unwrap();
            assert_eq!(
                postcard::from_bytes::<SelectionAnchor>(&encoded).unwrap(),
                anchor
            );
            assert!(
                serde_json::to_value(SelectionAnchor::of_text("x"))
                    .unwrap()
                    .get("items")
                    .is_none(),
                "a selection inside one item writes what it wrote before"
            );
            let too_many = SelectionAnchor {
                items: vec![SelectionPlace::item("f"); MAX_SELECTED_ITEMS + 1],
                ..SelectionAnchor::default()
            };
            assert!(too_many.validate().is_err());
        }

        phase place_travels_as_the_action_place_parameter_does;
        {
            let place = SelectionPlace::range("block-1", 9, 4);
            assert_eq!(
                serde_json::to_value(&place).unwrap(),
                serde_json::json!({"item": "block-1", "start": 4, "end": 9})
            );
            assert_eq!(
                serde_json::to_value(SelectionPlace::caret("b", 2)).unwrap(),
                serde_json::json!({"item": "b", "at": 2})
            );
            assert!(
                crate::validate_json_schema(
                    &serde_json::to_value(&place).unwrap(),
                    &crate::place_schema()
                )
                .is_ok()
            );
        }

        phase malformed_places_are_refused;
        {
            assert!(SelectionPlace::item("").validate().is_err());
            let both = SelectionPlace {
                at: Some(1),
                ..SelectionPlace::range("b", 1, 2)
            };
            assert!(both.validate().is_err());
            let open = SelectionPlace {
                start: Some(3),
                ..SelectionPlace::item("b")
            };
            assert!(open.validate().is_err());
            let backwards = SelectionPlace {
                start: Some(3),
                end: Some(1),
                ..SelectionPlace::item("b")
            };
            assert!(backwards.validate().is_err());
            assert!(
                serde_json::from_value::<SelectionPlace>(serde_json::json!({"item":"b","x":1}))
                    .is_err()
            );
        }

        phase anchor_round_trips_through_json_and_postcard;
        {
            let anchor = anchor();
            let json = serde_json::to_value(&anchor).unwrap();
            assert_eq!(json["place"]["start"], 4);
            assert_eq!(json["interaction"]["kind"], "objects");
            assert_eq!(
                serde_json::from_value::<SelectionAnchor>(json).unwrap(),
                anchor
            );
            let bytes = postcard::to_allocvec(&anchor).unwrap();
            assert_eq!(
                postcard::from_bytes::<SelectionAnchor>(&bytes).unwrap(),
                anchor
            );
        }

        phase absent_parts_are_omitted_and_older_readers_default_them;
        {
            let only_text = SelectionAnchor::of_text("  hello  ");
            assert_eq!(
                serde_json::to_value(&only_text).unwrap(),
                serde_json::json!({"excerpt": "hello"})
            );
            assert_eq!(
                serde_json::from_value::<SelectionAnchor>(serde_json::json!({})).unwrap(),
                SelectionAnchor::default()
            );
            assert!(SelectionAnchor::of_text("   ").non_empty().is_none());
        }

        phase excerpts_are_bounded_and_cut_visibly;
        {
            let long = "word ".repeat(400);
            let excerpt = excerpt_of(&long).unwrap();
            assert_eq!(excerpt.chars().count(), MAX_EXCERPT_CHARS + 1);
            assert!(excerpt.ends_with('…'));
            assert!(SelectionAnchor::of_text(&long).validate().is_ok());
            let wide = "界".repeat(MAX_EXCERPT_CHARS * 2);
            let excerpt = excerpt_of(&wide).unwrap();
            assert!(excerpt.len() <= MAX_EXCERPT_BYTES);
            assert!(excerpt.ends_with('…'));
            assert_eq!(excerpt_of("a\u{7}b\nc").as_deref(), Some("ab\nc"));
            let forged = SelectionAnchor {
                excerpt: Some("x".repeat(MAX_EXCERPT_BYTES + 1)),
                ..SelectionAnchor::default()
            };
            assert!(forged.validate().is_err());
        }
    }
}