pub struct SafePath { /* private fields */ }Expand description
A validated path that prevents path traversal attacks
This newtype wrapper around PathBuf ensures that all paths are validated
on construction and cannot be modified to violate safety constraints.
Implementations§
Source§impl SafePath
impl SafePath
Sourcepub fn new<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn new<P: AsRef<Path>>(path: P) -> Result<Self>
Create a new SafePath from a string-like type
§Validation Rules
- No parent directory references (..)
- Must be relative or within allowed root (if absolute)
- Maximum depth of 20 levels
- Path components must not be empty or contain only whitespace
§Examples
use ggen_utils::safe_path::SafePath;
// Valid paths
assert!(SafePath::new("src/generated").is_ok());
assert!(SafePath::new("./output").is_ok());
assert!(SafePath::new("test").is_ok());
// Invalid paths
assert!(SafePath::new("../etc").is_err());
assert!(SafePath::new("src/../../../etc").is_err());
assert!(SafePath::new("").is_err());Sourcepub fn join<P: AsRef<Path>>(&self, path: P) -> Result<Self>
pub fn join<P: AsRef<Path>>(&self, path: P) -> Result<Self>
Join this path with another path component
The joined path is validated to ensure safety constraints are maintained.
§Examples
use ggen_utils::safe_path::SafePath;
let base = SafePath::new("src").unwrap();
let joined = base.join("generated").unwrap();
assert_eq!(joined.as_path().to_str().unwrap(), "src/generated");
// Cannot join with parent references
assert!(base.join("../etc").is_err());Sourcepub fn as_path(&self) -> &Path
pub fn as_path(&self) -> &Path
Get a reference to the inner path
§Examples
use ggen_utils::safe_path::SafePath;
let safe = SafePath::new("src/generated").unwrap();
assert_eq!(safe.as_path().to_str().unwrap(), "src/generated");Sourcepub fn into_path_buf(self) -> PathBuf
pub fn into_path_buf(self) -> PathBuf
Convert into the inner PathBuf
§Examples
use ggen_utils::safe_path::SafePath;
let safe = SafePath::new("src/generated").unwrap();
let path_buf = safe.into_path_buf();
assert_eq!(path_buf.to_str().unwrap(), "src/generated");Sourcepub fn as_path_buf(&self) -> PathBuf
pub fn as_path_buf(&self) -> PathBuf
Get the PathBuf (clone)
§Examples
use ggen_utils::safe_path::SafePath;
let safe = SafePath::new("src/generated").unwrap();
let path_buf = safe.as_path_buf();
assert_eq!(path_buf.to_str().unwrap(), "src/generated");Sourcepub fn new_absolute<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn new_absolute<P: AsRef<Path>>(path: P) -> Result<Self>
Create a SafePath allowing absolute paths (for system paths like config files)
§Examples
use ggen_utils::safe_path::SafePath;
let safe = SafePath::new_absolute("/tmp/config.toml").unwrap();
assert!(safe.as_path().is_absolute());Sourcepub fn current_dir() -> Result<Self>
pub fn current_dir() -> Result<Self>
Get current working directory as SafePath
§Examples
use ggen_utils::safe_path::SafePath;
let cwd = SafePath::current_dir().unwrap();
assert!(cwd.as_path().is_absolute());Trait Implementations§
impl Eq for SafePath
impl StructuralPartialEq for SafePath
Auto Trait Implementations§
impl Freeze for SafePath
impl RefUnwindSafe for SafePath
impl Send for SafePath
impl Sync for SafePath
impl Unpin for SafePath
impl UnsafeUnpin for SafePath
impl UnwindSafe for SafePath
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.