typst-syntax 0.15.1

Parser and syntax tree for Typst.
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
//! Virtual, cross-platform reproducible path handling.

use std::error;
use std::ffi::OsStr;
use std::fmt::{self, Debug, Formatter};
use std::num::NonZeroU16;
use std::ops::Deref;
use std::path::{self, Path, PathBuf};
use std::sync::{LazyLock, RwLock};

use ecow::{EcoString, eco_format};
use rustc_hash::FxHashMap;

use crate::package::PackageSpec;

/// A path in a specific virtual file system root.
///
/// This identifies a location in a project or package.
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct RootedPath {
    root: VirtualRoot,
    vpath: VirtualPath,
}

impl RootedPath {
    /// Create a rooted path from a root and a virtual path within the root.
    pub fn new(root: VirtualRoot, vpath: VirtualPath) -> Self {
        Self { root, vpath }
    }

    /// Turns this path into a `FileId`.
    pub fn intern(self) -> FileId {
        FileId::new(self)
    }

    /// The root this path resides in.
    pub fn root(&self) -> &VirtualRoot {
        &self.root
    }

    /// The package the path resides in, if any.
    #[deprecated = "use `root` instead"]
    pub fn package(&self) -> Option<&PackageSpec> {
        match self.root() {
            VirtualRoot::Project => None,
            VirtualRoot::Package(package) => Some(package),
        }
    }

    /// The absolute and normalized path to the file _within_ the project or
    /// package.
    pub fn vpath(&self) -> &VirtualPath {
        &self.vpath
    }

    /// Maps the virtual path while retaining the root.
    pub fn map(&self, f: impl FnOnce(&VirtualPath) -> VirtualPath) -> Self {
        Self::new(self.root.clone(), f(&self.vpath))
    }
}

impl Debug for RootedPath {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let vpath = self.vpath();
        match self.root() {
            VirtualRoot::Project => Debug::fmt(vpath, f),
            VirtualRoot::Package(package) => write!(f, "{package:?}{vpath:?}"),
        }
    }
}

/// The root of a virtual file system.
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum VirtualRoot {
    /// The canonical root of the Typst project.
    ///
    /// This is what `TYPST_ROOT` defines.
    Project,
    /// A root in a package.
    Package(PackageSpec),
}

/// The global interner for rooted paths.
static INTERNER: LazyLock<RwLock<Interner>> = LazyLock::new(|| {
    RwLock::new(Interner { to_id: FxHashMap::default(), from_id: Vec::new() })
});

/// An interner for rooted paths.
struct Interner {
    to_id: FxHashMap<&'static RootedPath, FileId>,
    from_id: Vec<&'static RootedPath>,
}

/// An interned version of [`RootedPath`].
///
/// This type is globally interned and thus cheap to copy, compare, and hash.
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct FileId(NonZeroU16);

impl FileId {
    /// Create a new interned file specification.
    ///
    /// This is the same as [`RootedPath::intern`].
    #[track_caller]
    pub fn new(path: RootedPath) -> Self {
        // Try to find an existing entry that we can reuse.
        //
        // We could check with just a read lock, but if the pair is not yet
        // present, we would then need to recheck after acquiring a write lock,
        // which is probably not worth it.
        let mut interner = INTERNER.write().unwrap();
        if let Some(&id) = interner.to_id.get(&path) {
            return id;
        }

        // Create a new entry forever by leaking the pair. We can't leak more
        // than 2^16 pair (and typically will leak a lot less), so its not a
        // big deal.
        let num = u16::try_from(interner.from_id.len() + 1)
            .and_then(NonZeroU16::try_from)
            .expect("out of file ids");

        let id = FileId(num);
        let leaked = Box::leak(Box::new(path));
        interner.to_id.insert(leaked, id);
        interner.from_id.push(leaked);
        id
    }

