dream-path 0.1.3

Byte-first normalized virtual resource paths for DreamWeave/OpenMW-style asset lookup
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
//! Byte-first normalized virtual resource paths.
//!
//! `dream-path` owns the path normalization shared by archive readers, VFS,
//! resource loading, rendering-side resource lookup, and tooling. The rules are
//! intentionally the boring OpenMW-style virtual path rules:
//!
//! - `\` becomes `/`
//! - ASCII uppercase letters become lowercase
//! - repeated separators collapse
//! - leading separators are discarded
//! - arbitrary non-UTF-8 bytes are preserved
//!
//! The rules are byte-literal: `///` normalizes to an empty byte string, while a
//! non-leading trailing separator is kept (`foo/` stays `foo/`).
//!
//! It does not decode legacy filename encodings, perform Unicode
//! normalization, case-fold non-ASCII text, interpret host filesystem paths, or
//! compute archive-format hashes. Those are separate jobs. Mixing them into the
//! path type would be a small architectural crime, so naturally we avoid doing
//! that.
#![cfg_attr(
    feature = "lua",
    doc = r"
## Lua API

Enable the `lua` feature to embed the same byte-preserving normalization API
into an existing [`mlua::Lua`] state. [`lua::create_module`] builds the API table
without registering a global, while [`lua::register_module`] installs it as the
default `dream_path` global.

The `lua` feature does not choose a Lua backend. Engine/application crates own
that decision and should enable exactly one shared [`mlua`] runtime for the final
dependency graph. `DreamWeave` recommends `LuaJIT` in 5.2 compatibility mode and
does not currently test these bindings against other Lua runtimes. If a host
chooses another backend, it owns that compatibility burden. For standalone
documentation and local smoke tests, the `standalone-lua` feature enables `lua`
plus `mlua`'s `luajit52` and `vendored` features.

The Lua API treats Lua strings as raw path bytes, preserving invalid UTF-8 and
embedded NUL bytes. It is embed-only: this crate does not provide a `cdylib` Lua
module loader, and hosts that already own a different Lua runtime should bind the
Rust byte API themselves.
"
)]
#![cfg_attr(
    not(feature = "lua"),
    doc = r#"
## Lua API

Lua bindings are available behind the `lua` feature. Build documentation with
`--features standalone-lua` to include links to the embedded Lua API.

The `lua` feature does not choose a Lua backend. Engine/application crates should
enable exactly one shared `mlua` runtime for the final dependency graph.
`DreamWeave` recommends `LuaJIT` in 5.2 compatibility mode and does not currently
test these bindings against other Lua runtimes.
"#
)]

use std::{borrow::Borrow, str::Utf8Error};

use bstr::{BStr, BString, ByteSlice as _};

pub use bstr;

#[cfg(feature = "lua")]
pub mod lua;

/// A byte-first normalized virtual resource path.
///
/// [`NormalizedPath::new`] and the input [`From`] impls apply [`normalize_path`].
/// The normalized-byte adoption constructors require the caller to provide bytes
/// that are already normalized. This type is intended for repeated lookups where
/// normalizing the query every time would allocate and burn cycles for no useful
/// reason.
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct NormalizedPath(BString);

impl NormalizedPath {
    /// Normalize `path` into an owned virtual resource path.
    #[must_use]
    pub fn new(path: impl AsRef<[u8]>) -> Self {
        Self(BString::from(normalize_path(path.as_ref())))
    }

    /// Build from bytes that are already normalized.
    ///
    /// Returns the original bytes on rejection so callers can log, repair, or
    /// normalize them without cloning first. Use this when avoiding a second
    /// normalization pass matters and the caller can handle rejection.
    ///
    /// # Errors
    ///
    /// Returns the original `path` when it does not satisfy
    /// [`is_normalized_path`].
    pub fn try_from_normalized_bytes(path: Vec<u8>) -> Result<Self, Vec<u8>> {
        if is_normalized_path(&path) {
            Ok(Self(BString::from(path)))
        } else {
            Err(path)
        }
    }

