path2enum
is a Rust procedural macro that automatically generates enums from your project’s real file paths. It provides type-safe, autocomplete-friendly access to static assets, config files, or any resources in your filesystem, reducing errors and boosting developer productivity.
- Generate Rust enums directly from directory structures, including nested folders.
- Filter files by extension (e.g.,
svg
,toml
,rs
). - Variant names are auto-converted to valid Rust identifiers with readable formatting.
- Uses the unique Japanese character
ノ
to visually separate nested directory names in enum variants. - Provides
.to_str()
method to get the original file path as a string. - Supports optional prefixing for virtual namespaces or folder grouping.
Add path2enum
to your Cargo.toml
dependencies:
cargo add path2enum
Import the magic macro and apply it to an empty enum to automatically generate variants representing files in your project directories. You can optionally specify the directory path (path) and file extension filter (ext).
#![allow(mixed_script_confusables)]
use path2enum::magic;
#[magic(path = "tests/assets", ext = "svg,toml")]
pub enum PublicPaths {}
assert_eq!(PublicPaths::ArrowLeft・svg.to_str(), "arrow-left.svg");
assert_eq!(PublicPaths::NestedDirノIcon・svg.to_str(), "nested_dir/icon.svg");
assert_eq!(PublicPaths::NestedDirノDeepDirノDeepIcon・svg.to_str(), "nested_dir/deep_dir/deep-icon.svg");
#[magic(ext = "rs,svg,toml")]
pub enum ProjectPaths {}
assert_eq!(ProjectPaths::SrcノLib・rs.to_str(), "src/lib.rs");
assert_eq!(ProjectPaths::TestsノAssetsノArrowLeft・svg.to_str(), "tests/assets/arrow-left.svg");
assert_eq!(ProjectPaths::Cargo・toml.to_str(), "Cargo.toml");
#[magic(path = "tests/assets", ext = "svg", prefix = "assets")]
pub enum Icons {}
assert_eq!(Icons::AssetsノHome・svg.to_str(), "assets/home.svg");
assert_eq!(Icons::Assetsノ_11Testノ_11・svg.to_str(),"assets/11-test/11.svg");
assert_eq!(Icons::AssetsノNestedDirノDeepDirノDeepIcon・svg.to_str(),"assets/nested_dir/deep_dir/deep-icon.svg");