    /// Create a new unique ("fake") file specification, which is not accessible
    /// by path.
    ///
    /// Caution: the ID returned by this method is the *only* identifier of the
    /// file, constructing a file ID with a path will *not* reuse the ID even if
    /// the path is the same. This method should only be used for generating
    /// "virtual" file ids such as content read from stdin.
    ///
    /// While the returned ID is not otherwise accessible, the provided root and
    /// path still matter as they define how paths within the identified file
    /// will resolve. For the example of content read from stdin, it will define
    /// how a relative path in the stdin content is resolved.
    #[track_caller]
    pub fn unique(path: RootedPath) -> Self {
        let mut interner = INTERNER.write().unwrap();
        let num = u16::try_from(interner.from_id.len() + 1)
            .and_then(NonZeroU16::try_from)
            .expect("out of file ids");

        let id = FileId(num);
        let leaked = Box::leak(Box::new(path));
        interner.from_id.push(leaked);
        id
    }

    /// Construct from a raw number.
    ///
    /// Should only be used with numbers retrieved via
    /// [`into_raw`](Self::into_raw). Misuse may results in panics, but no
    /// unsafety.
    pub const fn from_raw(v: NonZeroU16) -> Self {
        Self(v)
    }

    /// Extract the raw underlying number.
    pub const fn into_raw(self) -> NonZeroU16 {
        self.0
    }

    /// Get the static, interned rooted path.
    pub fn get(&self) -> &'static RootedPath {
        INTERNER.read().unwrap().from_id[usize::from(self.0.get() - 1)]
    }
}

impl Deref for FileId {
    type Target = RootedPath;

    fn deref(&self) -> &Self::Target {
        self.get()
    }
}

impl Debug for FileId {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.get().fmt(f)
    }
}

/// A path in a virtual file system.
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct VirtualPath(Segments);

impl VirtualPath {
    /// Creates a new virtual path.
    pub fn new(path: impl AsRef<str>) -> Result<Self, PathError> {
        let segments = Segments::normalize(components(path.as_ref()))?;
        Ok(Self(segments))
    }

    /// Creates a virtual path from a real path and a real root.
    ///
    /// Returns `None` if the file path is not contained in the root (i.e. if
    /// `root_path` is not a lexical prefix of `path`). No file system
    /// operations are performed.
    ///
    /// This is the single function that translates from a real path to a
    /// virtual path. Its counterpart is [`VirtualPath::realize`].
    pub fn virtualize(root_path: &Path, path: &Path) -> Result<Self, VirtualizeError> {
        let path = path.strip_prefix(root_path).map_err(|_| PathError::Escapes)?;
        let mut segments = Segments::new();
        for c in path.components() {
            let comp = match c {
                path::Component::RootDir => Component::Root,
                path::Component::CurDir => Component::Current,
                path::Component::ParentDir => Component::Parent,
                path::Component::Normal(s) => {
                    let string = s.to_str().ok_or(VirtualizeError::Utf8)?;
                    let segment = Segment::new(string)
                        .map_err(|s| VirtualizeError::Invalid(s.into()))?;
                    Component::Normal(segment)
                }
                path::Component::Prefix(_) => return Err(PathError::Escapes.into()),
            };
            segments.push_component(comp)?;
        }
        Ok(Self(segments))
    }

    /// Turns the virtual path into an actual file system path (where the
    /// project or package resides). You need to provide the appropriate `root`
    /// path, relative to which this path will be resolved.
    ///
    /// This is the single function that translates from a virtual path to a
    /// real path. Its counterpart is [`VirtualPath::virtualize`].
    ///
    /// This function has platform-specific output and returns an error if the
    /// path contains platform-specific syntax that could lexically escape the
    /// `root`. Currently, no file system operations are performed, though this
    /// may change in the future.
    ///
    /// This can be used in the implementations of `World::source` and
    /// `World::file`.
    pub fn realize(&self, root: &Path) -> Result<PathBuf, RealizeError> {
        let mut out = root.to_path_buf();
        for s in self.0.iter() {
            out.push(s.realize()?);
        }
        Ok(out)
    }

    /// Extracts the path with a leading slash.
    pub fn into_with_slash(self) -> EcoString {
        self.0.into_with_slash()
    }

    /// Returns the path with a leading slash.
    pub fn get_with_slash(&self) -> &str {
        self.0.get_with_slash()
    }

    /// Returns the path without a leading slash.
    pub fn get_without_slash(&self) -> &str {
        self.0.get_without_slash()
    }

