Skip to content

path2enum is a procedural macro for Rust that generates enums based on your project's real file paths. It enables type-safe, autocomplete-friendly access to static assets and configuration files, reducing errors and improving developer experience.

License

Notifications You must be signed in to change notification settings

pas2rust/path2enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

path2enum

Crates.io Docs.rs License

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.


🔨 Features

  • 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.

⚙️ Installation

Add path2enum to your Cargo.toml dependencies:

cargo add path2enum

🚀 Usage

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");

❤️ Donate

Monero Bitcoin

About

path2enum is a procedural macro for Rust that generates enums based on your project's real file paths. It enables type-safe, autocomplete-friendly access to static assets and configuration files, reducing errors and improving developer experience.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages