pub struct Path(/* private fields */);Expand description
A validated path in StructFS.
Path components must be valid Unicode identifiers (per UAX#31) or numeric strings (for array indexing). This ensures paths can be used as identifiers in most programming languages.
§Refinement of LLPath
Path is a validated refinement of the low-level LLPath: it wraps an
LLPath whose every component is additionally guaranteed to be valid UTF-8
and a valid component grammar (identifier or numeric). Because a Path
is an LLPath that has been validated, widening (as_ll /
into_ll) is free, and narrowing
(validate) is the single place validation happens.
Components are stored as byte components, so Path -> LLPath never copies
and structural ops (join/slice/strip_prefix) clone Bytes
(reference-count bumps) rather than deep-copying Strings.
Implementations§
Source§impl Path
impl Path
Sourcepub fn parse(s: &str) -> Result<Self, PathError>
pub fn parse(s: &str) -> Result<Self, PathError>
Parse a path string, validating components.
§Path Syntax
- Components are separated by
/ - Empty components are ignored (normalizes
//and trailing/) - Each component must be a valid identifier or numeric string
§Examples
use structfs_core_store::Path;
let path = Path::parse("users/123/name").unwrap();
assert_eq!(path.len(), 3);
// Trailing slashes are normalized
assert_eq!(Path::parse("foo/bar/").unwrap(), Path::parse("foo/bar").unwrap());Sourcepub fn from_components(components: Vec<String>) -> Self
pub fn from_components(components: Vec<String>) -> Self
Create a path from pre-validated components.
§Panics
Panics if any component is invalid. Use try_from_components for
fallible construction.
Sourcepub fn try_from_components(components: Vec<String>) -> Result<Self, PathError>
pub fn try_from_components(components: Vec<String>) -> Result<Self, PathError>
Try to create a path from components, validating each.
Sourcepub fn validate_component(
component: &str,
position: usize,
) -> Result<(), PathError>
pub fn validate_component( component: &str, position: usize, ) -> Result<(), PathError>
Validate a single path component against the StructFS grammar.
The grammar is shared with the compile-time path! macro via the
structfs-path-validation crate: a component is a UAX#31 identifier
(an underscore prefix is allowed when followed by more identifier
characters) or a pure numeric string.
position is only used to build the error; pass 0 when validating
a component in isolation.
Sourcepub fn child(&self, component: impl Into<PathComponent>) -> Path
pub fn child(&self, component: impl Into<PathComponent>) -> Path
Return a new path with the component appended.
Sourcepub fn push(&mut self, component: impl Into<PathComponent>)
pub fn push(&mut self, component: impl Into<PathComponent>)
Append a component in place.
Sourcepub fn has_prefix(&self, prefix: &Path) -> bool
pub fn has_prefix(&self, prefix: &Path) -> bool
Check if this path has the given prefix.
Sourcepub fn strip_prefix(&self, prefix: &Path) -> Option<Path>
pub fn strip_prefix(&self, prefix: &Path) -> Option<Path>
Strip a prefix from this path.
Returns None if the prefix doesn’t match.
Sourcepub fn as_ll(&self) -> &LLPath
pub fn as_ll(&self) -> &LLPath
Borrow this path as its underlying LLPath — the free widening from
the validated high-level contract to the opaque low-level one.
Sourcepub fn into_ll(self) -> LLPath
pub fn into_ll(self) -> LLPath
Consume this path into its underlying LLPath — free widening with no
component copy.
Sourcepub fn validate(ll: LLPath) -> Result<Self, PathError>
pub fn validate(ll: LLPath) -> Result<Self, PathError>
Validate an LLPath into a Path — the single narrowing point where
opaque bytes become a validated identifier path. Reuses the Bytes
components (no copy); fails if any component is not valid UTF-8 or not a
valid component grammar.
Sourcepub fn from_ll_unchecked(ll: LLPath) -> Self
pub fn from_ll_unchecked(ll: LLPath) -> Self
Wrap an LLPath known to already satisfy the Path invariant, without
re-validating. This is the byte-path analogue of
from_validated_components: use it for
paths that originated host-side from a Path (e.g. a write result path
echoed back), so internal LL->HL hops don’t re-pay validation. Debug
builds re-check as a safety net.