    /// Whether this is the path `/`.
    pub fn is_root(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the file name portion of the path.
    pub fn file_name(&self) -> Option<&str> {
        self.0.last().map(Segment::get)
    }

    /// Returns the file name portion of the path without the extension.
    pub fn file_stem(&self) -> Option<&str> {
        let last = self.0.last()?;
        let (before, after) = last.split_dot();
        before.or(after)
    }

    /// Returns the file extension of the path.
    pub fn extension(&self) -> Option<&str> {
        let last = self.0.last()?;
        let (before, after) = last.split_dot();
        before.and(after)
    }

    /// Returns a modified path with an adjusted extension.
    ///
    /// # Panics
    /// Panics if the resulting path segment would be invalid, e.g. because the
    /// extension contains a forward or backslash.
    #[track_caller]
    pub fn with_extension(&self, ext: &str) -> Self {
        let Some(stem) = self.file_stem() else { return self.clone() };
        let buf = eco_format!("{stem}.{ext}");
        let segment = Segment::new(&buf).expect("extension is invalid");

        let mut segments = self.0.clone();
        segments.pop();
        segments.push(segment);
        Self(segments)
    }

    /// Returns the path with its final component removed.
    ///
    /// Returns `None` if the path is already at the root.
    pub fn parent(&self) -> Option<Self> {
        let mut segments = self.0.clone();
        if !segments.pop() {
            return None;
        }
        Some(Self(segments))
    }

    /// Joins the given `path` to `self`.
    pub fn join(&self, path: &str) -> Result<Self, PathError> {
        let combined = self
            .0
            .iter()
            .map(|c| Ok(Component::Normal(c)))
            .chain(components(path));
        let segments = Segments::normalize(combined)?;
        Ok(Self(segments))
    }

    /// Expresses this path as a relative path from the given base path.
    pub fn relative_from(&self, base: &Self) -> EcoString {
        // Adapted from rustc's `path_relative_from` function (MIT).
        // Copyright 2012-2015 The Rust Project Developers.
        // See NOTICE for full attribution.
        let mut ita = self.0.iter();
        let mut itb = base.0.iter();
        let mut buf: Vec<&str> = vec![];
        loop {
            match (ita.next(), itb.next()) {
                (None, None) => break,
                (Some(a), None) => {
                    buf.push(a.get());
                    buf.extend(ita.map(Segment::get));
                    break;
                }
                (None, Some(_)) => buf.push(".."),
                (Some(a), Some(b)) if buf.is_empty() && a == b => (),
                (Some(a), Some(_)) => {
                    buf.extend(std::iter::repeat_n("..", 1 + itb.count()));
                    buf.push(a.get());
                    buf.extend(ita.map(Segment::get));
                    break;
                }
            }
        }
        buf.join("/").into()
    }
}

impl VirtualPath {
    /// Create a virtual path from a real path and a real root.
    #[deprecated = "use `virtualize` with swapped arguments instead"]
    pub fn within_root(path: &Path, root: &Path) -> Option<Self> {
        Self::virtualize(root, path).ok()
    }

    /// Resolve the virtual path relative to an actual file system root
    /// (where the project or package resides).
    #[deprecated = "use `realize` instead"]
    pub fn resolve(&self, root: &Path) -> Option<PathBuf> {
        self.realize(root).ok()
    }

    /// Get the underlying path without a leading `/` or `\`.
    #[deprecated = "use `get_without_slash` instead"]
    pub fn as_rootless_path(&self) -> &Path {
        Path::new(self.get_without_slash())
    }

    /// Get the underlying path with a leading `/` or `\`.
    #[deprecated = "use `get_with_slash` instead"]
    pub fn as_rooted_path(&self) -> &Path {
        Path::new(self.get_with_slash())
    }
}

impl Debug for VirtualPath {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.get_with_slash().fmt(f)
    }
}

/// A component in a virtual path.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum Component<'a> {
    Root,
    Current,
    Parent,
    Normal(Segment<'a>),
}

// Special symbols in virtual paths.
const SEPARATOR: char = '/';
const CURRENT: &str = ".";
const PARENT: &str = "..";

