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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024-present, fjall-rs
// Copyright (c) 2026-present, Structured World Foundation
//! Path types for the storage layer.
//!
//! Dual, mirroring [`crate::io`]: under `std` these ARE
//! [`std::path::Path`] / [`std::path::PathBuf`] (re-export), so the default
//! `StdFs` backend and existing call sites are unchanged. Under
//! `no_std + alloc` they are thin newtypes over `str` / `String` — a storage
//! "path" is an object KEY (e.g. `"tables/123"`, `"MANIFEST"`), not a
//! filesystem path, which is exactly what an embedded / WASM-IndexedDB backend
//! addresses by. Components are `/`-separated.
//!
//! Filesystem *queries* (`exists` / `is_file` / `is_dir`) are deliberately NOT
//! on these types — under `no_std` there is no ambient filesystem; the engine
//! routes those through the injected [`crate::fs::Fs`] backend instead.
#[cfg(feature = "std")]
pub use std::path::{Path, PathBuf};
/// Absolute-path helper. Identity under `no_std` (object keys have no
/// process-relative ambiguity to resolve); canonicalises against the process
/// CWD under `std`.
#[cfg(feature = "std")]
#[must_use]
pub fn absolute_path(path: &Path) -> PathBuf {
// `std::path::absolute` only fails on a degenerate input (e.g. an empty
// path). Rather than abort the process, fall back to the path as given so
// a caller-supplied path can never trigger a panic; an already-relative
// path simply stays relative, which the caller resolves against its cwd.
std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf())
}
#[cfg(not(feature = "std"))]
pub use nostd::{Path, PathBuf};
/// Identity under `no_std`: an object key is already absolute (no CWD).
#[cfg(not(feature = "std"))]
#[must_use]
pub fn absolute_path(path: &Path) -> PathBuf {
path.to_path_buf()
}
#[cfg(not(feature = "std"))]
mod nostd {
use alloc::borrow::ToOwned;
use alloc::string::String;
use core::fmt;
use core::ops::Deref;
/// Borrowed path slice, mirroring the subset of [`std::path::Path`] the
/// storage layer uses. `/`-separated object key. `#[repr(transparent)]`
/// over `str` so `&str ⇄ &Path` is a zero-cost reference cast.
#[repr(transparent)]
pub struct Path {
inner: str,
}
impl Path {
/// Wraps a string slice as a `Path` (zero-cost).
pub fn new<S: AsRef<str> + ?Sized>(s: &S) -> &Self {
// SAFETY: `Path` is `#[repr(transparent)]` over `str`, so a
// `&str` and a `&Path` have identical layout; the cast only
// re-labels the reference type.
unsafe { &*(core::ptr::from_ref::<str>(s.as_ref()) as *const Self) }
}
/// The underlying string slice.
#[must_use]
pub const fn as_str(&self) -> &str {
&self.inner
}
/// Always `Some` (the whole key is valid UTF-8 by construction);
/// mirrors [`std::path::Path::to_str`].
#[must_use]
pub const fn to_str(&self) -> Option<&str> {
Some(&self.inner)
}
/// Lossless owned form; mirrors [`std::path::Path::to_string_lossy`]
/// (always lossless here).
#[must_use]
pub fn to_string_lossy(&self) -> &str {
&self.inner
}
/// Owned copy.
#[must_use]
pub fn to_path_buf(&self) -> PathBuf {
PathBuf {
inner: self.inner.to_owned(),
}
}
/// Identity (already a `&Path`); mirrors
/// [`std::path::Path::as_path`] on `PathBuf`-like deref chains.
#[must_use]
pub const fn as_path(&self) -> &Self {
self
}
/// Appends `component`, inserting a `/` separator as needed.
#[must_use]
pub fn join<S: AsRef<str>>(&self, component: S) -> PathBuf {
let mut buf = self.to_path_buf();
buf.push(component);
buf
}
/// Parent key (everything before the last `/`), or `None` at the root.
#[must_use]
pub fn parent(&self) -> Option<&Self> {
self.inner
.rfind('/')
.map(|idx| Self::new(self.inner.split_at(idx).0))
}
/// Final `/`-separated component, or `None` if empty.
#[must_use]
pub fn file_name(&self) -> Option<&str> {
if self.inner.is_empty() {
return None;
}
Some(self.inner.rsplit('/').next().unwrap_or(&self.inner))
}
/// Whether this key begins with `base` on a component boundary.
#[must_use]
pub fn starts_with<S: AsRef<str>>(&self, base: S) -> bool {
let base = base.as_ref().trim_end_matches('/');
if base.is_empty() {
return true;
}
&self.inner == base
|| self
.inner
.strip_prefix(base)
.is_some_and(|rest| rest.starts_with('/'))
}
/// Strips a leading `base` component prefix.
///
/// # Errors
/// Returns `Err(StripPrefixError)` if `self` does not start with `base`.
pub fn strip_prefix<S: AsRef<str>>(&self, base: S) -> Result<&Self, StripPrefixError> {
let base = base.as_ref().trim_end_matches('/');
if base.is_empty() {
return Ok(self);
}
self.inner
.strip_prefix(base)
// Match only on a component boundary: "tables" must not strip
// the prefix of "tables10". The remainder is either empty
// (exact match) or begins with the `/` separator.
.filter(|rest| rest.is_empty() || rest.starts_with('/'))
.map(|rest| Self::new(rest.trim_start_matches('/')))
.ok_or(StripPrefixError(()))
}
/// `/`-separated components, skipping empty segments.
pub fn components(&self) -> impl Iterator<Item = &str> {
self.inner.split('/').filter(|s| !s.is_empty())
}
/// `Display`-able view (the raw key).
#[must_use]
pub const fn display(&self) -> &str {
&self.inner
}
/// Underlying bytes view; mirrors [`std::path::Path::as_os_str`].
/// The key is always valid UTF-8 here, so this is the raw `str`.
#[must_use]
pub const fn as_os_str(&self) -> &str {
&self.inner
}
}
impl alloc::borrow::ToOwned for Path {
type Owned = PathBuf;
fn to_owned(&self) -> PathBuf {
self.to_path_buf()
}
}
impl fmt::Debug for Path {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl fmt::Display for Path {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.inner)
}
}
impl PartialEq for Path {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl Eq for Path {}
impl PartialOrd for Path {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Path {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.inner.cmp(&other.inner)
}
}
impl core::hash::Hash for Path {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}
impl AsRef<Path> for Path {
fn as_ref(&self) -> &Path {
self
}
}
impl AsRef<str> for Path {
fn as_ref(&self) -> &str {
&self.inner
}
}
/// Owned path key, mirroring the subset of [`std::path::PathBuf`] used.
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PathBuf {
inner: String,
}
impl PathBuf {
/// Empty path.
#[must_use]
pub const fn new() -> Self {
Self {
inner: String::new(),
}
}
/// Borrow as a [`Path`].
#[must_use]
pub fn as_path(&self) -> &Path {
Path::new(&self.inner)
}
/// Appends `component`, inserting a `/` separator when neither side
/// already supplies one. An absolute `component` (leading `/`)
/// replaces the buffer, matching `std`'s root-push semantics.
pub fn push<S: AsRef<str>>(&mut self, component: S) {
let c = component.as_ref();
if c.starts_with('/') {
self.inner.clear();
self.inner.push_str(c);
return;
}
if !self.inner.is_empty() && !self.inner.ends_with('/') {
self.inner.push('/');
}
self.inner.push_str(c);
}
/// Removes the last component; returns `false` at the root.
pub fn pop(&mut self) -> bool {
match self.inner.rfind('/') {
Some(idx) => {
self.inner.truncate(idx);
true
}
None if self.inner.is_empty() => false,
None => {
self.inner.clear();
true
}
}
}
/// `Display`-able view.
#[must_use]
pub fn display(&self) -> &str {
&self.inner
}
}
impl Deref for PathBuf {
type Target = Path;
fn deref(&self) -> &Path {
self.as_path()
}
}
impl PartialEq<Path> for PathBuf {
fn eq(&self, other: &Path) -> bool {
self.as_path() == other
}
}
impl PartialEq<PathBuf> for Path {
fn eq(&self, other: &PathBuf) -> bool {
self == other.as_path()
}
}
impl PartialEq<&Path> for PathBuf {
fn eq(&self, other: &&Path) -> bool {
self.as_path() == *other
}
}
impl alloc::borrow::Borrow<Path> for PathBuf {
fn borrow(&self) -> &Path {
self.as_path()
}
}
impl AsRef<Path> for PathBuf {
fn as_ref(&self) -> &Path {
self.as_path()
}
}
impl From<String> for PathBuf {
fn from(inner: String) -> Self {
Self { inner }
}
}
impl From<&str> for PathBuf {
fn from(s: &str) -> Self {
Self {
inner: s.to_owned(),
}
}
}
impl From<&Path> for PathBuf {
fn from(p: &Path) -> Self {
p.to_path_buf()
}
}
impl AsRef<str> for PathBuf {
fn as_ref(&self) -> &str {
&self.inner
}
}
impl fmt::Debug for PathBuf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl fmt::Display for PathBuf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.inner)
}
}
/// Error returned by [`Path::strip_prefix`] when the prefix does not match.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StripPrefixError(());
impl fmt::Display for StripPrefixError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("prefix not found")
}
}
}