    /// Build from bytes that are already normalized without checking them.
    ///
    /// `path` should satisfy [`is_normalized_path`]. Passing non-normalized bytes
    /// breaks the logical invariant that every [`NormalizedPath`] contains
    /// normalized virtual path spelling. That can produce cache misses and
    /// duplicate keys. It is not memory-unsafe; it is just wrong, which is quite
    /// bad enough.
    #[must_use]
    pub fn from_normalized_bytes_unchecked(path: Vec<u8>) -> Self {
        debug_assert!(is_normalized_path(&path));
        Self(BString::from(path))
    }

    /// Return this path as a [`BStr`].
    #[must_use]
    pub fn as_bstr(&self) -> &BStr {
        self.0.as_bstr()
    }

    /// Return this path as raw bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Return this path as UTF-8 when the normalized bytes are valid UTF-8.
    ///
    /// This is a convenience for string-oriented callers. It is not a promise
    /// that virtual resource paths are Unicode, display-safe, C-string-safe, or
    /// host filesystem paths. Valid UTF-8 paths may still contain NUL bytes.
    ///
    /// # Errors
    ///
    /// Returns [`Utf8Error`] if the path contains invalid UTF-8 bytes.
    pub fn to_str(&self) -> Result<&str, Utf8Error> {
        std::str::from_utf8(self.as_bytes())
    }

    /// Return the final non-empty component of this virtual path.
    ///
    /// A trailing separator is ignored for component extraction, so `foo/bar/`
    /// has file name `bar`.
    #[must_use]
    pub fn file_name(&self) -> Option<&BStr> {
        file_name_normalized(self.as_bytes())
    }

    /// Return the parent portion of this virtual path.
    ///
    /// This is a byte-level virtual path operation. It does not interpret `.`,
    /// `..`, drive prefixes, roots, or host filesystem rules.
    #[must_use]
    pub fn parent(&self) -> Option<&BStr> {
        parent_normalized(self.as_bytes())
    }

    /// Return the extension of the final component, without the dot.
    ///
    /// Dotfiles such as `.hidden`, names ending in `.`, and paths ending in `/`
    /// have no extension.
    #[must_use]
    pub fn extension(&self) -> Option<&BStr> {
        extension_normalized(self.as_bytes())
    }