/// Splits a user-supplied path into its constituent parts.
///
/// This only splits and recognizes special segments. It does not check the
/// validity of normal segments. This is done in [`Segments::push`].
fn components(path: &str) -> impl Iterator<Item = Result<Component<'_>, PathError>> {
    path.split(SEPARATOR).enumerate().map(|(i, s)| {
        match s {
            // A leading separator indicates an absolute path.
            "" if i == 0 && !path.is_empty() => Ok(Component::Root),
            // Consecutive separators have no effect.
            "" => Ok(Component::Current),
            CURRENT => Ok(Component::Current),
            PARENT => Ok(Component::Parent),
            other => match Segment::new(other) {
                Ok(segment) => Ok(Component::Normal(segment)),
                Err("\\") => Err(PathError::Backslash),
                Err(_) => unreachable!(),
            },
        }
    })
}

/// A segment in a normalized path.
///
/// A segments is never empty, `.`, or `..` and it never contains back- or
/// forward slashes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct Segment<'a>(&'a str);

impl<'a> Segment<'a> {
    fn new(segment: &'a str) -> Result<Self, &'a str> {
        // These invariants are important to avoid the path from escaping the
        // root after being realized, in particular the `..` part.
        if matches!(segment, "" | CURRENT | PARENT) {
            return Err(segment);
        }

        // Interior separators or backslashes are not allowed.
        if let Some(m) = segment.matches([SEPARATOR, '\\']).next() {
            return Err(m);
        }

        Ok(Self(segment))
    }

    fn new_unchecked(segment: &'a str) -> Self {
        debug_assert!(Self::new(segment).is_ok());
        Self(segment)
    }

    fn get(self) -> &'a str {
        self.0
    }

    fn split_dot(self) -> (Option<&'a str>, Option<&'a str>) {
        let mut iter = self.0.rsplitn(2, '.');
        let after = iter.next();
        let before = iter.next();
        if before == Some("") { (Some(self.0), None) } else { (before, after) }
    }

    /// Ensures that this segment can be joined to an existing platform-specific
    /// path without lexically escaping the root formed by that path.
    ///
    /// Sanity-checked against <https://pkg.go.dev/path/filepath#IsLocal>.
    fn realize(self) -> Result<&'a OsStr, RealizeError> {
        // We ensure that this segment corresponds to exactly one platform-level
        // path component. Drive letters on Windows are rejected since they are
        // `Prefix` components.
        let mut iter = Path::new(self.get()).components();
        match (iter.next(), iter.next()) {
            // Exactly one normal component.
            (Some(path::Component::Normal(s)), None) => {
                // Forbid access to reserved files on Windows as they are
                // conceptually not part of any root.
                #[cfg(windows)]
                if is_windows_reserved(self.get()) {
                    return Err(RealizeError::Invalid(self.get().into()));
                }
                Ok(s)
            }
            // No or multiple components, starting with normal.
            (None | Some(path::Component::Normal(_)), _) => {
                Err(RealizeError::Invalid(self.get().into()))
            }
            // Non-normal component.
            (Some(other), _) => {
                Err(RealizeError::Invalid(other.as_os_str().to_string_lossy().into()))
            }
        }
    }
}

/// Whether the give file name is reserved on Windows.
///
/// Follows <https://cs.opensource.google/go/go/+/refs/tags/go1.26.4:src/internal/filepathlite/path_windows.go;l=98>.
/// See also: <https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file>
#[cfg(windows)]
fn is_windows_reserved(name: &str) -> bool {
    #[rustfmt::skip]
    fn is_reserved_base(basename: &str) -> bool {
        matches!(
            basename,
            "CON" | "PRN" | "AUX" | "NUL"
                | "COM0" | "COM1" | "COM2" | "COM3" | "COM4" | "COM5" | "COM6"
                | "COM7" | "COM8" | "COM9" | "COM¹" | "COM²" | "COM³"
                | "LPT0" | "LPT1" | "LPT2" | "LPT3" | "LPT4" | "LPT5" | "LPT6"
                | "LPT7" | "LPT8" | "LPT9" | "LPT¹" | "LPT²" | "LPT³"
                | "CONIN$" | "CONOUT$"
        )
    }

    // Some Windows versions also allow arbitrary characters after a dot or
    // colon in device names (and trailing spaces before that suffix).
    let base = name.split(['.', ':']).next().unwrap_or(name).trim_end_matches(' ');
    base.len() <= 7 && is_reserved_base(&EcoString::from(base).to_ascii_uppercase())
}

