vvv-core 0.2.0

Domain types and plugin traits for vvv
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
//! Names and paths as types, never as text.
//!
//! A [`Name`] is one identifier. A [`ModulePath`] is a path *as a language
//! spells it* — `crate::a::b`, `../x/y`, `react/jsx-runtime` — parsed once
//! by the grammar's [`PathSyntax`] when facts are extracted, and spelled
//! back only by that syntax. What a path *means* is an
//! [`Address`](crate::resolve::Address), which the layout resolves; the two
//! never mix. Nothing above the grammar ever sees a separator character.

use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

/// One identifier: a module, a declaration, a file stem. Compared by text.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(transparent)]
pub struct Name(String);

impl Name {
    pub fn new(text: impl Into<String>) -> Self {
        Self(text.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// The path as this machine spells it, for an actual file call.
    pub fn as_path(&self) -> &Path {
        Path::new(&self.0)
    }
}

impl Deref for Name {
    type Target = str;
    fn deref(&self) -> &str {
        &self.0
    }
}

impl Borrow<str> for Name {
    fn borrow(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for Name {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl PartialEq<str> for Name {
    fn eq(&self, other: &str) -> bool {
        self.0 == other
    }
}

impl PartialEq<&str> for Name {
    fn eq(&self, other: &&str) -> bool {
        self.0 == *other
    }
}

impl PartialEq<String> for Name {
    fn eq(&self, other: &String) -> bool {
        &self.0 == other
    }
}

impl From<&str> for Name {
    fn from(text: &str) -> Self {
        Self::new(text)
    }
}

impl From<String> for Name {
    fn from(text: String) -> Self {
        Self(text)
    }
}

impl From<&String> for Name {
    fn from(text: &String) -> Self {
        Self(text.clone())
    }
}

impl From<Name> for String {
    fn from(name: Name) -> Self {
        name.0
    }
}

impl fmt::Display for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for Name {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

/// A file's path inside the workspace, spelled the way the project does: `/`
/// on every platform.
///
/// A workspace path is a project path — like a `mod` path or a
/// `crate::a::b` address — not a path of the machine, so it carries its own
/// spelling and is what goes on the wire, into a row, or into a diagnostic.
/// It reads as a [`Path`] (an OS path is made from one at the edge, where a
/// file is actually read), but it cannot spell a separator the host's way:
/// `Display`, serde and the stored bytes are always `/`.
#[derive(Clone, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(transparent)]
pub struct RelPath(String);

impl RelPath {
    /// Whatever the host spells a path with, a workspace path is `/`.
    /// Constructors and deserialization normalize host separators alike;
    /// literal backslashes in Unix filenames are preserved.
    pub fn new(path: &Path) -> Self {
        Self::from(path.display().to_string())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// The path as this machine spells it, for an actual file call.
    pub fn as_path(&self) -> &Path {
        Path::new(&self.0)
    }

    /// The last two components: enough to tell rows apart, short enough for
    /// one. The full path is in the detail.
    pub fn short(&self) -> String {
        let parts: Vec<String> = self
            .as_path()
            .components()
            .map(|c| c.as_os_str().to_string_lossy().into_owned())
            .collect();
        let n = parts.len();
        parts[n.saturating_sub(2)..].join("/")
    }

    /// `/` on every platform; on Unix this is the text unchanged.
    fn separators(text: String) -> String {
        if std::path::MAIN_SEPARATOR == '/' {
            text
        } else {
            text.replace(std::path::MAIN_SEPARATOR, "/")
        }
    }
}

impl Deref for RelPath {
    type Target = Path;
    fn deref(&self) -> &Path {
        Path::new(&self.0)
    }
}

impl AsRef<Path> for RelPath {
    fn as_ref(&self) -> &Path {
        self
    }
}

impl AsRef<str> for RelPath {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Borrow<Path> for RelPath {
    fn borrow(&self) -> &Path {
        self
    }
}

impl PartialEq<Path> for RelPath {
    fn eq(&self, other: &Path) -> bool {
        self.as_path() == other
    }
}

impl PartialEq<&Path> for RelPath {
    fn eq(&self, other: &&Path) -> bool {
        self.as_path() == *other
    }
}

impl PartialEq<RelPath> for Path {
    fn eq(&self, other: &RelPath) -> bool {
        self == other.as_path()
    }
}

impl PartialEq<PathBuf> for RelPath {
    fn eq(&self, other: &PathBuf) -> bool {
        self.as_path() == other.as_path()
    }
}

impl PartialEq<RelPath> for PathBuf {
    fn eq(&self, other: &RelPath) -> bool {
        self.as_path() == other.as_path()
    }
}

impl PartialEq<str> for RelPath {
    fn eq(&self, other: &str) -> bool {
        self.0 == other
    }
}

impl PartialEq<&str> for RelPath {
    fn eq(&self, other: &&str) -> bool {
        self.0 == *other
    }
}

impl From<&RelPath> for RelPath {
    fn from(path: &RelPath) -> Self {
        path.clone()
    }
}

impl From<&str> for RelPath {
    fn from(path: &str) -> Self {
        Self::from(path.to_owned())
    }
}

impl From<String> for RelPath {
    fn from(path: String) -> Self {
        Self(RelPath::separators(path))
    }
}

impl<'de> Deserialize<'de> for RelPath {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        String::deserialize(deserializer).map(Self::from)
    }
}

impl From<&Path> for RelPath {
    fn from(path: &Path) -> Self {
        Self::new(path)
    }
}

impl From<PathBuf> for RelPath {
    fn from(path: PathBuf) -> Self {
        Self::new(&path)
    }
}

impl From<&PathBuf> for RelPath {
    fn from(path: &PathBuf) -> Self {
        Self::new(path)
    }
}

impl From<&RelPath> for PathBuf {
    fn from(path: &RelPath) -> Self {
        PathBuf::from(&path.0)
    }
}

impl fmt::Display for RelPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl fmt::Debug for RelPath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

/// Where a path starts: the part before its segments that says what they
/// are relative to.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PathHead {
    /// The root of the package the file belongs to: Rust's `crate::`.
    Package,
    /// The file's own module or directory: `self::`, `./`.
    Here,
    /// `n` levels up from the file's module or directory: `super::super::`,
    /// `../../`.
    Up(u8),
    /// The enclosing type, not a module: Rust's `Self::`. Never a module path.
    SelfType,
    /// A named package or an in-scope name, which only the layout can tell
    /// apart: `serde::`, `react`, `@scope/pkg`, a child module, an imported
    /// name. The name is the first segment.
    Named,
    /// The file system root: `/x/y`.
    Root,
}

/// How a language spells paths: the separator between segments and the
/// words it uses for a head. A grammar declares one; it parses import text
/// into [`ModulePath`]s and spells them back.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PathSyntax {
    /// `crate::a::b`, `self::a`, `super::super::a`, `Self::A`, `serde::X`.
    Scoped,
    /// `./a/b`, `../../a`, `/a/b`, `react/x`, `@scope/pkg`.
    Posix,
}

impl PathSyntax {
    pub const fn separator(self) -> &'static str {
        match self {
            Self::Scoped => "::",
            Self::Posix => "/",
        }
    }

    /// Read a path as written. Lossless for what a grammar hands over:
    /// `spell(parse(text)) == text` for every well-formed path.
    pub fn parse(self, text: &str) -> ModulePath {
        let mut segments: Vec<&str> = text.split(self.separator()).collect();
        let head = match self {
            Self::Scoped => match segments.first().copied() {
                Some("") if segments.len() > 1 => {
                    segments.remove(0);
                    PathHead::Root
                }
                Some("crate") => {
                    segments.remove(0);
                    PathHead::Package
                }
                Some("self") => {
                    segments.remove(0);
                    PathHead::Here
                }
                Some("Self") => {
                    segments.remove(0);
                    PathHead::SelfType
                }
                Some("super") => {
                    let ups = segments.iter().take_while(|s| **s == "super").count();
                    segments.drain(..ups);
                    PathHead::Up(ups as u8)
                }
                _ => PathHead::Named,
            },
            Self::Posix => match segments.first().copied() {
                Some("") if segments.len() > 1 => {
                    segments.remove(0);
                    PathHead::Root
                }
                Some(".") => {
                    segments.remove(0);
                    PathHead::Here
                }
                Some("..") => {
                    let ups = segments.iter().take_while(|s| **s == "..").count();
                    segments.drain(..ups);
                    PathHead::Up(ups as u8)
                }
                // `@scope/pkg` is one package name.
                Some(scope) if scope.starts_with('@') && segments.len() > 1 => {
                    let package = format!("{scope}/{}", segments[1]);
                    segments.drain(..2);
                    segments.insert(0, "");
                    let mut path = ModulePath {
                        syntax: self,
                        head: PathHead::Named,
                        segments: segments.iter().map(|s| Name::new(*s)).collect(),
                    };
                    path.segments[0] = Name::new(package);
                    return path;
                }
                _ => PathHead::Named,
            },
        };
        ModulePath {
            syntax: self,
            head,
            segments: segments
                .into_iter()
                .filter(|s| !s.is_empty())
                .map(Name::new)
                .collect(),
        }
    }

    /// The path as this syntax writes it.
    pub fn spell(self, path: &ModulePath) -> String {
        let sep = self.separator();
        let segments = path
            .segments
            .iter()
            .map(Name::as_str)
            .collect::<Vec<_>>()
            .join(sep);
        match (self, &path.head) {
            (Self::Scoped, PathHead::Package) => Self::joined("crate", sep, &segments),
            (Self::Scoped, PathHead::Here) => Self::joined("self", sep, &segments),
            (Self::Scoped, PathHead::SelfType) => Self::joined("Self", sep, &segments),
            (Self::Scoped, PathHead::Up(n)) => {
                let ups = vec!["super"; *n as usize].join(sep);
                Self::joined(&ups, sep, &segments)
            }
            (Self::Scoped, PathHead::Named) => segments,
            (Self::Scoped, PathHead::Root) => format!("::{segments}"),
            (Self::Posix, PathHead::Here) => Self::joined(".", sep, &segments),
            (Self::Posix, PathHead::Up(n)) => {
                let ups = vec![".."; *n as usize].join(sep);
                Self::joined(&ups, sep, &segments)
            }
            (Self::Posix, PathHead::Root) => format!("/{segments}"),
            (Self::Posix, PathHead::Package | PathHead::SelfType | PathHead::Named) => segments,
        }
    }

    fn joined(head: &str, sep: &str, segments: &str) -> String {
        if segments.is_empty() {
            head.to_owned()
        } else {
            format!("{head}{sep}{segments}")
        }
    }
}

/// A path as spelled in source: its head and its segments, with the syntax
/// that spells it. Serialises as the text, so the wire is what the user
/// wrote.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "schema", schemars(with = "String"))]
pub struct ModulePath {
    syntax: PathSyntax,
    pub head: PathHead,
    pub segments: Vec<Name>,
}

impl ModulePath {
    pub fn new(
        syntax: PathSyntax,
        head: PathHead,
        segments: impl IntoIterator<Item = impl Into<Name>>,
    ) -> Self {
        Self {
            syntax,
            head,
            segments: segments.into_iter().map(Into::into).collect(),
        }
    }

    pub fn syntax(&self) -> PathSyntax {
        self.syntax
    }

    pub fn first(&self) -> Option<&Name> {
        self.segments.first()
    }

    pub fn last(&self) -> Option<&Name> {
        self.segments.last()
    }

    pub fn is_empty(&self) -> bool {
        self.segments.is_empty()
    }

    /// The segments after `prefix`, when this path continues it: same head,
    /// its segments first.
    pub fn strip_prefix(&self, prefix: &ModulePath) -> Option<&[Name]> {
        (self.head == prefix.head)
            .then(|| self.segments.strip_prefix(prefix.segments.as_slice()))
            .flatten()
    }

    /// Whether this path continues `prefix` by at least one segment.
    pub fn continues(&self, prefix: &ModulePath) -> bool {
        self.strip_prefix(prefix)
            .is_some_and(|rest| !rest.is_empty())
    }

    /// The same path with `segments` in place of its own.
    pub fn with_segments(&self, segments: impl IntoIterator<Item = impl Into<Name>>) -> Self {
        Self::new(self.syntax, self.head.clone(), segments)
    }

    /// The path up to and including segment `index`: what a token in the
    /// middle of a path names.
    pub fn prefix(&self, index: usize) -> Self {
        self.with_segments(self.segments[..=index].iter().cloned())
    }

    /// Which segment is spelled at byte `offset` from the path's start, as
    /// its syntax writes it; `None` on a separator or the head.
    pub fn segment_at(&self, offset: usize) -> Option<usize> {
        let sep = self.syntax.separator().len();
        let head = self
            .syntax
            .spell(&self.with_segments(std::iter::empty::<Name>()));
        let mut at = match (&self.head, head.len()) {
            (PathHead::Root, n) => n,
            (_, 0) => 0,
            (_, n) => n + sep,
        };
        for (i, segment) in self.segments.iter().enumerate() {
            if (at..at + segment.len()).contains(&offset) {
                return Some(i);
            }
            at += segment.len() + sep;
        }
        None
    }

    /// `segments` alone, spelled in this path's syntax: the tail after a
    /// group's prefix.
    pub fn spell_segments(&self, segments: &[Name]) -> String {
        segments
            .iter()
            .map(Name::as_str)
            .collect::<Vec<_>>()
            .join(self.syntax.separator())
    }
}

impl fmt::Display for ModulePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.syntax.spell(self))
    }
}

impl fmt::Debug for ModulePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "ModulePath({:?})", self.to_string())
    }
}

