23 unstable releases (3 breaking)

Uses new Rust 2024

0.4.7 Oct 4, 2025
0.4.6 Oct 4, 2025
0.4.5 Sep 21, 2025
0.4.0 Jul 11, 2025
0.1.6 Jul 10, 2025

#1310 in Procedural macros

Download history 65/week @ 2025-10-06 15/week @ 2025-10-13 3/week @ 2025-10-20

2,051 downloads per month

MIT/Apache

18KB
176 lines

path2enum

Crates.io Docs.rs License GitHub top language GitHub stars GitHub forks Tests Crates.io downloads GitHub last commit

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");
assert_eq!(PublicPaths::NestedDir.to_str(), "nested_dir");
assert_eq!(PublicPaths::NestedDirノDeepDir.to_str(), "nested_dir/deep_dir");

#[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");
assert_eq!(ProjectPaths::TestsノAssets.to_str(), "tests/assets");

#[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");
assert_eq!(Icons::Assetsノ_11Test.to_str(), "assets/11-test");
assert_eq!(Icons::AssetsノNestedDirノDeepDir.to_str(), "assets/nested_dir/deep_dir");

❤️ Donate

Monero QR BTC QR Revolut QR Wise QR


Dependencies

~130–520KB
~12K SLoC