/// Stores a sequence of path segments as a string.
///
/// The underlying string always represents a normalized absolute path and is
/// guaranteed to start with a slash. Segments are never empty, `.`, or `..` and
/// they never contain back- or forward slashes.
#[derive(Clone, Eq, PartialEq, Hash)]
struct Segments(EcoString);

impl Segments {
    fn new() -> Self {
        Self(EcoString::from(SEPARATOR))
    }

    fn normalize<'a>(
        comps: impl IntoIterator<Item = Result<Component<'a>, PathError>>,
    ) -> Result<Segments, PathError> {
        let mut out = Segments::new();
        for component in comps {
            out.push_component(component?)?;
        }
        Ok(out)
    }

    fn is_empty(&self) -> bool {
        self.0.len() == 1
    }

    fn into_with_slash(self) -> EcoString {
        self.0
    }

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

    fn get_without_slash(&self) -> &str {
        self.0.strip_prefix(SEPARATOR).expect("path to start with slash")
    }

    fn clear(&mut self) {
        self.0.truncate(1);
    }

    fn push_component(&mut self, component: Component) -> Result<(), PathError> {
        match component {
            // Root component resets the path.
            Component::Root => self.clear(),
            // Current component has no effect.
            Component::Current => {}
            // Parent component removes the last segment. If there is no
            // segment, this indicates that the path would escape the root.
            // In this case, we return an error.
            Component::Parent => {
                if !self.pop() {
                    return Err(PathError::Escapes);
                }
            }
            Component::Normal(segment) => self.push(segment),
        }
        Ok(())
    }

    fn push<'a>(&mut self, segment: Segment<'a>) {
        if !self.is_empty() {
            self.0.push(SEPARATOR);
        }
        self.0.push_str(segment.0);
    }

    fn pop(&mut self) -> bool {
        if self.is_empty() {
            return false;
        }
        let i = self.0.rfind(SEPARATOR).expect("to contain a slash");
        self.0.truncate(std::cmp::max(1, i));
        true
    }

    fn last(&self) -> Option<Segment<'_>> {
        self.iter().next_back()
    }

    fn iter(&self) -> impl DoubleEndedIterator<Item = Segment<'_>> {
        let mut iter = self.0[1..].split(SEPARATOR);
        if self.is_empty() {
            iter.next();
        }
        iter.map(Segment::new_unchecked)
    }
}

/// An error that can occur on construction or modification of a
/// [`VirtualPath`].
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum PathError {
    /// The constructed or modified path would escape the root. This would
    /// for instance, when trying to join `..` to the path `/`.
    ///
    /// Note that a path might still escape through symlinks.
    Escapes,
    /// The path contains a backslash. This is not allowed as it leads to
    /// cross-platform compatibility hazards (since Windows uses backslashes as
    /// a path separator).
    Backslash,
}

impl fmt::Display for PathError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Escapes => write!(f, "path escapes project root"),
            Self::Backslash => write!(f, "path contains backslash"),
        }
    }
}

impl error::Error for PathError {}

/// An error that can occur in [`VirtualPath::virtualize`].
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum VirtualizeError {
    /// A normal path error.
    Path(PathError),
    /// A path component contained an invalid string. This should almost never
    /// occur under normal circumstances, but it could happen if some OS allows
    /// forward slashes or dots in path components.
    Invalid(EcoString),
    /// The file path contains non-UTF-8 encodable bytes.
    Utf8,
}

impl fmt::Display for VirtualizeError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Path(inner) => fmt::Display::fmt(inner, f),
            Self::Invalid(component) => {
                write!(f, "path contains invalid component `{component:?}`")
            }
            Self::Utf8 => write!(f, "path contains non-UTF-8 bytes"),
        }
    }
}

// NOTE: Because we opt to inline the formatting of the PathError we cannot
// also return it as the error source, else it will be displayed twice in error
// backtraces.
impl error::Error for VirtualizeError {}

