Skip to main content

srpath

Macro srpath 

Source
srpath!() { /* proc-macro */ }
Expand description

Validate a path literal at compile time and produce a &'static SafeRelativePath.

The macro form of SafeRelativePath::from_relative_path: it runs the same check, but on a string literal at compile time, so the runtime cost is zero. Useful for constants — sentinel paths, hard-coded subdirectory names, anything that’s known when the program is compiled.

A literal containing .. (or that doesn’t parse as a relative path) becomes a compile error instead of a run-time Result::Err.

§Example

use zenops_safe_relative_path::{SafeRelativePath, srpath};

const CONFIG: &SafeRelativePath = srpath!("config/app.toml");
assert_eq!(CONFIG.as_str(), "config/app.toml");

Rejected at compile time:

use zenops_safe_relative_path::srpath;
let _ = srpath!("../etc/passwd");

Creates a SafeRelativePath without run-time cost by validating the string literal at compile time