Skip to main content

SinglePathComponent

Struct SinglePathComponent 

Source
pub struct SinglePathComponent(/* private fields */);
Expand description

A path that is exactly one segment — no separators, no traversal.

Narrower than SafeRelativePath: where SafeRelativePath allows any number of components as long as none of them are .., SinglePathComponent permits exactly one. Reach for it when a value has to be a single name in a flat namespace — a package key, a configuration map key, a directory entry — and you want the type system to enforce that.

Derefs to SafeRelativePath, so a SinglePathComponent can be handed to anything that takes &SafeRelativePath without conversion.

§Example

use zenops_safe_relative_path::SinglePathComponent;

assert!(SinglePathComponent::try_new("zsh").is_ok());

// More than one component — rejected.
assert!(SinglePathComponent::try_new("zsh/init.sh").is_err());
// Traversal — also rejected.
assert!(SinglePathComponent::try_new("..").is_err());

Implementations§

Source§

impl SinglePathComponent

Source

pub fn try_new(v: &str) -> Result<Self, Error>

Try to wrap a string as a SinglePathComponent.

Fails on anything containing /, anything with .. traversal, and the empty string.

Source

pub fn as_safe_relative_path(&self) -> &SafeRelativePath

View this component as a SafeRelativePath.

SinglePathComponent already Derefs to SafeRelativePath, so most call sites don’t need this directly — it’s exposed for places where an explicit conversion reads more clearly than a reborrow.

Methods from Deref<Target = SafeRelativePath>§

Source

pub fn to_safe_relative_path_buf(&self) -> SafeRelativePathBuf

Copy this borrowed path into an owned SafeRelativePathBuf.

Source

pub fn normalize_safe(&self) -> SafeRelativePathBuf

Collapse . components and produce a normalised owned path.

Unlike Path::canonicalize this is purely lexical — no filesystem access. A SafeRelativePath cannot contain .. segments, so normalisation only ever drops . components.

§Example
use zenops_safe_relative_path::SafeRelativePath;

let p = SafeRelativePath::from_relative_path("a/./b").unwrap();
assert_eq!(p.normalize_safe().as_str(), "a/b");
Source

pub fn safe_join( &self, path: impl AsRef<SafeRelativePath>, ) -> SafeRelativePathBuf

Join another already-safe path onto this one.

The infallible counterpart to try_join: both sides are already known to be safe, so the join cannot introduce traversal and no validation is needed.

§Example
use zenops_safe_relative_path::srpath;

let joined = srpath!("config").safe_join(srpath!("app.toml"));
assert_eq!(joined.as_str(), "config/app.toml");
Source

pub fn try_join( &self, path: impl AsRef<RelativePath>, ) -> Result<SafeRelativePathBuf, Error>

Join another path onto this one, returning an error if the joined segment would escape.

This is the safe counterpart to Path::join for inputs that come from configuration or another untrusted source: the result is still a relative path contained by the original base.

§Example
use zenops_safe_relative_path::srpath;

let base = srpath!("config");
assert_eq!(
    base.try_join("app.toml").unwrap().as_str(),
    "config/app.toml",
);
assert!(base.try_join("../../etc/passwd").is_err());
Source

pub fn as_str(&self) -> &str

View the path as a string slice.

Source

pub fn to_full_path(&self, base: impl AsRef<Path>) -> PathBuf

Resolve this relative path against base to produce an absolute PathBuf.

Use this at the edge of the program, when a SafeRelativePath finally needs to be handed to a filesystem call against a known root — typically $HOME or $XDG_CONFIG_HOME. The result is base followed by this path’s components, with no .. traversal between them.

§Example
use std::path::Path;
use zenops_safe_relative_path::srpath;

let abs = srpath!("config/app.toml").to_full_path(Path::new("/home/ada"));
assert_eq!(abs, Path::new("/home/ada/config/app.toml"));
Source

pub fn safe_parent(&self) -> Option<&SafeRelativePath>

Return the parent path, or None if there is no parent.

The parent of a SafeRelativePath is itself a SafeRelativePath — dropping a final component can never introduce traversal.

§Example
use zenops_safe_relative_path::srpath;

assert_eq!(srpath!("a/b/c").safe_parent().unwrap().as_str(), "a/b");
assert!(srpath!("").safe_parent().is_none());

Trait Implementations§

Source§

impl AsRef<SafeRelativePath> for SinglePathComponent

Source§

fn as_ref(&self) -> &SafeRelativePath

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for SinglePathComponent

Source§

fn clone(&self) -> SinglePathComponent

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SinglePathComponent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for SinglePathComponent

Source§

type Target = SafeRelativePath

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'de> Deserialize<'de> for SinglePathComponent

Source§

fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for SinglePathComponent

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for SinglePathComponent

Source§

impl Hash for SinglePathComponent

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for SinglePathComponent

Source§

fn cmp(&self, other: &SinglePathComponent) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for SinglePathComponent

Source§

fn eq(&self, other: &SinglePathComponent) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for SinglePathComponent

Source§

fn partial_cmp(&self, other: &SinglePathComponent) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for SinglePathComponent

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.