    /// Return true if the normalized path is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Return the normalized path length in bytes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl AsRef<[u8]> for NormalizedPath {
    fn as_ref(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl AsRef<BStr> for NormalizedPath {
    fn as_ref(&self) -> &BStr {
        self.as_bstr()
    }
}

impl Borrow<[u8]> for NormalizedPath {
    fn borrow(&self) -> &[u8] {
        self.as_bytes()
    }
}

impl Borrow<BStr> for NormalizedPath {
    fn borrow(&self) -> &BStr {
        self.as_bstr()
    }
}

impl From<&[u8]> for NormalizedPath {
    fn from(path: &[u8]) -> Self {
        Self::new(path)
    }
}

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

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

impl From<Vec<u8>> for NormalizedPath {
    fn from(path: Vec<u8>) -> Self {
        Self::new(path)
    }
}

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

impl From<BString> for NormalizedPath {
    fn from(path: BString) -> Self {
        Self::new(path)
    }
}

impl From<NormalizedPath> for BString {
    fn from(path: NormalizedPath) -> Self {
        path.0
    }
}

impl From<NormalizedPath> for Vec<u8> {
    fn from(path: NormalizedPath) -> Self {
        path.0.into()
    }
}

/// Return true if `path` already matches this crate's normalized spelling.
///
/// This checks byte spelling only. It does not mean that `path` is a valid
/// file-like resource, safe host path, URI, display string, or archive path.
/// Empty paths, trailing separators, NUL bytes, invalid UTF-8, dot segments,
/// and already-mangled host/URI-looking strings such as `c:/foo` or
/// `http:/foo/bar` may all be normalized according to this predicate.
#[must_use]
pub fn is_normalized_path(path: &[u8]) -> bool {
    let mut previous_was_separator = true;
    for &byte in path {
        match byte {
            b'\\' | b'A'..=b'Z' => return false,
            b'/' if previous_was_separator => return false,
            b'/' => previous_was_separator = true,
            _ => previous_was_separator = false,
        }
    }
    true
}

/// Return the final non-empty component of an already-normalized virtual path.
///
/// A trailing separator is ignored for component extraction, so `foo/bar/` has
/// file name `bar`. The input is assumed to satisfy [`is_normalized_path`]; this
/// function does not normalize, validate resource suitability, or interpret host
/// filesystem syntax.
#[must_use]
pub fn file_name_normalized(path: &[u8]) -> Option<&BStr> {
    let bytes = without_trailing_separator(path);
    if bytes.is_empty() {
        return None;
    }
    let start = bytes
        .iter()
        .rposition(|byte| *byte == b'/')
        .map_or(0, |pos| pos + 1);
    Some(bytes[start..].as_bstr())
}

/// Return the parent portion of an already-normalized virtual path.
///
/// The input is assumed to satisfy [`is_normalized_path`]. This is a byte-level
/// virtual path operation; it does not resolve `.`, `..`, roots, drive prefixes,
/// URI schemes, or host filesystem rules.
#[must_use]
pub fn parent_normalized(path: &[u8]) -> Option<&BStr> {
    let bytes = without_trailing_separator(path);
    let end = bytes.iter().rposition(|byte| *byte == b'/')?;
    Some(bytes[..end].as_bstr())
}

/// Return the extension of the final component of an already-normalized virtual
/// path, without the dot.
///
/// Dotfiles such as `.hidden`, names ending in `.`, and paths ending in `/` have
/// no extension. The input is assumed to satisfy [`is_normalized_path`].
#[must_use]
pub fn extension_normalized(path: &[u8]) -> Option<&BStr> {
    if path.ends_with(b"/") {
        return None;
    }
    let file_name = file_name_normalized(path)?.as_bytes();
    let dot = file_name.iter().rposition(|byte| *byte == b'.')?;
    if dot == 0 || dot + 1 == file_name.len() {
        return None;
    }
    Some(file_name[dot + 1..].as_bstr())
}

/// Normalize a virtual resource path into owned bytes.
#[must_use]
pub fn normalize_path(path: impl AsRef<[u8]>) -> Vec<u8> {
    let path = path.as_ref();
    let mut out = Vec::with_capacity(path.len());
    normalize_path_into(&mut out, path);
    out
}

/// Normalize an owned virtual resource path, reusing its allocation.
///
/// This is a convenience for callers that already own a byte buffer and do not
/// need to preserve the original spelling. It has the same normalization rules
/// as [`normalize_path`].
#[must_use]
pub fn normalize_path_owned(mut path: Vec<u8>) -> Vec<u8> {
    normalize_path_in_place(&mut path);
    path
}

/// Normalize an owned virtual resource path in place.
///
/// The buffer is rewritten using the same rules as [`normalize_path`]. Its
/// allocation is reused; its length may shrink when leading or repeated
/// separators are removed.
pub fn normalize_path_in_place(path: &mut Vec<u8>) {
    let mut write = 0;
    let mut previous_was_separator = true;
    for read in 0..path.len() {
        let byte = match path[read] {
            b'\\' => b'/',
            b'A'..=b'Z' => path[read] + 32,
            byte => byte,
        };
        if byte == b'/' && previous_was_separator {
            continue;
        }
        path[write] = byte;
        write += 1;
        previous_was_separator = byte == b'/';
    }
    path.truncate(write);
}

/// Normalize a virtual resource path into an existing buffer.
///
/// `out` is cleared before writing. Its previous allocation is reused when
/// possible. This crate does not enforce a maximum path length; callers handling
/// untrusted archive, tool, or Lua input should enforce their own byte budget.
/// A scratch buffer can retain a large allocation after a pathological input, so
/// discard or shrink it at the caller boundary if that matters.
pub fn normalize_path_into(out: &mut Vec<u8>, path: &[u8]) {
    out.clear();
    out.reserve(path.len());
    for byte in path.iter().copied() {
        let byte = match byte {
            b'\\' => b'/',
            b'A'..=b'Z' => byte + 32,
            _ => byte,
        };
        if byte == b'/' && (out.is_empty() || out.last() == Some(&b'/')) {
            continue;
        }
        out.push(byte);
    }
}

fn without_trailing_separator(bytes: &[u8]) -> &[u8] {
    bytes.strip_suffix(b"/").unwrap_or(bytes)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use bstr::{BStr, BString};

    use super::{
        NormalizedPath, extension_normalized, file_name_normalized, is_normalized_path,
        normalize_path, normalize_path_in_place, normalize_path_into, normalize_path_owned,
        parent_normalized,
    };

    #[test]
    fn leaves_empty_path_empty() {
        assert_eq!(normalize_path(b""), b"");
    }

    #[test]
    fn removes_leading_separators() {
        assert_eq!(normalize_path(b"/foo"), b"foo");
        assert_eq!(normalize_path(b"///foo//bar"), b"foo/bar");
    }

    #[test]
    fn all_separators_normalize_to_empty() {
        assert_eq!(normalize_path(b"/"), b"");
        assert_eq!(normalize_path(br"\\\/"), b"");
    }

    #[test]
    fn keeps_non_leading_trailing_separator() {
        assert_eq!(normalize_path(b"foo/"), b"foo/");
        assert_eq!(normalize_path(br"foo\\"), b"foo/");
    }

    #[test]
    fn folds_backslashes_and_ascii_case() {
        assert_eq!(normalize_path(br"FOO\BaR"), b"foo/bar");
    }

    #[test]
    fn collapses_repeated_separators_after_backslash_folding() {
        assert_eq!(normalize_path(br"foo\\//bar"), b"foo/bar");
    }

    #[test]
    fn preserves_non_ascii_bytes() {
        assert_eq!(normalize_path("Café/Ä".as_bytes()), "café/Ä".as_bytes());
    }

    #[test]
    fn only_ascii_uppercase_is_folded() {
        assert_eq!(normalize_path(b"ABC[\\]^_`XYZ"), b"abc[/]^_`xyz");
    }

    #[test]
    fn preserves_invalid_utf8_bytes() {
        assert_eq!(normalize_path(b"DIR/\xff/FILE"), b"dir/\xff/file");
    }

    #[test]
    fn preserves_nul_bytes() {
        assert_eq!(normalize_path(b"FOO\0BAR"), b"foo\0bar");
        assert_eq!(normalize_path(b"DIR/\0/FILE"), b"dir/\0/file");
    }

    #[test]
    fn does_not_resolve_dot_segments() {
        assert_eq!(normalize_path(b"A/./B"), b"a/./b");
        assert_eq!(normalize_path(b"A/../B"), b"a/../b");
        assert_eq!(
            NormalizedPath::new(b"Foo/../BAR").parent(),
            Some(BStr::new(b"foo/.."))
        );
    }

    #[test]
    fn does_not_preserve_uri_or_host_path_syntax() {
        assert_eq!(normalize_path(b"HTTP://Foo/Bar"), b"http:/foo/bar");
        assert_eq!(normalize_path(br"C:\Foo"), b"c:/foo");
        assert_eq!(
            normalize_path(br"\\Server\Share\File"),
            b"server/share/file"
        );
    }

    #[test]
    fn trailing_separator_remains_part_of_key() {
        let file = NormalizedPath::new("textures/foo.dds");
        let directory_like = NormalizedPath::new("textures/foo.dds/");

        assert_ne!(file, directory_like);
        assert_eq!(directory_like.as_bytes(), b"textures/foo.dds/");
    }

    #[test]
    fn normalization_is_idempotent() {
        let once = normalize_path(br"//Foo\\BAR///baz");
        let twice = normalize_path(&once);
        assert_eq!(once, twice);
    }

    #[test]
    fn normalization_invariants_hold_for_byte_corpus() {
        let mut cases: Vec<Vec<u8>> = (0u8..=u8::MAX).map(|byte| vec![byte]).collect();
        cases.extend([
            b"".to_vec(),
            br"//Foo\\BAR///baz".to_vec(),
            b"HTTP://Foo/Bar".to_vec(),
            br"C:\Foo".to_vec(),
            br"\\Server\Share\File".to_vec(),
            b"A/./B".to_vec(),
            b"A/../B".to_vec(),
            b"DIR/\0/FILE".to_vec(),
            b"DIR/\xff/FILE".to_vec(),
            br"/A//B\\C/".to_vec(),
        ]);

        for case in cases {
            let normalized = normalize_path(&case);
            assert!(
                is_normalized_path(&normalized),
                "normalized output failed predicate: {case:?}"
            );
            assert_eq!(
                normalize_path(&normalized),
                normalized,
                "normalization was not idempotent: {case:?}"
            );
            assert!(
                !normalized.contains(&b'\\'),
                "backslash survived normalization: {case:?}"
            );
            assert!(
                !normalized.iter().any(u8::is_ascii_uppercase),
                "uppercase ASCII survived normalization: {case:?}"
            );
            assert!(
                !normalized.starts_with(b"/"),
                "leading slash survived normalization: {case:?}"
            );
            assert!(
                !normalized.windows(2).any(|window| window == b"//"),
                "repeated slash survived normalization: {case:?}"
            );
        }
    }

    #[test]
    fn detects_already_normalized_paths() {
        assert!(is_normalized_path(b""));
        assert!(is_normalized_path(b"textures/foo.dds"));
        assert!(is_normalized_path(b"textures/foo/"));
        assert!(is_normalized_path(b"textures/\xff/file"));
        assert!(is_normalized_path(b"foo\0bar"));
        assert!(is_normalized_path(b"foo/../bar"));
        assert!(is_normalized_path(b"c:/foo"));

        assert!(!is_normalized_path(b"/textures/foo.dds"));
        assert!(!is_normalized_path(b"textures//foo.dds"));
        assert!(!is_normalized_path(br"textures\foo.dds"));
        assert!(!is_normalized_path(b"textures/FOO.dds"));
    }

    #[test]
    fn normalize_into_reuses_and_clears_output() {
        let mut out = b"stale".to_vec();
        let capacity = out.capacity();
        normalize_path_into(&mut out, br"/Foo\Bar");
        assert_eq!(out, b"foo/bar");
        assert!(out.capacity() >= capacity);
    }

    #[test]
    fn normalize_owned_and_in_place_reuse_existing_storage() {
        let mut path = br"//Textures\\Foo///BAR.DDS".to_vec();
        let capacity = path.capacity();
        normalize_path_in_place(&mut path);
        assert_eq!(path, b"textures/foo/bar.dds");
        assert_eq!(path.capacity(), capacity);

        assert_eq!(
            normalize_path_owned(br"//Meshes\\Door.NIF".to_vec()),
            b"meshes/door.nif"
        );
    }

    #[test]
    fn normalized_path_exposes_bytes_and_length() {
        let path = NormalizedPath::new(br"/Meshes\Thing.NIF");
        assert_eq!(path.as_bytes(), b"meshes/thing.nif");
        assert_eq!(path.as_bstr(), b"meshes/thing.nif".as_slice());
        assert_eq!(path.len(), b"meshes/thing.nif".len());
        assert!(!path.is_empty());
    }

    #[test]
    fn normalized_path_reports_utf8_only_when_valid() {
        assert_eq!(
            NormalizedPath::new("Textures/Foo.DDS").to_str(),
            Ok("textures/foo.dds")
        );
        assert!(NormalizedPath::new(b"textures/\xff.dds").to_str().is_err());
        assert_eq!(NormalizedPath::new(b"A\0B").to_str(), Ok("a\0b"));
    }

    #[test]
    fn normalized_path_exposes_virtual_components() {
        let path = NormalizedPath::new(br"/Textures/Architecture/Wall.DDS");
        assert_eq!(path.parent(), Some(BStr::new(b"textures/architecture")));
        assert_eq!(path.file_name(), Some(BStr::new(b"wall.dds")));
        assert_eq!(path.extension(), Some(BStr::new(b"dds")));

        let directory_like = NormalizedPath::new("textures/foo/");
        assert_eq!(directory_like.parent(), Some(BStr::new(b"textures")));
        assert_eq!(directory_like.file_name(), Some(BStr::new(b"foo")));
        assert_eq!(directory_like.extension(), None);
    }

    #[test]
    fn normalized_path_extension_is_byte_literal() {
        assert_eq!(
            NormalizedPath::new("foo.tar.gz").extension(),
            Some(BStr::new(b"gz"))
        );
        assert_eq!(NormalizedPath::new(".hidden").extension(), None);
        assert_eq!(NormalizedPath::new("foo.").extension(), None);
        assert_eq!(NormalizedPath::new("foo.dds/").extension(), None);
        assert_eq!(
            NormalizedPath::new(b"foo.\xff").extension(),
            Some(BStr::new(b"\xff"))
        );
    }

    #[test]
    fn normalized_component_helpers_operate_on_borrowed_bytes() {
        let path = b"textures/architecture/wall.dds";

        assert_eq!(
            parent_normalized(path),
            Some(BStr::new(b"textures/architecture"))
        );
        assert_eq!(file_name_normalized(path), Some(BStr::new(b"wall.dds")));
        assert_eq!(extension_normalized(path), Some(BStr::new(b"dds")));
        assert_eq!(extension_normalized(b"textures/foo.dds/"), None);
    }

    #[test]
    fn checked_normalized_constructor_rejects_unnormalized_bytes() {
        let path = NormalizedPath::try_from_normalized_bytes(b"textures/foo.dds".to_vec())
            .expect("path is already normalized");
        assert_eq!(path.as_bytes(), b"textures/foo.dds");
        assert_eq!(
            NormalizedPath::try_from_normalized_bytes(b"textures/foo/".to_vec())
                .expect("trailing separator is normalized")
                .as_bytes(),
            b"textures/foo/"
        );

        for path in [
            b"Textures/Foo.DDS".as_slice(),
            b"/textures/foo.dds".as_slice(),
            b"textures//foo.dds".as_slice(),
            br"textures\foo.dds".as_slice(),
        ] {
            let rejected = NormalizedPath::try_from_normalized_bytes(path.to_vec())
                .expect_err("path is not normalized");
            assert_eq!(rejected, path);
        }
    }

    #[test]
    fn normalized_path_borrows_as_normalized_bytes_for_lookup() {
        let mut values = HashMap::new();
        values.insert(NormalizedPath::new(br"/Meshes\Thing.NIF"), 7);

        assert_eq!(values.get(b"meshes/thing.nif".as_slice()), Some(&7));
        assert_eq!(values.get(BStr::new(b"meshes/thing.nif")), Some(&7));
    }

    #[test]
    fn normalized_path_as_ref_supports_byte_and_bstr_views() {
        let path = NormalizedPath::new(br"/Textures\Foo.DDS");
        let bytes: &[u8] = path.as_ref();
        let bstr: &BStr = path.as_ref();

        assert_eq!(bytes, b"textures/foo.dds");
        assert_eq!(bstr, b"textures/foo.dds".as_slice());
    }

    #[test]
    fn normalized_path_converts_into_owned_byte_strings() {
        let path = NormalizedPath::new(br"/Icons\Foo.TGA");
        let bstring = BString::from(path.clone());
        let bytes = Vec::<u8>::from(path);

        assert_eq!(bstring, b"icons/foo.tga".as_slice());
        assert_eq!(bytes, b"icons/foo.tga");
    }

    #[test]
    fn from_impls_normalize() {
        let bstring = BString::from(b"/Foo".to_vec());

        assert_eq!(NormalizedPath::from("/Foo").as_bytes(), b"foo");
        assert_eq!(NormalizedPath::from(b"/Foo".as_slice()).as_bytes(), b"foo");
        assert_eq!(NormalizedPath::from(BStr::new(&bstring)).as_bytes(), b"foo");
        assert_eq!(
            NormalizedPath::from(String::from("/Foo")).as_bytes(),
            b"foo"
        );
        assert_eq!(NormalizedPath::from(b"/Foo".to_vec()).as_bytes(), b"foo");
        assert_eq!(
            NormalizedPath::from(BString::from(b"/Foo".to_vec())).as_bytes(),
            b"foo"
        );
    }
}