impl Serialize for ModulePath {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&self.to_string())
    }
}

/// A path read back from text carries no syntax; `::` anywhere means
/// scoped, anything else Posix.
impl<'de> Deserialize<'de> for ModulePath {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let text = String::deserialize(deserializer)?;
        let syntax = if text.contains("::") {
            PathSyntax::Scoped
        } else {
            PathSyntax::Posix
        };
        Ok(syntax.parse(&text))
    }
}

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

    fn round_trip(syntax: PathSyntax, text: &str) -> ModulePath {
        let path = syntax.parse(text);
        assert_eq!(syntax.spell(&path), text, "{path:?}");
        path
    }

    #[test]
    fn scoped_absolute_paths_preserve_their_root_through_the_wire() {
        let path = PathSyntax::Scoped.parse("::dependency::LIMIT");
        assert_eq!(path.head, PathHead::Root);
        assert_eq!(PathSyntax::Scoped.spell(&path), "::dependency::LIMIT");
        let encoded = serde_json::to_string(&path).unwrap();
        let decoded: ModulePath = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded, path);
    }

    #[test]
    fn scoped_paths_parse_and_spell_losslessly() {
        assert_eq!(
            round_trip(PathSyntax::Scoped, "crate::a::b").head,
            PathHead::Package
        );
        assert_eq!(
            round_trip(PathSyntax::Scoped, "self::a").head,
            PathHead::Here
        );
        assert_eq!(
            round_trip(PathSyntax::Scoped, "super::super::x").head,
            PathHead::Up(2)
        );
        assert_eq!(round_trip(PathSyntax::Scoped, "super").segments.len(), 0);
        assert_eq!(
            round_trip(PathSyntax::Scoped, "Self::X").head,
            PathHead::SelfType
        );
        let named = round_trip(PathSyntax::Scoped, "serde::Serialize");
        assert_eq!(named.head, PathHead::Named);
        assert_eq!(named.first().map(Name::as_str), Some("serde"));
        assert_eq!(round_trip(PathSyntax::Scoped, "Foo").segments.len(), 1);
    }

    #[test]
    fn workspace_path_wire_round_trip_uses_the_constructor_policy() {
        let native = PathBuf::from("src").join("café").join("lib.rs");
        let native_text = native.to_str().unwrap();
        let wire = serde_json::to_string(native_text).unwrap();
        let decoded: RelPath = serde_json::from_str(&wire).unwrap();
        assert_eq!(decoded, RelPath::new(&native));
        assert_eq!(decoded.as_str(), "src/café/lib.rs");
        assert_eq!(decoded.as_path(), native.as_path());
        assert_eq!(
            serde_json::to_string(&decoded).unwrap(),
            serde_json::to_string("src/café/lib.rs").unwrap()
        );
        let again: RelPath =
            serde_json::from_str(&serde_json::to_string(&decoded).unwrap()).unwrap();
        assert_eq!(again, decoded);
    }

    #[cfg(windows)]
    #[test]
    fn workspace_paths_normalize_mixed_windows_separators_on_input() {
        let text = r"src\nested/café\lib.rs";
        let decoded: RelPath = serde_json::from_str(&serde_json::to_string(text).unwrap()).unwrap();
        assert_eq!(decoded, RelPath::from(text));
        assert_eq!(decoded.as_str(), "src/nested/café/lib.rs");
        assert_eq!(decoded.short(), "café/lib.rs");
    }

    #[cfg(unix)]
    #[test]
    fn workspace_paths_preserve_literal_backslashes_in_unix_filenames() {
        let native = Path::new(r"src/name\part.rs");
        let decoded: RelPath =
            serde_json::from_str(&serde_json::to_string(native.to_str().unwrap()).unwrap())
                .unwrap();
        assert_eq!(decoded, RelPath::new(native));
        assert_eq!(decoded.as_path(), native);
        assert_eq!(decoded.as_str(), r"src/name\part.rs");
        assert_eq!(decoded.short(), r"src/name\part.rs");
    }

    #[test]
    fn posix_paths_parse_and_spell_losslessly() {
        assert_eq!(round_trip(PathSyntax::Posix, "./x.js").head, PathHead::Here);
        assert_eq!(round_trip(PathSyntax::Posix, ".").segments.len(), 0);
        assert_eq!(
            round_trip(PathSyntax::Posix, "../../a/b").head,
            PathHead::Up(2)
        );
        assert_eq!(round_trip(PathSyntax::Posix, "../..").head, PathHead::Up(2));
        assert_eq!(round_trip(PathSyntax::Posix, "/etc/x").head, PathHead::Root);
        let scoped = round_trip(PathSyntax::Posix, "@scope/pkg/sub");
        assert_eq!(scoped.first().map(Name::as_str), Some("@scope/pkg"));
        assert_eq!(scoped.segments.len(), 2);
        assert_eq!(
            round_trip(PathSyntax::Posix, "react/jsx-runtime").head,
            PathHead::Named
        );
    }

    #[test]
    fn prefixes_compare_by_head_and_segments() {
        let group = PathSyntax::Scoped.parse("crate::util");
        let entry = PathSyntax::Scoped.parse("crate::util::parse::X");
        assert_eq!(
            entry.strip_prefix(&group).map(|r| entry.spell_segments(r)),
            Some("parse::X".to_owned())
        );
        assert!(!PathSyntax::Scoped.parse("crate::util").continues(&group));
        assert!(
            PathSyntax::Scoped
                .parse("self::util::x")
                .strip_prefix(&group)
                .is_none()
        );
    }

    #[test]
    fn segments_are_found_by_offset() {
        let path = PathSyntax::Scoped.parse("crate::commands::batch::BatchCmd");
        assert_eq!(path.segment_at(0), None, "the head");
        assert_eq!(path.segment_at(7), Some(0));
        assert_eq!(path.segment_at(17), Some(1));
        assert_eq!(path.segment_at(24), Some(2));
        assert_eq!(path.segment_at(15), None, "a separator");
        assert_eq!(path.prefix(1).to_string(), "crate::commands::batch");
        let bare = PathSyntax::Scoped.parse("a::b");
        assert_eq!(bare.segment_at(0), Some(0));
        assert_eq!(bare.segment_at(3), Some(1));
        let posix = PathSyntax::Posix.parse("../x/y.ts");
        assert_eq!(posix.segment_at(3), Some(0));
        assert_eq!(posix.segment_at(5), Some(1));
        assert_eq!(PathSyntax::Posix.parse("/etc/x").segment_at(1), Some(0));
    }

    #[test]
    fn serialises_as_text() {
        let path = PathSyntax::Scoped.parse("crate::a");
        assert_eq!(serde_json::to_string(&path).unwrap(), "\"crate::a\"");
        let back: ModulePath = serde_json::from_str("\"../x/y\"").unwrap();
        assert_eq!(back, PathSyntax::Posix.parse("../x/y"));
    }
}