impl From<PathError> for VirtualizeError {
    fn from(err: PathError) -> Self {
        Self::Path(err)
    }
}

/// An error that can occur in [`VirtualPath::realize`].
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum RealizeError {
    /// A virtual path component was of a form that would be specially
    /// interpreted on the current platform as opposed to designating just a
    /// file or directory.
    ///
    /// For example, this occurs when a path segments contains a drive letter
    /// prefix on Windows (i.e. the virtual path `/C:/System32/..`) as Windows
    /// interprets interior drive letters as resetting the path to the cwd (if
    /// the cwd is on the current drive) or resetting the path to root (if the
    /// cwd is on another drive).
    Invalid(EcoString),
}

impl fmt::Display for RealizeError {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid(component) => {
                write!(f, "path contains invalid component `{component:?}`")
            }
        }
    }
}

impl error::Error for RealizeError {}

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

    #[track_caller]
    fn path(p: &str) -> VirtualPath {
        VirtualPath::new(p).unwrap()
    }

    #[test]
    fn test_new() {
        #[track_caller]
        fn test(path: &str, expected: Result<&str, PathError>) {
            let path = VirtualPath::new(path);
            assert_eq!(
                path.as_ref().map(|s| s.get_with_slash()).map_err(Clone::clone),
                expected
            );
        }

        test("", Ok("/"));
        test("a/./file.txt", Ok("/a/file.txt"));
        test("file.txt", Ok("/file.txt"));
        test("/file.txt", Ok("/file.txt"));
        test("hello/world", Ok("/hello/world"));
        test("hello/world/", Ok("/hello/world"));
        test("a///b", Ok("/a/b"));
        test("/a///b", Ok("/a/b"));
        test("./world.txt", Ok("/world.txt"));
        test("./world.txt/", Ok("/world.txt"));
        test("hello/.././/wor/ld.typ.extra", Ok("/wor/ld.typ.extra"));
        test("hello/.../world", Ok("/hello/.../world"));
        test("\u{200b}..", Ok("/\u{200b}.."));
        test("..", Err(PathError::Escapes));
        test("../world.txt", Err(PathError::Escapes));
        test("a\\world.txt", Err(PathError::Backslash));
    }

    #[test]
    #[cfg(unix)]
    fn test_virtualize_unix() {
        test_virtualize("/", "/main.typ", Ok("/main.typ"));
        test_virtualize("//a/b", "/a//b///c//d", Ok("/c/d"));
        test_virtualize(
            "/home/typst/desktop/",
            "/home/typst/desktop/src/main.typ",
            Ok("/src/main.typ"),
        );
        test_virtualize(
            "/home/typst/desktop/",
            "/home/typst/main.typ",
            Err(PathError::Escapes.into()),
        );
    }

    #[test]
    #[cfg(windows)]
    fn test_virtualize_windows() {
        test_virtualize(
            "C:\\Users\\typst\\Desktop",
            "C:\\Users\\typst\\Desktop\\src\\main.typ",
            Ok("/src/main.typ"),
        );
        test_virtualize(
            "C:\\Users\\typst\\Desktop",
            "C:\\Users\\typst\\main.typ",
            Err(PathError::Escapes.into()),
        );
    }

    #[track_caller]
    fn test_virtualize(
        root_path: impl AsRef<Path>,
        path: impl AsRef<Path>,
        expected: Result<&str, VirtualizeError>,
    ) {
        assert_eq!(
            VirtualPath::virtualize(root_path.as_ref(), path.as_ref(),)
                .as_ref()
                .map(|v| v.get_with_slash())
                .map_err(Clone::clone),
            expected,
        );
    }

    #[test]
    fn test_realize() {
        let p = path("src/text/main.typ");
        assert_eq!(
            p.realize(Path::new("/home/users/typst")),
            Ok(PathBuf::from("/home/users/typst/src/text/main.typ")),
        );
    }

    #[test]
    #[cfg(windows)]
    fn test_realize_windows() {
        let root = Path::new("C:\\Users\\typst");
        let invalid = |s: &str| RealizeError::Invalid(s.into());
        assert_eq!(path("C:System32").realize(root), Err(invalid("C:")));
        assert_eq!(path("D:Stuff").realize(root), Err(invalid("D:")));
        assert_eq!(path("C:/System32").realize(root), Err(invalid("C:")));
        assert_eq!(path("Foo/E:System").realize(root), Err(invalid("E:")));
        assert_eq!(path("Foo/E:/System").realize(root), Err(invalid("E:")));
        assert_eq!(path("F:").realize(root), Err(invalid("F:")));
        assert_eq!(path("CON").realize(root), Err(invalid("CON")));
        assert_eq!(path("A/CON .txt").realize(root), Err(invalid("CON .txt")));
        assert_eq!(path("A/CON:foo/bar").realize(root), Err(invalid("CON:foo")));
        assert_eq!(path("a/LPT\u{00b2} .baz/b").realize(root), Err(invalid("LPT² .baz")));
    }

    #[test]
    fn test_file_ops() {
        let p1 = path("src/text/file.typ");
        assert_eq!(p1.file_name(), Some("file.typ"));
        assert_eq!(p1.file_stem(), Some("file"));
        assert_eq!(p1.extension(), Some("typ"));
        assert_eq!(p1.with_extension("txt"), path("src/text/file.txt"));
        assert_eq!(p1.parent(), Some(path("src/text")));

        let p2 = path("src");
        assert_eq!(p2.file_name(), Some("src"));
        assert_eq!(p2.file_stem(), Some("src"));
        assert_eq!(p2.extension(), None);
        assert_eq!(p2.with_extension("txt"), path("src.txt"));
        assert_eq!(p2.parent(), Some(path("/")));

        let p3 = path("");
        assert_eq!(p3.file_name(), None);
        assert_eq!(p3.file_stem(), None);
        assert_eq!(p3.extension(), None);
        assert_eq!(p3.with_extension("txt"), p3);
        assert_eq!(p3.parent(), None);
    }

    #[test]
    fn test_join() {
        let p1 = path("src");
        assert_eq!(p1.join("a\\b"), Err(PathError::Backslash));
        let p2 = p1.join("text").unwrap();
        assert_eq!(p2.get_with_slash(), "/src/text");
        let p3 = p2.join("..").unwrap();
        assert_eq!(p1, p3);
        assert_eq!(p3.get_with_slash(), "/src");
        let p4 = p3.join("..").unwrap();
        assert_eq!(p4.get_with_slash(), "/");
        assert_eq!(p4.join(".."), Err(PathError::Escapes));
    }

    #[test]
    fn test_relative_from() {
        let p1 = path("src/text/main.typ");
        assert_eq!(p1.relative_from(&path("/src/text")), "main.typ");
        assert_eq!(p1.relative_from(&path("/src/data")), "../text/main.typ");
        assert_eq!(p1.relative_from(&path("src/")), "text/main.typ");
        assert_eq!(p1.relative_from(&path("/")), "src/text/main.typ");

        let p2 = path("src");
        assert_eq!(p2.relative_from(&path("src")), "");
        assert_eq!(p2.relative_from(&path("src/data")), "..");
    }

    #[test]
    fn test_segments() {
        let mut s = Segments::new();
        assert_eq!(s.get_with_slash(), "/");
        assert_eq!(s.get_without_slash(), "");
        s.push(Segment::new("to").unwrap());
        assert_eq!(s.get_with_slash(), "/to");
        s.push(Segment::new("hi.txt").unwrap());
        assert_eq!(s.get_with_slash(), "/to/hi.txt");
        assert_eq!(s.get_without_slash(), "to/hi.txt");
        assert_eq!(s.last().map(Segment::get), Some("hi.txt"));
        assert!(s.pop());
        assert_eq!(s.get_with_slash(), "/to");
        assert!(s.pop());
        assert_eq!(s.get_with_slash(), "/");
        assert!(!s.pop());
        assert_eq!(s.get_with_slash(), "/");
        assert_eq!(s.last(), None);
    }

    #[test]
    fn test_segment() {
        assert_eq!(Segment::new("\\b"), Err("\\"));
        assert_eq!(Segment::new("a/b"), Err("/"));
        assert_eq!(Segment::new(""), Err(""));
        assert_eq!(Segment::new("."), Err("."));
        assert_eq!(Segment::new(".."), Err(".."));
    }
}