Compare commits
31 commits
Author | SHA1 | Date | |
---|---|---|---|
49df03b2e8 | |||
b7dd6f9379 | |||
c7d4ba974b | |||
621f0670ce | |||
086b84c81d | |||
fdc79b41cf | |||
4de26f8e50 | |||
ca7262d40e | |||
902ef858ce | |||
9b90c53b35 | |||
daf793eb06 | |||
f0ce2b2737 | |||
9aa37c1519 | |||
ffa1dc5a7b | |||
9dc4cc6c37 | |||
fecb084cd5 | |||
551009c3eb | |||
d05df1bb4d | |||
a8864786aa | |||
e6316bef59 | |||
e19459cd3d | |||
51fe489cb0 | |||
3db4c91f48 | |||
9db89454a3 | |||
49612d642c | |||
d65409a3ea | |||
8d2faf1f64 | |||
f049d39bb8 | |||
9934e566c6 | |||
73672d8c96 | |||
b3771ba87a |
32 changed files with 1143 additions and 867 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
.direnv/
|
.direnv/
|
||||||
target/
|
target/
|
||||||
|
wip/
|
||||||
|
|
92
CHANGELOG.md
Normal file
92
CHANGELOG.md
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
- Nothing yet
|
||||||
|
|
||||||
|
## [0.3.6] - 2025-04-17
|
||||||
|
|
||||||
|
- Better error messages when having route.rs files with invalid code
|
||||||
|
|
||||||
|
## [0.3.5] - 2025-04-16
|
||||||
|
|
||||||
|
- Moved macrotest to dev deps
|
||||||
|
|
||||||
|
## [0.3.4] - 2025-04-16
|
||||||
|
|
||||||
|
- Refactored huge lib.rs into 3 seperate files.
|
||||||
|
- Downgraded edition to 2021 for better compatability
|
||||||
|
|
||||||
|
## [0.3.3] - 2025-04-15
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Add support for remaining HTTP methods
|
||||||
|
- we no support the full set as defined by rfc9110
|
||||||
|
- trace & connect were missing specifically
|
||||||
|
- Add support for `any` axum router method (default method router, others will take precedence)
|
||||||
|
|
||||||
|
## [0.3.2] - 2025-04-15
|
||||||
|
- Refactor internals
|
||||||
|
- Add solid testing
|
||||||
|
- explicitly test generated macro output using macrotest
|
||||||
|
- test error output using trybuilt
|
||||||
|
|
||||||
|
## [0.3.1] - 2025-04-15
|
||||||
|
|
||||||
|
- Fix invalid doc links
|
||||||
|
|
||||||
|
## [0.3.0] - 2025-04-15
|
||||||
|
|
||||||
|
After some experimentation, the API has begun to stabilize. This should likely be the last breaking change for some time.
|
||||||
|
|
||||||
|
### Breaking Changes
|
||||||
|
|
||||||
|
- **Reworked implementation into an attribute macro**
|
||||||
|
- Previous implementation required function calls:
|
||||||
|
```rust
|
||||||
|
folder_router!("./examples/simple/api", AppState);
|
||||||
|
// ...
|
||||||
|
let folder_router: Router<AppState> = folder_router();
|
||||||
|
```
|
||||||
|
- New implementation uses an attribute macro:
|
||||||
|
```rust
|
||||||
|
#[folder_router("./examples/simple/api", AppState)]
|
||||||
|
struct MyFolderRouter;
|
||||||
|
// ...
|
||||||
|
let folder_router: Router<AppState> = MyFolderRouter::into_router();
|
||||||
|
```
|
||||||
|
- This approach provides a cleaner API and allows for multiple separate folder-based Routers
|
||||||
|
|
||||||
|
## [0.2.3] - 2025-04-14
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **Improved method detection** - Now properly parses files instead of using string matching
|
||||||
|
- Previous version checked if file contained ```pub async #method_name```
|
||||||
|
- New version properly parses the file using `syn` for more accurate detection
|
||||||
|
|
||||||
|
## [0.2.2] - 2025-04-14
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **License changed to MIT**
|
||||||
|
|
||||||
|
## [0.2.1] - 2025-04-14
|
||||||
|
|
||||||
|
### Improved
|
||||||
|
- Enhanced documentation
|
||||||
|
- Added more comprehensive tests
|
||||||
|
|
||||||
|
## [0.2.0] - 2024-04-14
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- **Improved code integration**
|
||||||
|
- Generate module imports instead of using ```include!```
|
||||||
|
- Makes the code compatible with rust-analyzer
|
||||||
|
- Provides better IDE support
|
||||||
|
|
||||||
|
## [0.1.0] - 2024-04-14
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Initial release
|
||||||
|
- Minimum viable product adapted from https://github.com/richardanaya/axum-folder-router-htmx
|
218
Cargo.lock
generated
218
Cargo.lock
generated
|
@ -17,6 +17,15 @@ version = "2.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aho-corasick"
|
||||||
|
version = "1.1.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "anyhow"
|
name = "anyhow"
|
||||||
version = "1.0.98"
|
version = "1.0.98"
|
||||||
|
@ -85,15 +94,18 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "axum-folder-router"
|
name = "axum-folder-router"
|
||||||
version = "0.1.0"
|
version = "0.3.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
"glob",
|
"glob",
|
||||||
|
"macrotest",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
"regex",
|
||||||
"syn",
|
"syn",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"trybuild",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -129,6 +141,24 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diff"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "equivalent"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fastrand"
|
||||||
|
version = "2.3.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fnv"
|
name = "fnv"
|
||||||
version = "1.0.7"
|
version = "1.0.7"
|
||||||
|
@ -189,6 +219,12 @@ version = "0.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
|
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "hashbrown"
|
||||||
|
version = "0.15.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "http"
|
name = "http"
|
||||||
version = "1.3.1"
|
version = "1.3.1"
|
||||||
|
@ -270,6 +306,16 @@ dependencies = [
|
||||||
"tower-service",
|
"tower-service",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "indexmap"
|
||||||
|
version = "2.9.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cea70ddb795996207ad57735b50c5982d8844f38ba9ee5f1aedcfb708a2aa11e"
|
||||||
|
dependencies = [
|
||||||
|
"equivalent",
|
||||||
|
"hashbrown",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.15"
|
version = "1.0.15"
|
||||||
|
@ -278,9 +324,9 @@ checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.171"
|
version = "0.2.172"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
|
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
|
@ -298,6 +344,23 @@ version = "0.4.27"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "macrotest"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0597a8d49ceeea5845b12d1970aa993261e68d4660b327eabab667b3e7ffd60"
|
||||||
|
dependencies = [
|
||||||
|
"diff",
|
||||||
|
"fastrand",
|
||||||
|
"glob",
|
||||||
|
"prettyplease",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json",
|
||||||
|
"syn",
|
||||||
|
"toml_edit",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchit"
|
name = "matchit"
|
||||||
version = "0.8.4"
|
version = "0.8.4"
|
||||||
|
@ -333,7 +396,7 @@ checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"wasi",
|
"wasi",
|
||||||
"windows-sys",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -392,6 +455,16 @@ version = "0.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "prettyplease"
|
||||||
|
version = "0.2.32"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "664ec5419c51e34154eec046ebcba56312d5a2fc3b09a06da188e1ad21afadf6"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.94"
|
version = "1.0.94"
|
||||||
|
@ -412,13 +485,42 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "redox_syscall"
|
name = "redox_syscall"
|
||||||
version = "0.5.10"
|
version = "0.5.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1"
|
checksum = "d2f103c6d277498fbceb16e84d317e2a400f160f46904d5f5410848c829511a3"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex"
|
||||||
|
version = "1.11.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-automata",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-automata"
|
||||||
|
version = "0.4.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
|
||||||
|
dependencies = [
|
||||||
|
"aho-corasick",
|
||||||
|
"memchr",
|
||||||
|
"regex-syntax",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "regex-syntax"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc-demangle"
|
name = "rustc-demangle"
|
||||||
version = "0.1.24"
|
version = "0.1.24"
|
||||||
|
@ -485,6 +587,15 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_spanned"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
|
@ -519,7 +630,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef"
|
checksum = "4f5fd57c80058a56cf5c777ab8a126398ece8e442983605d280a44ce79d0edef"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -539,6 +650,21 @@ version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "target-triple"
|
||||||
|
version = "0.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1ac9aa371f599d22256307c24a9d748c041e548cbf599f35d890f9d365361790"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "termcolor"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.44.2"
|
version = "1.44.2"
|
||||||
|
@ -554,7 +680,7 @@ dependencies = [
|
||||||
"signal-hook-registry",
|
"signal-hook-registry",
|
||||||
"socket2",
|
"socket2",
|
||||||
"tokio-macros",
|
"tokio-macros",
|
||||||
"windows-sys",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -568,6 +694,40 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.8.20"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"toml_edit",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_datetime"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_edit"
|
||||||
|
version = "0.22.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap",
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower"
|
name = "tower"
|
||||||
version = "0.5.2"
|
version = "0.5.2"
|
||||||
|
@ -616,6 +776,21 @@ dependencies = [
|
||||||
"once_cell",
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "trybuild"
|
||||||
|
version = "1.0.104"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6ae08be68c056db96f0e6c6dd820727cca756ced9e1f4cc7fdd20e2a55e23898"
|
||||||
|
dependencies = [
|
||||||
|
"glob",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json",
|
||||||
|
"target-triple",
|
||||||
|
"termcolor",
|
||||||
|
"toml",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
@ -628,6 +803,15 @@ version = "0.11.0+wasi-snapshot-preview1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winapi-util"
|
||||||
|
version = "0.1.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||||
|
dependencies = [
|
||||||
|
"windows-sys 0.59.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-sys"
|
name = "windows-sys"
|
||||||
version = "0.52.0"
|
version = "0.52.0"
|
||||||
|
@ -637,6 +821,15 @@ dependencies = [
|
||||||
"windows-targets",
|
"windows-targets",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-sys"
|
||||||
|
version = "0.59.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-targets"
|
name = "windows-targets"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
|
@ -700,3 +893,12 @@ name = "windows_x86_64_msvc"
|
||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.7.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "63d3fcd9bba44b03821e7d699eeee959f3126dcc4aa8e4ae18ec617c2a5cea10"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
16
Cargo.toml
16
Cargo.toml
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "axum-folder-router"
|
name = "axum-folder-router"
|
||||||
version = "0.1.0"
|
version = "0.3.6"
|
||||||
edition = "2024"
|
edition = "2021"
|
||||||
readme = "./README.md"
|
readme = "./README.md"
|
||||||
authors = ["Tristan Druyen <ek36g2vcc@mozmail.com>"]
|
authors = ["Tristan Druyen <ek36g2vcc@mozmail.com>"]
|
||||||
categories = ["web-programming"]
|
categories = ["web-programming"]
|
||||||
|
@ -9,20 +9,26 @@ keywords = ["axum", "controller", "macro", "routing"]
|
||||||
description = "Helper macro for simple folder based routing of axum handlers"
|
description = "Helper macro for simple folder based routing of axum handlers"
|
||||||
homepage = "https://git.vlt81.de/vault81/axum-folder-router"
|
homepage = "https://git.vlt81.de/vault81/axum-folder-router"
|
||||||
repository = "https://git.vlt81.de/vault81/axum-folder-router"
|
repository = "https://git.vlt81.de/vault81/axum-folder-router"
|
||||||
license = "AGPL-3.0-or-later"
|
license = "MIT"
|
||||||
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "./src/lib.rs"
|
path = "./src/lib.rs"
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
syn = "2.0"
|
syn = { version = "2.0", features = ["full"] }
|
||||||
quote = "1.0"
|
quote = "1.0"
|
||||||
proc-macro2 = "1.0"
|
proc-macro2 = "1.0"
|
||||||
glob = "0.3"
|
glob = "0.3"
|
||||||
|
regex = "1.11.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1.0.98"
|
anyhow = "1.0.98"
|
||||||
axum = "0.8.3"
|
axum = "0.8.3"
|
||||||
tokio = { version = "1.44.2", features = ["full"] }
|
tokio = { version = "1.44.2", features = ["full"] }
|
||||||
|
trybuild = "1.0.104"
|
||||||
|
macrotest = "1.1.0"
|
||||||
|
|
||||||
|
[lints.clippy]
|
||||||
|
pedantic = { level = "warn", priority = -1 }
|
||||||
|
unused-async = { level = "allow", priority = 0 } # required for examples without unecessary noise
|
||||||
|
|
675
LICENSE.md
675
LICENSE.md
|
@ -1,651 +1,26 @@
|
||||||
GNU Affero General Public License
|
The MIT License (MIT)
|
||||||
=================================
|
=====================
|
||||||
|
|
||||||
|
Copyright © `2025` `Tristan Druyen`
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person
|
||||||
|
obtaining a copy of this software and associated documentation
|
||||||
|
files (the “Software”), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use,
|
||||||
|
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the
|
||||||
|
Software is furnished to do so, subject to the following
|
||||||
|
conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||||
|
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||||
|
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||||
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
_Version 3, 19 November 2007_
|
|
||||||
_Copyright © 2007 Free Software Foundation, Inc. <<http://fsf.org/>>_
|
|
||||||
|
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
|
||||||
of this license document, but changing it is not allowed.
|
|
||||||
|
|
||||||
## Preamble
|
|
||||||
|
|
||||||
The GNU Affero General Public License is a free, copyleft license for
|
|
||||||
software and other kinds of works, specifically designed to ensure
|
|
||||||
cooperation with the community in the case of network server software.
|
|
||||||
|
|
||||||
The licenses for most software and other practical works are designed
|
|
||||||
to take away your freedom to share and change the works. By contrast,
|
|
||||||
our General Public Licenses are intended to guarantee your freedom to
|
|
||||||
share and change all versions of a program--to make sure it remains free
|
|
||||||
software for all its users.
|
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
|
||||||
price. Our General Public Licenses are designed to make sure that you
|
|
||||||
have the freedom to distribute copies of free software (and charge for
|
|
||||||
them if you wish), that you receive source code or can get it if you
|
|
||||||
want it, that you can change the software or use pieces of it in new
|
|
||||||
free programs, and that you know you can do these things.
|
|
||||||
|
|
||||||
Developers that use our General Public Licenses protect your rights
|
|
||||||
with two steps: **(1)** assert copyright on the software, and **(2)** offer
|
|
||||||
you this License which gives you legal permission to copy, distribute
|
|
||||||
and/or modify the software.
|
|
||||||
|
|
||||||
A secondary benefit of defending all users' freedom is that
|
|
||||||
improvements made in alternate versions of the program, if they
|
|
||||||
receive widespread use, become available for other developers to
|
|
||||||
incorporate. Many developers of free software are heartened and
|
|
||||||
encouraged by the resulting cooperation. However, in the case of
|
|
||||||
software used on network servers, this result may fail to come about.
|
|
||||||
The GNU General Public License permits making a modified version and
|
|
||||||
letting the public access it on a server without ever releasing its
|
|
||||||
source code to the public.
|
|
||||||
|
|
||||||
The GNU Affero General Public License is designed specifically to
|
|
||||||
ensure that, in such cases, the modified source code becomes available
|
|
||||||
to the community. It requires the operator of a network server to
|
|
||||||
provide the source code of the modified version running there to the
|
|
||||||
users of that server. Therefore, public use of a modified version, on
|
|
||||||
a publicly accessible server, gives the public access to the source
|
|
||||||
code of the modified version.
|
|
||||||
|
|
||||||
An older license, called the Affero General Public License and
|
|
||||||
published by Affero, was designed to accomplish similar goals. This is
|
|
||||||
a different license, not a version of the Affero GPL, but Affero has
|
|
||||||
released a new version of the Affero GPL which permits relicensing under
|
|
||||||
this license.
|
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
|
||||||
modification follow.
|
|
||||||
|
|
||||||
## TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
### 0. Definitions
|
|
||||||
|
|
||||||
“This License” refers to version 3 of the GNU Affero General Public License.
|
|
||||||
|
|
||||||
“Copyright” also means copyright-like laws that apply to other kinds of
|
|
||||||
works, such as semiconductor masks.
|
|
||||||
|
|
||||||
“The Program” refers to any copyrightable work licensed under this
|
|
||||||
License. Each licensee is addressed as “you”. “Licensees” and
|
|
||||||
“recipients” may be individuals or organizations.
|
|
||||||
|
|
||||||
To “modify” a work means to copy from or adapt all or part of the work
|
|
||||||
in a fashion requiring copyright permission, other than the making of an
|
|
||||||
exact copy. The resulting work is called a “modified version” of the
|
|
||||||
earlier work or a work “based on” the earlier work.
|
|
||||||
|
|
||||||
A “covered work” means either the unmodified Program or a work based
|
|
||||||
on the Program.
|
|
||||||
|
|
||||||
To “propagate” a work means to do anything with it that, without
|
|
||||||
permission, would make you directly or secondarily liable for
|
|
||||||
infringement under applicable copyright law, except executing it on a
|
|
||||||
computer or modifying a private copy. Propagation includes copying,
|
|
||||||
distribution (with or without modification), making available to the
|
|
||||||
public, and in some countries other activities as well.
|
|
||||||
|
|
||||||
To “convey” a work means any kind of propagation that enables other
|
|
||||||
parties to make or receive copies. Mere interaction with a user through
|
|
||||||
a computer network, with no transfer of a copy, is not conveying.
|
|
||||||
|
|
||||||
An interactive user interface displays “Appropriate Legal Notices”
|
|
||||||
to the extent that it includes a convenient and prominently visible
|
|
||||||
feature that **(1)** displays an appropriate copyright notice, and **(2)**
|
|
||||||
tells the user that there is no warranty for the work (except to the
|
|
||||||
extent that warranties are provided), that licensees may convey the
|
|
||||||
work under this License, and how to view a copy of this License. If
|
|
||||||
the interface presents a list of user commands or options, such as a
|
|
||||||
menu, a prominent item in the list meets this criterion.
|
|
||||||
|
|
||||||
### 1. Source Code
|
|
||||||
|
|
||||||
The “source code” for a work means the preferred form of the work
|
|
||||||
for making modifications to it. “Object code” means any non-source
|
|
||||||
form of a work.
|
|
||||||
|
|
||||||
A “Standard Interface” means an interface that either is an official
|
|
||||||
standard defined by a recognized standards body, or, in the case of
|
|
||||||
interfaces specified for a particular programming language, one that
|
|
||||||
is widely used among developers working in that language.
|
|
||||||
|
|
||||||
The “System Libraries” of an executable work include anything, other
|
|
||||||
than the work as a whole, that **(a)** is included in the normal form of
|
|
||||||
packaging a Major Component, but which is not part of that Major
|
|
||||||
Component, and **(b)** serves only to enable use of the work with that
|
|
||||||
Major Component, or to implement a Standard Interface for which an
|
|
||||||
implementation is available to the public in source code form. A
|
|
||||||
“Major Component”, in this context, means a major essential component
|
|
||||||
(kernel, window system, and so on) of the specific operating system
|
|
||||||
(if any) on which the executable work runs, or a compiler used to
|
|
||||||
produce the work, or an object code interpreter used to run it.
|
|
||||||
|
|
||||||
The “Corresponding Source” for a work in object code form means all
|
|
||||||
the source code needed to generate, install, and (for an executable
|
|
||||||
work) run the object code and to modify the work, including scripts to
|
|
||||||
control those activities. However, it does not include the work's
|
|
||||||
System Libraries, or general-purpose tools or generally available free
|
|
||||||
programs which are used unmodified in performing those activities but
|
|
||||||
which are not part of the work. For example, Corresponding Source
|
|
||||||
includes interface definition files associated with source files for
|
|
||||||
the work, and the source code for shared libraries and dynamically
|
|
||||||
linked subprograms that the work is specifically designed to require,
|
|
||||||
such as by intimate data communication or control flow between those
|
|
||||||
subprograms and other parts of the work.
|
|
||||||
|
|
||||||
The Corresponding Source need not include anything that users
|
|
||||||
can regenerate automatically from other parts of the Corresponding
|
|
||||||
Source.
|
|
||||||
|
|
||||||
The Corresponding Source for a work in source code form is that
|
|
||||||
same work.
|
|
||||||
|
|
||||||
### 2. Basic Permissions
|
|
||||||
|
|
||||||
All rights granted under this License are granted for the term of
|
|
||||||
copyright on the Program, and are irrevocable provided the stated
|
|
||||||
conditions are met. This License explicitly affirms your unlimited
|
|
||||||
permission to run the unmodified Program. The output from running a
|
|
||||||
covered work is covered by this License only if the output, given its
|
|
||||||
content, constitutes a covered work. This License acknowledges your
|
|
||||||
rights of fair use or other equivalent, as provided by copyright law.
|
|
||||||
|
|
||||||
You may make, run and propagate covered works that you do not
|
|
||||||
convey, without conditions so long as your license otherwise remains
|
|
||||||
in force. You may convey covered works to others for the sole purpose
|
|
||||||
of having them make modifications exclusively for you, or provide you
|
|
||||||
with facilities for running those works, provided that you comply with
|
|
||||||
the terms of this License in conveying all material for which you do
|
|
||||||
not control copyright. Those thus making or running the covered works
|
|
||||||
for you must do so exclusively on your behalf, under your direction
|
|
||||||
and control, on terms that prohibit them from making any copies of
|
|
||||||
your copyrighted material outside their relationship with you.
|
|
||||||
|
|
||||||
Conveying under any other circumstances is permitted solely under
|
|
||||||
the conditions stated below. Sublicensing is not allowed; section 10
|
|
||||||
makes it unnecessary.
|
|
||||||
|
|
||||||
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law
|
|
||||||
|
|
||||||
No covered work shall be deemed part of an effective technological
|
|
||||||
measure under any applicable law fulfilling obligations under article
|
|
||||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
|
||||||
similar laws prohibiting or restricting circumvention of such
|
|
||||||
measures.
|
|
||||||
|
|
||||||
When you convey a covered work, you waive any legal power to forbid
|
|
||||||
circumvention of technological measures to the extent such circumvention
|
|
||||||
is effected by exercising rights under this License with respect to
|
|
||||||
the covered work, and you disclaim any intention to limit operation or
|
|
||||||
modification of the work as a means of enforcing, against the work's
|
|
||||||
users, your or third parties' legal rights to forbid circumvention of
|
|
||||||
technological measures.
|
|
||||||
|
|
||||||
### 4. Conveying Verbatim Copies
|
|
||||||
|
|
||||||
You may convey verbatim copies of the Program's source code as you
|
|
||||||
receive it, in any medium, provided that you conspicuously and
|
|
||||||
appropriately publish on each copy an appropriate copyright notice;
|
|
||||||
keep intact all notices stating that this License and any
|
|
||||||
non-permissive terms added in accord with section 7 apply to the code;
|
|
||||||
keep intact all notices of the absence of any warranty; and give all
|
|
||||||
recipients a copy of this License along with the Program.
|
|
||||||
|
|
||||||
You may charge any price or no price for each copy that you convey,
|
|
||||||
and you may offer support or warranty protection for a fee.
|
|
||||||
|
|
||||||
### 5. Conveying Modified Source Versions
|
|
||||||
|
|
||||||
You may convey a work based on the Program, or the modifications to
|
|
||||||
produce it from the Program, in the form of source code under the
|
|
||||||
terms of section 4, provided that you also meet all of these conditions:
|
|
||||||
|
|
||||||
* **a)** The work must carry prominent notices stating that you modified
|
|
||||||
it, and giving a relevant date.
|
|
||||||
* **b)** The work must carry prominent notices stating that it is
|
|
||||||
released under this License and any conditions added under section 7.
|
|
||||||
This requirement modifies the requirement in section 4 to
|
|
||||||
“keep intact all notices”.
|
|
||||||
* **c)** You must license the entire work, as a whole, under this
|
|
||||||
License to anyone who comes into possession of a copy. This
|
|
||||||
License will therefore apply, along with any applicable section 7
|
|
||||||
additional terms, to the whole of the work, and all its parts,
|
|
||||||
regardless of how they are packaged. This License gives no
|
|
||||||
permission to license the work in any other way, but it does not
|
|
||||||
invalidate such permission if you have separately received it.
|
|
||||||
* **d)** If the work has interactive user interfaces, each must display
|
|
||||||
Appropriate Legal Notices; however, if the Program has interactive
|
|
||||||
interfaces that do not display Appropriate Legal Notices, your
|
|
||||||
work need not make them do so.
|
|
||||||
|
|
||||||
A compilation of a covered work with other separate and independent
|
|
||||||
works, which are not by their nature extensions of the covered work,
|
|
||||||
and which are not combined with it such as to form a larger program,
|
|
||||||
in or on a volume of a storage or distribution medium, is called an
|
|
||||||
“aggregate” if the compilation and its resulting copyright are not
|
|
||||||
used to limit the access or legal rights of the compilation's users
|
|
||||||
beyond what the individual works permit. Inclusion of a covered work
|
|
||||||
in an aggregate does not cause this License to apply to the other
|
|
||||||
parts of the aggregate.
|
|
||||||
|
|
||||||
### 6. Conveying Non-Source Forms
|
|
||||||
|
|
||||||
You may convey a covered work in object code form under the terms
|
|
||||||
of sections 4 and 5, provided that you also convey the
|
|
||||||
machine-readable Corresponding Source under the terms of this License,
|
|
||||||
in one of these ways:
|
|
||||||
|
|
||||||
* **a)** Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by the
|
|
||||||
Corresponding Source fixed on a durable physical medium
|
|
||||||
customarily used for software interchange.
|
|
||||||
* **b)** Convey the object code in, or embodied in, a physical product
|
|
||||||
(including a physical distribution medium), accompanied by a
|
|
||||||
written offer, valid for at least three years and valid for as
|
|
||||||
long as you offer spare parts or customer support for that product
|
|
||||||
model, to give anyone who possesses the object code either **(1)** a
|
|
||||||
copy of the Corresponding Source for all the software in the
|
|
||||||
product that is covered by this License, on a durable physical
|
|
||||||
medium customarily used for software interchange, for a price no
|
|
||||||
more than your reasonable cost of physically performing this
|
|
||||||
conveying of source, or **(2)** access to copy the
|
|
||||||
Corresponding Source from a network server at no charge.
|
|
||||||
* **c)** Convey individual copies of the object code with a copy of the
|
|
||||||
written offer to provide the Corresponding Source. This
|
|
||||||
alternative is allowed only occasionally and noncommercially, and
|
|
||||||
only if you received the object code with such an offer, in accord
|
|
||||||
with subsection 6b.
|
|
||||||
* **d)** Convey the object code by offering access from a designated
|
|
||||||
place (gratis or for a charge), and offer equivalent access to the
|
|
||||||
Corresponding Source in the same way through the same place at no
|
|
||||||
further charge. You need not require recipients to copy the
|
|
||||||
Corresponding Source along with the object code. If the place to
|
|
||||||
copy the object code is a network server, the Corresponding Source
|
|
||||||
may be on a different server (operated by you or a third party)
|
|
||||||
that supports equivalent copying facilities, provided you maintain
|
|
||||||
clear directions next to the object code saying where to find the
|
|
||||||
Corresponding Source. Regardless of what server hosts the
|
|
||||||
Corresponding Source, you remain obligated to ensure that it is
|
|
||||||
available for as long as needed to satisfy these requirements.
|
|
||||||
* **e)** Convey the object code using peer-to-peer transmission, provided
|
|
||||||
you inform other peers where the object code and Corresponding
|
|
||||||
Source of the work are being offered to the general public at no
|
|
||||||
charge under subsection 6d.
|
|
||||||
|
|
||||||
A separable portion of the object code, whose source code is excluded
|
|
||||||
from the Corresponding Source as a System Library, need not be
|
|
||||||
included in conveying the object code work.
|
|
||||||
|
|
||||||
A “User Product” is either **(1)** a “consumer product”, which means any
|
|
||||||
tangible personal property which is normally used for personal, family,
|
|
||||||
or household purposes, or **(2)** anything designed or sold for incorporation
|
|
||||||
into a dwelling. In determining whether a product is a consumer product,
|
|
||||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
|
||||||
product received by a particular user, “normally used” refers to a
|
|
||||||
typical or common use of that class of product, regardless of the status
|
|
||||||
of the particular user or of the way in which the particular user
|
|
||||||
actually uses, or expects or is expected to use, the product. A product
|
|
||||||
is a consumer product regardless of whether the product has substantial
|
|
||||||
commercial, industrial or non-consumer uses, unless such uses represent
|
|
||||||
the only significant mode of use of the product.
|
|
||||||
|
|
||||||
“Installation Information” for a User Product means any methods,
|
|
||||||
procedures, authorization keys, or other information required to install
|
|
||||||
and execute modified versions of a covered work in that User Product from
|
|
||||||
a modified version of its Corresponding Source. The information must
|
|
||||||
suffice to ensure that the continued functioning of the modified object
|
|
||||||
code is in no case prevented or interfered with solely because
|
|
||||||
modification has been made.
|
|
||||||
|
|
||||||
If you convey an object code work under this section in, or with, or
|
|
||||||
specifically for use in, a User Product, and the conveying occurs as
|
|
||||||
part of a transaction in which the right of possession and use of the
|
|
||||||
User Product is transferred to the recipient in perpetuity or for a
|
|
||||||
fixed term (regardless of how the transaction is characterized), the
|
|
||||||
Corresponding Source conveyed under this section must be accompanied
|
|
||||||
by the Installation Information. But this requirement does not apply
|
|
||||||
if neither you nor any third party retains the ability to install
|
|
||||||
modified object code on the User Product (for example, the work has
|
|
||||||
been installed in ROM).
|
|
||||||
|
|
||||||
The requirement to provide Installation Information does not include a
|
|
||||||
requirement to continue to provide support service, warranty, or updates
|
|
||||||
for a work that has been modified or installed by the recipient, or for
|
|
||||||
the User Product in which it has been modified or installed. Access to a
|
|
||||||
network may be denied when the modification itself materially and
|
|
||||||
adversely affects the operation of the network or violates the rules and
|
|
||||||
protocols for communication across the network.
|
|
||||||
|
|
||||||
Corresponding Source conveyed, and Installation Information provided,
|
|
||||||
in accord with this section must be in a format that is publicly
|
|
||||||
documented (and with an implementation available to the public in
|
|
||||||
source code form), and must require no special password or key for
|
|
||||||
unpacking, reading or copying.
|
|
||||||
|
|
||||||
### 7. Additional Terms
|
|
||||||
|
|
||||||
“Additional permissions” are terms that supplement the terms of this
|
|
||||||
License by making exceptions from one or more of its conditions.
|
|
||||||
Additional permissions that are applicable to the entire Program shall
|
|
||||||
be treated as though they were included in this License, to the extent
|
|
||||||
that they are valid under applicable law. If additional permissions
|
|
||||||
apply only to part of the Program, that part may be used separately
|
|
||||||
under those permissions, but the entire Program remains governed by
|
|
||||||
this License without regard to the additional permissions.
|
|
||||||
|
|
||||||
When you convey a copy of a covered work, you may at your option
|
|
||||||
remove any additional permissions from that copy, or from any part of
|
|
||||||
it. (Additional permissions may be written to require their own
|
|
||||||
removal in certain cases when you modify the work.) You may place
|
|
||||||
additional permissions on material, added by you to a covered work,
|
|
||||||
for which you have or can give appropriate copyright permission.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, for material you
|
|
||||||
add to a covered work, you may (if authorized by the copyright holders of
|
|
||||||
that material) supplement the terms of this License with terms:
|
|
||||||
|
|
||||||
* **a)** Disclaiming warranty or limiting liability differently from the
|
|
||||||
terms of sections 15 and 16 of this License; or
|
|
||||||
* **b)** Requiring preservation of specified reasonable legal notices or
|
|
||||||
author attributions in that material or in the Appropriate Legal
|
|
||||||
Notices displayed by works containing it; or
|
|
||||||
* **c)** Prohibiting misrepresentation of the origin of that material, or
|
|
||||||
requiring that modified versions of such material be marked in
|
|
||||||
reasonable ways as different from the original version; or
|
|
||||||
* **d)** Limiting the use for publicity purposes of names of licensors or
|
|
||||||
authors of the material; or
|
|
||||||
* **e)** Declining to grant rights under trademark law for use of some
|
|
||||||
trade names, trademarks, or service marks; or
|
|
||||||
* **f)** Requiring indemnification of licensors and authors of that
|
|
||||||
material by anyone who conveys the material (or modified versions of
|
|
||||||
it) with contractual assumptions of liability to the recipient, for
|
|
||||||
any liability that these contractual assumptions directly impose on
|
|
||||||
those licensors and authors.
|
|
||||||
|
|
||||||
All other non-permissive additional terms are considered “further
|
|
||||||
restrictions” within the meaning of section 10. If the Program as you
|
|
||||||
received it, or any part of it, contains a notice stating that it is
|
|
||||||
governed by this License along with a term that is a further
|
|
||||||
restriction, you may remove that term. If a license document contains
|
|
||||||
a further restriction but permits relicensing or conveying under this
|
|
||||||
License, you may add to a covered work material governed by the terms
|
|
||||||
of that license document, provided that the further restriction does
|
|
||||||
not survive such relicensing or conveying.
|
|
||||||
|
|
||||||
If you add terms to a covered work in accord with this section, you
|
|
||||||
must place, in the relevant source files, a statement of the
|
|
||||||
additional terms that apply to those files, or a notice indicating
|
|
||||||
where to find the applicable terms.
|
|
||||||
|
|
||||||
Additional terms, permissive or non-permissive, may be stated in the
|
|
||||||
form of a separately written license, or stated as exceptions;
|
|
||||||
the above requirements apply either way.
|
|
||||||
|
|
||||||
### 8. Termination
|
|
||||||
|
|
||||||
You may not propagate or modify a covered work except as expressly
|
|
||||||
provided under this License. Any attempt otherwise to propagate or
|
|
||||||
modify it is void, and will automatically terminate your rights under
|
|
||||||
this License (including any patent licenses granted under the third
|
|
||||||
paragraph of section 11).
|
|
||||||
|
|
||||||
However, if you cease all violation of this License, then your
|
|
||||||
license from a particular copyright holder is reinstated **(a)**
|
|
||||||
provisionally, unless and until the copyright holder explicitly and
|
|
||||||
finally terminates your license, and **(b)** permanently, if the copyright
|
|
||||||
holder fails to notify you of the violation by some reasonable means
|
|
||||||
prior to 60 days after the cessation.
|
|
||||||
|
|
||||||
Moreover, your license from a particular copyright holder is
|
|
||||||
reinstated permanently if the copyright holder notifies you of the
|
|
||||||
violation by some reasonable means, this is the first time you have
|
|
||||||
received notice of violation of this License (for any work) from that
|
|
||||||
copyright holder, and you cure the violation prior to 30 days after
|
|
||||||
your receipt of the notice.
|
|
||||||
|
|
||||||
Termination of your rights under this section does not terminate the
|
|
||||||
licenses of parties who have received copies or rights from you under
|
|
||||||
this License. If your rights have been terminated and not permanently
|
|
||||||
reinstated, you do not qualify to receive new licenses for the same
|
|
||||||
material under section 10.
|
|
||||||
|
|
||||||
### 9. Acceptance Not Required for Having Copies
|
|
||||||
|
|
||||||
You are not required to accept this License in order to receive or
|
|
||||||
run a copy of the Program. Ancillary propagation of a covered work
|
|
||||||
occurring solely as a consequence of using peer-to-peer transmission
|
|
||||||
to receive a copy likewise does not require acceptance. However,
|
|
||||||
nothing other than this License grants you permission to propagate or
|
|
||||||
modify any covered work. These actions infringe copyright if you do
|
|
||||||
not accept this License. Therefore, by modifying or propagating a
|
|
||||||
covered work, you indicate your acceptance of this License to do so.
|
|
||||||
|
|
||||||
### 10. Automatic Licensing of Downstream Recipients
|
|
||||||
|
|
||||||
Each time you convey a covered work, the recipient automatically
|
|
||||||
receives a license from the original licensors, to run, modify and
|
|
||||||
propagate that work, subject to this License. You are not responsible
|
|
||||||
for enforcing compliance by third parties with this License.
|
|
||||||
|
|
||||||
An “entity transaction” is a transaction transferring control of an
|
|
||||||
organization, or substantially all assets of one, or subdividing an
|
|
||||||
organization, or merging organizations. If propagation of a covered
|
|
||||||
work results from an entity transaction, each party to that
|
|
||||||
transaction who receives a copy of the work also receives whatever
|
|
||||||
licenses to the work the party's predecessor in interest had or could
|
|
||||||
give under the previous paragraph, plus a right to possession of the
|
|
||||||
Corresponding Source of the work from the predecessor in interest, if
|
|
||||||
the predecessor has it or can get it with reasonable efforts.
|
|
||||||
|
|
||||||
You may not impose any further restrictions on the exercise of the
|
|
||||||
rights granted or affirmed under this License. For example, you may
|
|
||||||
not impose a license fee, royalty, or other charge for exercise of
|
|
||||||
rights granted under this License, and you may not initiate litigation
|
|
||||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
|
||||||
any patent claim is infringed by making, using, selling, offering for
|
|
||||||
sale, or importing the Program or any portion of it.
|
|
||||||
|
|
||||||
### 11. Patents
|
|
||||||
|
|
||||||
A “contributor” is a copyright holder who authorizes use under this
|
|
||||||
License of the Program or a work on which the Program is based. The
|
|
||||||
work thus licensed is called the contributor's “contributor version”.
|
|
||||||
|
|
||||||
A contributor's “essential patent claims” are all patent claims
|
|
||||||
owned or controlled by the contributor, whether already acquired or
|
|
||||||
hereafter acquired, that would be infringed by some manner, permitted
|
|
||||||
by this License, of making, using, or selling its contributor version,
|
|
||||||
but do not include claims that would be infringed only as a
|
|
||||||
consequence of further modification of the contributor version. For
|
|
||||||
purposes of this definition, “control” includes the right to grant
|
|
||||||
patent sublicenses in a manner consistent with the requirements of
|
|
||||||
this License.
|
|
||||||
|
|
||||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
|
||||||
patent license under the contributor's essential patent claims, to
|
|
||||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
|
||||||
propagate the contents of its contributor version.
|
|
||||||
|
|
||||||
In the following three paragraphs, a “patent license” is any express
|
|
||||||
agreement or commitment, however denominated, not to enforce a patent
|
|
||||||
(such as an express permission to practice a patent or covenant not to
|
|
||||||
sue for patent infringement). To “grant” such a patent license to a
|
|
||||||
party means to make such an agreement or commitment not to enforce a
|
|
||||||
patent against the party.
|
|
||||||
|
|
||||||
If you convey a covered work, knowingly relying on a patent license,
|
|
||||||
and the Corresponding Source of the work is not available for anyone
|
|
||||||
to copy, free of charge and under the terms of this License, through a
|
|
||||||
publicly available network server or other readily accessible means,
|
|
||||||
then you must either **(1)** cause the Corresponding Source to be so
|
|
||||||
available, or **(2)** arrange to deprive yourself of the benefit of the
|
|
||||||
patent license for this particular work, or **(3)** arrange, in a manner
|
|
||||||
consistent with the requirements of this License, to extend the patent
|
|
||||||
license to downstream recipients. “Knowingly relying” means you have
|
|
||||||
actual knowledge that, but for the patent license, your conveying the
|
|
||||||
covered work in a country, or your recipient's use of the covered work
|
|
||||||
in a country, would infringe one or more identifiable patents in that
|
|
||||||
country that you have reason to believe are valid.
|
|
||||||
|
|
||||||
If, pursuant to or in connection with a single transaction or
|
|
||||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
|
||||||
covered work, and grant a patent license to some of the parties
|
|
||||||
receiving the covered work authorizing them to use, propagate, modify
|
|
||||||
or convey a specific copy of the covered work, then the patent license
|
|
||||||
you grant is automatically extended to all recipients of the covered
|
|
||||||
work and works based on it.
|
|
||||||
|
|
||||||
A patent license is “discriminatory” if it does not include within
|
|
||||||
the scope of its coverage, prohibits the exercise of, or is
|
|
||||||
conditioned on the non-exercise of one or more of the rights that are
|
|
||||||
specifically granted under this License. You may not convey a covered
|
|
||||||
work if you are a party to an arrangement with a third party that is
|
|
||||||
in the business of distributing software, under which you make payment
|
|
||||||
to the third party based on the extent of your activity of conveying
|
|
||||||
the work, and under which the third party grants, to any of the
|
|
||||||
parties who would receive the covered work from you, a discriminatory
|
|
||||||
patent license **(a)** in connection with copies of the covered work
|
|
||||||
conveyed by you (or copies made from those copies), or **(b)** primarily
|
|
||||||
for and in connection with specific products or compilations that
|
|
||||||
contain the covered work, unless you entered into that arrangement,
|
|
||||||
or that patent license was granted, prior to 28 March 2007.
|
|
||||||
|
|
||||||
Nothing in this License shall be construed as excluding or limiting
|
|
||||||
any implied license or other defenses to infringement that may
|
|
||||||
otherwise be available to you under applicable patent law.
|
|
||||||
|
|
||||||
### 12. No Surrender of Others' Freedom
|
|
||||||
|
|
||||||
If conditions are imposed on you (whether by court order, agreement or
|
|
||||||
otherwise) that contradict the conditions of this License, they do not
|
|
||||||
excuse you from the conditions of this License. If you cannot convey a
|
|
||||||
covered work so as to satisfy simultaneously your obligations under this
|
|
||||||
License and any other pertinent obligations, then as a consequence you may
|
|
||||||
not convey it at all. For example, if you agree to terms that obligate you
|
|
||||||
to collect a royalty for further conveying from those to whom you convey
|
|
||||||
the Program, the only way you could satisfy both those terms and this
|
|
||||||
License would be to refrain entirely from conveying the Program.
|
|
||||||
|
|
||||||
### 13. Remote Network Interaction; Use with the GNU General Public License
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, if you modify the
|
|
||||||
Program, your modified version must prominently offer all users
|
|
||||||
interacting with it remotely through a computer network (if your version
|
|
||||||
supports such interaction) an opportunity to receive the Corresponding
|
|
||||||
Source of your version by providing access to the Corresponding Source
|
|
||||||
from a network server at no charge, through some standard or customary
|
|
||||||
means of facilitating copying of software. This Corresponding Source
|
|
||||||
shall include the Corresponding Source for any work covered by version 3
|
|
||||||
of the GNU General Public License that is incorporated pursuant to the
|
|
||||||
following paragraph.
|
|
||||||
|
|
||||||
Notwithstanding any other provision of this License, you have
|
|
||||||
permission to link or combine any covered work with a work licensed
|
|
||||||
under version 3 of the GNU General Public License into a single
|
|
||||||
combined work, and to convey the resulting work. The terms of this
|
|
||||||
License will continue to apply to the part which is the covered work,
|
|
||||||
but the work with which it is combined will remain governed by version
|
|
||||||
3 of the GNU General Public License.
|
|
||||||
|
|
||||||
### 14. Revised Versions of this License
|
|
||||||
|
|
||||||
The Free Software Foundation may publish revised and/or new versions of
|
|
||||||
the GNU Affero General Public License from time to time. Such new versions
|
|
||||||
will be similar in spirit to the present version, but may differ in detail to
|
|
||||||
address new problems or concerns.
|
|
||||||
|
|
||||||
Each version is given a distinguishing version number. If the
|
|
||||||
Program specifies that a certain numbered version of the GNU Affero General
|
|
||||||
Public License “or any later version” applies to it, you have the
|
|
||||||
option of following the terms and conditions either of that numbered
|
|
||||||
version or of any later version published by the Free Software
|
|
||||||
Foundation. If the Program does not specify a version number of the
|
|
||||||
GNU Affero General Public License, you may choose any version ever published
|
|
||||||
by the Free Software Foundation.
|
|
||||||
|
|
||||||
If the Program specifies that a proxy can decide which future
|
|
||||||
versions of the GNU Affero General Public License can be used, that proxy's
|
|
||||||
public statement of acceptance of a version permanently authorizes you
|
|
||||||
to choose that version for the Program.
|
|
||||||
|
|
||||||
Later license versions may give you additional or different
|
|
||||||
permissions. However, no additional obligations are imposed on any
|
|
||||||
author or copyright holder as a result of your choosing to follow a
|
|
||||||
later version.
|
|
||||||
|
|
||||||
### 15. Disclaimer of Warranty
|
|
||||||
|
|
||||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
|
||||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
|
||||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY
|
|
||||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
|
||||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
||||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
|
||||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
|
||||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
|
||||||
|
|
||||||
### 16. Limitation of Liability
|
|
||||||
|
|
||||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
|
||||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
|
||||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
|
||||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
|
||||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
|
||||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
|
||||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
|
||||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
|
||||||
SUCH DAMAGES.
|
|
||||||
|
|
||||||
### 17. Interpretation of Sections 15 and 16
|
|
||||||
|
|
||||||
If the disclaimer of warranty and limitation of liability provided
|
|
||||||
above cannot be given local legal effect according to their terms,
|
|
||||||
reviewing courts shall apply local law that most closely approximates
|
|
||||||
an absolute waiver of all civil liability in connection with the
|
|
||||||
Program, unless a warranty or assumption of liability accompanies a
|
|
||||||
copy of the Program in return for a fee.
|
|
||||||
|
|
||||||
_END OF TERMS AND CONDITIONS_
|
|
||||||
|
|
||||||
## How to Apply These Terms to Your New Programs
|
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest
|
|
||||||
possible use to the public, the best way to achieve this is to make it
|
|
||||||
free software which everyone can redistribute and change under these terms.
|
|
||||||
|
|
||||||
To do so, attach the following notices to the program. It is safest
|
|
||||||
to attach them to the start of each source file to most effectively
|
|
||||||
state the exclusion of warranty; and each file should have at least
|
|
||||||
the “copyright” line and a pointer to where the full notice is found.
|
|
||||||
|
|
||||||
<one line to give the program's name and a brief idea of what it does.>
|
|
||||||
Copyright (C) <year> <name of author>
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU Affero General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU Affero General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU Affero General Public License
|
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
|
||||||
|
|
||||||
If your software can interact with users remotely through a computer
|
|
||||||
network, you should also make sure that it provides a way for users to
|
|
||||||
get its source. For example, if your program is a web application, its
|
|
||||||
interface could display a “Source” link that leads users to an archive
|
|
||||||
of the code. There are many ways you could offer source, and different
|
|
||||||
solutions will be better for different programs; see section 13 for the
|
|
||||||
specific requirements.
|
|
||||||
|
|
||||||
You should also get your employer (if you work as a programmer) or school,
|
|
||||||
if any, to sign a “copyright disclaimer” for the program, if necessary.
|
|
||||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
|
||||||
<<http://www.gnu.org/licenses/>>.
|
|
||||||
|
|
24
README.md
24
README.md
|
@ -1,28 +1,32 @@
|
||||||
|
|
||||||
[](https://crates.io/crates/axum-folder-router)
|
[](https://crates.io/crates/axum-folder-router)
|
||||||
[](https://docs.rs/axum-folder-router)
|
[](https://docs.rs/axum-folder-router)
|
||||||

|

|
||||||
[](https://www.gnu.org/licenses/agpl-3.0)
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
|
||||||
# axum-folder-router
|
# axum-folder-router
|
||||||
|
|
||||||
```folder_router``` is a procedural macro for the Axum web framework that automatically generates router boilerplate based on your file structure. It simplifies route organization by using filesystem conventions to define your API routes.
|
```#[folder_router(...)]``` is a procedural attribute macro for the Axum web framework that automatically generates router boilerplate based on your direcory & file structure.
|
||||||
|
Inspired by popular frameworks like next.js.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **File System-Based Routing**: Define your API routes using intuitive folder structures
|
||||||
|
- **Reduced Boilerplate**: Automatically generates route mapping code
|
||||||
|
- **IDE Support**: Generates proper module imports for better rust-analyzer integration
|
||||||
|
- **Multiple Routers**: Create separate folder-based routers in the same application
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
See [the examples](./examples) or [docs.rs](https://docs.rs/axum-folder-router).
|
For detailed instructions see [the examples](./examples) or [docs.rs](https://docs.rs/axum-folder-router).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This repository, like all my personal projects, is licensed under the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later).
|
This repository, is licensed permissively under the terms of the MIT license.
|
||||||
This ensures that modifications to the code remain open source when used in network services.
|
|
||||||
Contact me if this doesn't suit your needs.
|
|
||||||
|
|
||||||
### Contribution
|
### Contribution
|
||||||
|
|
||||||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the GNU Affero General Public License v3.0 or later (AGPL-3.0-or-later) license, shall be licensed as above, without any additional terms or conditions.
|
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, shall be licensed as above, without any additional terms or conditions.
|
||||||
|
|
||||||
|
|
||||||
### Attribution
|
### Attribution
|
||||||
|
|
||||||
|
|
5
examples/advanced/api/files/[...path]/route.rs
Normal file
5
examples/advanced/api/files/[...path]/route.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
use axum::{extract::Path, response::IntoResponse};
|
||||||
|
|
||||||
|
pub async fn get(Path(path): Path<String>) -> impl IntoResponse {
|
||||||
|
format!("Requested file path: {path}")
|
||||||
|
}
|
9
examples/advanced/api/files/route.rs
Normal file
9
examples/advanced/api/files/route.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
12
examples/advanced/api/ping/route.rs
Normal file
12
examples/advanced/api/ping/route.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
use axum::response::Html;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>GET Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
// This tests that our macro generates the routes in the correct order
|
||||||
|
// as any is only allowable as a first route.
|
||||||
|
pub async fn any() -> impl IntoResponse {
|
||||||
|
Html("<h1>ANY Pong!</h1>").into_response()
|
||||||
|
}
|
9
examples/advanced/api/route.rs
Normal file
9
examples/advanced/api/route.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
5
examples/advanced/api/users/[id]/route.rs
Normal file
5
examples/advanced/api/users/[id]/route.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
use axum::{extract::Path, response::IntoResponse};
|
||||||
|
|
||||||
|
pub async fn get(Path(id): Path<String>) -> impl IntoResponse {
|
||||||
|
format!("User ID: {id}")
|
||||||
|
}
|
9
examples/advanced/api/users/route.rs
Normal file
9
examples/advanced/api/users/route.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
7
examples/advanced/main.rs
Normal file
7
examples/advanced/main.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
mod server;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
server::server().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
29
examples/advanced/server.rs
Normal file
29
examples/advanced/server.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use axum::Router;
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct AppState {
|
||||||
|
_foo: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Imports route.rs files & generates an ::into_router() fn
|
||||||
|
#[folder_router("examples/advanced/api", AppState)]
|
||||||
|
struct MyFolderRouter();
|
||||||
|
|
||||||
|
pub async fn server() -> anyhow::Result<()> {
|
||||||
|
// Create app state
|
||||||
|
let app_state = AppState {
|
||||||
|
_foo: String::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Use the init fn generated above
|
||||||
|
let folder_router: Router<AppState> = MyFolderRouter::into_router();
|
||||||
|
|
||||||
|
// Build the router and provide the state
|
||||||
|
let app: Router<()> = folder_router.with_state(app_state);
|
||||||
|
|
||||||
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
|
||||||
|
println!("Listening on http://{}", listener.local_addr()?);
|
||||||
|
axum::serve(listener, app).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -3,8 +3,3 @@ use axum::response::{Html, IntoResponse};
|
||||||
pub async fn get() -> impl IntoResponse {
|
pub async fn get() -> impl IntoResponse {
|
||||||
Html("<h1>Hello World!</h1>").into_response()
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post() -> impl IntoResponse {
|
|
||||||
"Posted successfully".into_response()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
use anyhow;
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
use axum_folder_router::folder_router;
|
use axum_folder_router::folder_router;
|
||||||
use tokio;
|
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone)]
|
||||||
struct AppState {
|
struct AppState;
|
||||||
_foo: String,
|
|
||||||
}
|
// Imports route.rs files & generates an ::into_router() fn
|
||||||
|
#[folder_router("./examples/simple/api", AppState)]
|
||||||
|
struct MyFolderRouter();
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Create app state
|
// Create app state
|
||||||
let app_state = AppState {
|
let app_state = AppState;
|
||||||
_foo: "".to_string(),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Generate the router using the macro
|
// Use the init fn generated above
|
||||||
let folder_router: Router<AppState> = folder_router!("./examples/simple/api", AppState);
|
let folder_router: Router<AppState> = MyFolderRouter::into_router();
|
||||||
|
|
||||||
// Build the router and provide the state
|
// Build the router and provide the state
|
||||||
let app: Router<()> = folder_router.with_state(app_state);
|
let app: Router<()> = folder_router.with_state(app_state);
|
||||||
|
|
|
@ -72,7 +72,8 @@
|
||||||
cargo-udeps
|
cargo-udeps
|
||||||
cargo-outdated
|
cargo-outdated
|
||||||
cargo-release
|
cargo-release
|
||||||
cargo-readme
|
cargo-readme
|
||||||
|
cargo-expand
|
||||||
calc
|
calc
|
||||||
fish
|
fish
|
||||||
inotify-tools
|
inotify-tools
|
||||||
|
@ -93,7 +94,8 @@
|
||||||
packages = {
|
packages = {
|
||||||
# default = pkgs.callPackage ./package.nix { };
|
# default = pkgs.callPackage ./package.nix { };
|
||||||
};
|
};
|
||||||
}) // {
|
})
|
||||||
|
// {
|
||||||
hydraJobs =
|
hydraJobs =
|
||||||
let
|
let
|
||||||
system = "x86_64-linux";
|
system = "x86_64-linux";
|
||||||
|
|
23
rustfmt.toml
Normal file
23
rustfmt.toml
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
|
||||||
|
edition = "2024"
|
||||||
|
max_width = 100
|
||||||
|
tab_spaces = 4
|
||||||
|
|
||||||
|
|
||||||
|
# unstable
|
||||||
|
format_macro_bodies = true
|
||||||
|
format_macro_matchers = true
|
||||||
|
format_strings = true
|
||||||
|
group_imports = "StdExternalCrate"
|
||||||
|
imports_granularity = "Crate"
|
||||||
|
imports_layout = "HorizontalVertical"
|
||||||
|
overflow_delimited_expr = true
|
||||||
|
reorder_impl_items = true
|
||||||
|
struct_field_align_threshold = 4
|
||||||
|
struct_lit_single_line = false
|
||||||
|
trailing_comma = "Vertical"
|
||||||
|
unstable_features = true
|
||||||
|
use_field_init_shorthand = true
|
||||||
|
use_try_shorthand = true
|
||||||
|
wrap_comments = true
|
||||||
|
# format_brace_macros = true
|
234
src/generate.rs
Normal file
234
src/generate.rs
Normal file
|
@ -0,0 +1,234 @@
|
||||||
|
use std::{collections::BTreeMap, fmt::Write, path::Path};
|
||||||
|
|
||||||
|
use proc_macro2::TokenStream;
|
||||||
|
use quote::{format_ident, quote};
|
||||||
|
use syn::LitStr;
|
||||||
|
|
||||||
|
use crate::parse::methods_for_route;
|
||||||
|
|
||||||
|
// A struct representing a directory in the module tree
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct ModuleDir {
|
||||||
|
name: String,
|
||||||
|
has_route: bool,
|
||||||
|
children: BTreeMap<String, ModuleDir>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ModuleDir {
|
||||||
|
fn new(name: &str) -> Self {
|
||||||
|
ModuleDir {
|
||||||
|
name: name.to_string(),
|
||||||
|
has_route: false,
|
||||||
|
children: BTreeMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_to_module_tree(&mut self, rel_path: &Path, _route_path: &Path) {
|
||||||
|
let components: Vec<_> = rel_path
|
||||||
|
.components()
|
||||||
|
.map(|c| c.as_os_str().to_string_lossy().to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if components.is_empty() {
|
||||||
|
self.has_route = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut root = self;
|
||||||
|
|
||||||
|
for (i, segment) in components.iter().enumerate() {
|
||||||
|
if i == components.len() - 1 && segment == "route.rs" {
|
||||||
|
root.has_route = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
root = root
|
||||||
|
.children
|
||||||
|
.entry(segment.clone())
|
||||||
|
.or_insert_with(|| ModuleDir::new(segment));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a route to the module tree
|
||||||
|
|
||||||
|
// Normalize a path segment for use as a module name
|
||||||
|
fn normalize_module_name(name: &str) -> String {
|
||||||
|
if name.starts_with('[') && name.ends_with(']') {
|
||||||
|
let inner = &name[1..name.len() - 1];
|
||||||
|
if let Some(stripped) = inner.strip_prefix("...") {
|
||||||
|
format!("___{stripped}")
|
||||||
|
} else {
|
||||||
|
format!("__{inner}")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
name.replace(['-', '.'], "_")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert a relative path to module path segments and axum route path
|
||||||
|
fn path_to_module_path(rel_path: &Path) -> (String, Vec<String>) {
|
||||||
|
let mut axum_path = String::new();
|
||||||
|
let mut mod_path = Vec::new();
|
||||||
|
|
||||||
|
let components: Vec<_> = rel_path
|
||||||
|
.components()
|
||||||
|
.map(|c| c.as_os_str().to_string_lossy().to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// Handle root route
|
||||||
|
if components.is_empty() {
|
||||||
|
return ("/".to_string(), vec!["route".to_string()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i, segment) in components.iter().enumerate() {
|
||||||
|
if i == components.len() - 1 && segment == "route.rs" {
|
||||||
|
mod_path.push("route".to_string());
|
||||||
|
} else {
|
||||||
|
// Process directory name
|
||||||
|
let normalized = normalize_module_name(segment);
|
||||||
|
mod_path.push(normalized);
|
||||||
|
|
||||||
|
// Process URL path
|
||||||
|
if segment.starts_with('[') && segment.ends_with(']') {
|
||||||
|
let param = &segment[1..segment.len() - 1];
|
||||||
|
if let Some(stripped) = param.strip_prefix("...") {
|
||||||
|
write!(&mut axum_path, "/{{*{stripped}}}").unwrap();
|
||||||
|
} else {
|
||||||
|
write!(&mut axum_path, "/{{:{param}}}").unwrap();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
write!(&mut axum_path, "/{segment}").unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if axum_path.is_empty() {
|
||||||
|
axum_path = "/".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
(axum_path, mod_path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate tokens for a module path
|
||||||
|
fn generate_mod_path_tokens(mod_path: &[String]) -> TokenStream {
|
||||||
|
let mut result = TokenStream::new();
|
||||||
|
|
||||||
|
for (i, segment) in mod_path.iter().enumerate() {
|
||||||
|
let segment_ident = format_ident!("{}", segment);
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
result = quote! { #segment_ident };
|
||||||
|
} else {
|
||||||
|
result = quote! { #result::#segment_ident };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate module hierarchy code
|
||||||
|
fn generate_module_hierarchy(dir: &ModuleDir) -> TokenStream {
|
||||||
|
let mut result = TokenStream::new();
|
||||||
|
|
||||||
|
// Add route.rs module if this directory has one
|
||||||
|
if dir.has_route {
|
||||||
|
let route_mod = quote! {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route;
|
||||||
|
};
|
||||||
|
result.extend(route_mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add subdirectories
|
||||||
|
for child in dir.children.values() {
|
||||||
|
let child_name = format_ident!("{}", normalize_module_name(&child.name));
|
||||||
|
let child_path_lit = LitStr::new(&child.name, proc_macro2::Span::call_site());
|
||||||
|
let child_content = generate_module_hierarchy(child);
|
||||||
|
|
||||||
|
let child_mod = quote! {
|
||||||
|
#[path = #child_path_lit]
|
||||||
|
pub mod #child_name {
|
||||||
|
#child_content
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
result.extend(child_mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn route_registrations(
|
||||||
|
root_namespace_str: &str,
|
||||||
|
routes: &Vec<(std::path::PathBuf, std::path::PathBuf)>,
|
||||||
|
) -> TokenStream {
|
||||||
|
let root_namespace_ident = format_ident!("{}", root_namespace_str);
|
||||||
|
|
||||||
|
let mut route_registrations = Vec::new();
|
||||||
|
for (route_path, rel_path) in routes {
|
||||||
|
// Generate module path and axum path
|
||||||
|
let (axum_path, mod_path) = path_to_module_path(rel_path);
|
||||||
|
|
||||||
|
let method_registrations = methods_for_route(route_path);
|
||||||
|
|
||||||
|
if !method_registrations.is_empty() {
|
||||||
|
let first_method = &method_registrations[0];
|
||||||
|
let first_method_ident = format_ident!("{}", first_method);
|
||||||
|
|
||||||
|
let mod_path_tokens = generate_mod_path_tokens(&mod_path);
|
||||||
|
|
||||||
|
let mut builder = quote! {
|
||||||
|
axum::routing::#first_method_ident(#root_namespace_ident::#mod_path_tokens::#first_method_ident)
|
||||||
|
};
|
||||||
|
|
||||||
|
for method in &method_registrations[1..] {
|
||||||
|
let method_ident = format_ident!("{}", method);
|
||||||
|
|
||||||
|
builder = quote! {
|
||||||
|
#builder.#method_ident(#root_namespace_ident::#mod_path_tokens::#method_ident)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let registration = quote! {
|
||||||
|
router = router.route(#axum_path, #builder);
|
||||||
|
};
|
||||||
|
route_registrations.push(registration);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if route_registrations.is_empty() {
|
||||||
|
return quote! {
|
||||||
|
compile_error!(concat!(
|
||||||
|
"No routes defined in your route.rs's !\n",
|
||||||
|
"Ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete)"
|
||||||
|
));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TokenStream::from_iter(route_registrations)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn module_tree(
|
||||||
|
root_namespace_str: &str,
|
||||||
|
base_dir: &Path,
|
||||||
|
routes: &Vec<(std::path::PathBuf, std::path::PathBuf)>,
|
||||||
|
) -> TokenStream {
|
||||||
|
let root_namespace_ident = format_ident!("{}", root_namespace_str);
|
||||||
|
let base_path_lit = LitStr::new(
|
||||||
|
base_dir.to_str().unwrap_or("./"),
|
||||||
|
proc_macro2::Span::call_site(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut root = ModuleDir::new(root_namespace_str);
|
||||||
|
for (route_path, rel_path) in routes {
|
||||||
|
root.add_to_module_tree(rel_path, route_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mod_hierarchy = generate_module_hierarchy(&root);
|
||||||
|
quote! {
|
||||||
|
#[path = #base_path_lit]
|
||||||
|
mod #root_namespace_ident {
|
||||||
|
#mod_hierarchy
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
267
src/lib.rs
267
src/lib.rs
|
@ -1,6 +1,9 @@
|
||||||
//! # ```axum_folder_router``` Macro Documentation
|
//! # ```axum_folder_router``` Macro Documentation
|
||||||
//!
|
//!
|
||||||
//! [folder_router!] is a procedural macro for the Axum web framework that automatically generates router boilerplate based on your file structure. It simplifies route organization by using filesystem conventions to define your API routes.
|
//! [macro@folder_router] is a procedural macro for the Axum web framework that
|
||||||
|
//! automatically generates router boilerplate based on your file structure. It
|
||||||
|
//! simplifies route organization by using filesystem conventions to define your
|
||||||
|
//! API routes.
|
||||||
//!
|
//!
|
||||||
//! ## Installation
|
//! ## Installation
|
||||||
//!
|
//!
|
||||||
|
@ -8,22 +11,22 @@
|
||||||
//!
|
//!
|
||||||
//! ```toml
|
//! ```toml
|
||||||
//! [dependencies]
|
//! [dependencies]
|
||||||
//! axum_folder_router = "0.1.0"
|
//! axum_folder_router = "0.3"
|
||||||
//! axum = "0.7"
|
//! axum = "0.8"
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Basic Usage
|
//! ## Basic Usage
|
||||||
//!
|
//!
|
||||||
//! The macro scans a directory for ```route.rs``` files and automatically creates an Axum router based on the file structure:
|
//! The macro scans a directory for ```route.rs``` files and automatically
|
||||||
|
//! creates an Axum router based on the file structure:
|
||||||
//!
|
//!
|
||||||
//! ```rust,no_run
|
//! ```rust,no_run
|
||||||
#![doc = include_str!("../examples/simple/main.rs")]
|
#![doc = include_str!("../examples/simple/main.rs")]
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## File Structure Convention
|
//! ## File Structure Convention
|
||||||
//!
|
//!
|
||||||
//! The macro converts your file structure into routes:
|
//! The macro converts your file structure into routes:
|
||||||
//!
|
|
||||||
//! ```text
|
//! ```text
|
||||||
//! src/api/
|
//! src/api/
|
||||||
//! ├── route.rs -> "/"
|
//! ├── route.rs -> "/"
|
||||||
|
@ -37,22 +40,21 @@
|
||||||
//! └── [...path]/
|
//! └── [...path]/
|
||||||
//! └── route.rs -> "/files/*path"
|
//! └── route.rs -> "/files/*path"
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Each ```route.rs``` file can contain HTTP method handlers that are automatically mapped to the corresponding route.
|
//! Each ```route.rs``` file can contain HTTP method handlers that are automatically mapped to the corresponding route.
|
||||||
//!
|
//!
|
||||||
//! ## Route Handlers
|
//! ## Route Handlers
|
||||||
//!
|
//!
|
||||||
//! Inside each ```route.rs``` file, define async functions named after HTTP methods:
|
//! Inside each ```route.rs``` file, define async functions named after HTTP methods:
|
||||||
//!
|
|
||||||
//! ```rust
|
//! ```rust
|
||||||
#![doc = include_str!("../examples/simple/api/route.rs")]
|
#![doc = include_str!("../examples/simple/api/route.rs")]
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Supported Features
|
//! ## Supported Features
|
||||||
//!
|
//!
|
||||||
//! ### HTTP Methods
|
//! ### HTTP Methods
|
||||||
//!
|
//!
|
||||||
//! The macro supports all standard HTTP methods:
|
//! The macro supports all standard HTTP methods as defined in RFC9110.
|
||||||
//! - ```get```
|
//! - ```get```
|
||||||
//! - ```post```
|
//! - ```post```
|
||||||
//! - ```put```
|
//! - ```put```
|
||||||
|
@ -60,17 +62,20 @@
|
||||||
//! - ```patch```
|
//! - ```patch```
|
||||||
//! - ```head```
|
//! - ```head```
|
||||||
//! - ```options```
|
//! - ```options```
|
||||||
|
//! - ```trace```
|
||||||
|
//! - ```connect```
|
||||||
|
//!
|
||||||
|
//! And additionally
|
||||||
|
//! - ```any```, which maches all methods
|
||||||
//!
|
//!
|
||||||
//! ### Path Parameters
|
//! ### Path Parameters
|
||||||
//!
|
//!
|
||||||
//! Dynamic path segments are defined using brackets:
|
//! Dynamic path segments are defined using brackets:
|
||||||
//!
|
|
||||||
//! ```text
|
//! ```text
|
||||||
//! src/api/users/[id]/route.rs -> "/users/{id}"
|
//! src/api/users/[id]/route.rs -> "/users/{id}"
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Inside the route handler:
|
//! Inside the route handler:
|
||||||
//!
|
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use axum::{
|
//! use axum::{
|
||||||
//! extract::Path,
|
//! extract::Path,
|
||||||
|
@ -81,15 +86,13 @@
|
||||||
//! format!("User ID: {}", id)
|
//! format!("User ID: {}", id)
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ### Catch-all Parameters
|
//! ### Catch-all Parameters
|
||||||
//!
|
//!
|
||||||
//! Use the spread syntax for catch-all segments:
|
//! Use the spread syntax for catch-all segments:
|
||||||
//!
|
|
||||||
//! ```text
|
//! ```text
|
||||||
//! src/api/files/[...path]/route.rs -> "/files/*path"
|
//! src/api/files/[...path]/route.rs -> "/files/*path"
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use axum::{
|
//! use axum::{
|
||||||
//! extract::Path,
|
//! extract::Path,
|
||||||
|
@ -100,11 +103,11 @@
|
||||||
//! format!("Requested file path: {}", path)
|
//! format!("Requested file path: {}", path)
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ### State Extraction
|
//! ### State Extraction
|
||||||
//!
|
//!
|
||||||
//! The state type provided to the macro is available in all route handlers:
|
//! The state type provided to the macro is available in all route handlers:
|
||||||
//!
|
//! All routes share the same state type, though you can use ```FromRef``` for more granular state extraction.
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use axum::{
|
//! use axum::{
|
||||||
//! extract::State,
|
//! extract::State,
|
||||||
|
@ -118,190 +121,102 @@
|
||||||
//! format!("State: {:?}", state)
|
//! format!("State: {:?}", state)
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! ## Limitations
|
//! ## Limitations
|
||||||
//!
|
//!
|
||||||
//! - **Compile-time Only**: The routing is determined at compile time, so dynamic route registration isn't supported.
|
//! - **Compile-time Only**: The routing is determined at compile time, so dynamic route registration isn't supported.
|
||||||
//! - **File I/O**: The macro performs file I/O during compilation, which may have implications in certain build environments.
|
//! - **Expects seperate directory**: To make rust-analyzer & co work correctly the macro imports all route.rs files inside the given directory tree.
|
||||||
//! - **Single State Type**: All routes share the same state type, though you can use ```FromRef``` for more granular state extraction.
|
//! It is highly recommended to keep the route directory seperate from the rest of your module-tree.
|
||||||
//!
|
use std::path::Path;
|
||||||
//! ## Best Practices
|
|
||||||
//!
|
|
||||||
//! 1. **Consistent Structure**: Maintain a consistent file structure to make your API organization predictable.
|
|
||||||
//! 2. **Individual Route Files**: Use one ```route.rs``` file per route path for clarity.
|
|
||||||
//! 3. **Module Organization**: Consider grouping related functionality in directories.
|
|
||||||
//! 4. **Documentation**: Add comments to each route handler explaining its purpose.
|
|
||||||
|
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::{format_ident, quote};
|
use quote::quote;
|
||||||
use std::fs;
|
use syn::parse_macro_input;
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use syn::{Ident, LitStr, Result, Token, parse::Parse, parse::ParseStream, parse_macro_input};
|
|
||||||
|
|
||||||
struct FolderRouterArgs {
|
mod generate;
|
||||||
path: String,
|
mod parse;
|
||||||
state_type: Ident,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Parse for FolderRouterArgs {
|
/// Creates an Axum router module tree & creation function
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
/// by scanning a directory for `route.rs` files.
|
||||||
let path_lit = input.parse::<LitStr>()?;
|
|
||||||
input.parse::<Token![,]>()?;
|
|
||||||
let state_type = input.parse::<Ident>()?;
|
|
||||||
|
|
||||||
Ok(FolderRouterArgs {
|
|
||||||
path: path_lit.value(),
|
|
||||||
state_type,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates an Axum router by scanning a directory for `route.rs` files.
|
|
||||||
///
|
///
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// * `path` - A string literal pointing to the API directory, relative to the Cargo manifest directory
|
/// * `path` - A string literal pointing to the route directory, relative to the
|
||||||
/// * `state_type` - The type name of your application state that will be shared across all routes
|
/// Cargo manifest directory
|
||||||
///
|
/// * `state_type` - The type name of your application state that will be shared
|
||||||
/// # Example
|
/// across all routes
|
||||||
///
|
#[allow(clippy::missing_panics_doc)]
|
||||||
/// ```rust
|
#[proc_macro_attribute]
|
||||||
/// # use axum_folder_router::folder_router;
|
pub fn folder_router(attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||||
/// # #[derive(Debug, Clone)]
|
let args = parse_macro_input!(attr as parse::FolderRouterArgs);
|
||||||
/// # struct AppState ();
|
let input_item = parse_macro_input!(item as syn::ItemStruct);
|
||||||
/// #
|
let struct_name = &input_item.ident;
|
||||||
/// let router = folder_router!("./src/api", AppState);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
/// This will scan all `route.rs` files in the `./src/api` directory and its subdirectories,
|
|
||||||
/// automatically mapping their path structure to URL routes with the specified state type.
|
|
||||||
#[proc_macro]
|
|
||||||
pub fn folder_router(input: TokenStream) -> TokenStream {
|
|
||||||
let args = parse_macro_input!(input as FolderRouterArgs);
|
|
||||||
let base_path = args.path;
|
let base_path = args.path;
|
||||||
let state_type = args.state_type;
|
let state_type = args.state_type;
|
||||||
|
|
||||||
// Get the project root directory
|
let manifest_dir = get_manifest_dir();
|
||||||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
|
|
||||||
let base_dir = Path::new(&manifest_dir).join(&base_path);
|
let base_dir = Path::new(&manifest_dir).join(&base_path);
|
||||||
|
|
||||||
// Collect route files
|
let mod_namespace = format!(
|
||||||
let mut routes = Vec::new();
|
"__folder_router__{}__{}",
|
||||||
collect_route_files(&base_dir, &base_dir, &mut routes);
|
struct_name
|
||||||
|
.to_string()
|
||||||
|
.chars()
|
||||||
|
.map(|c| if c.is_alphanumeric() { c } else { '_' })
|
||||||
|
.map(|c| c.to_ascii_lowercase())
|
||||||
|
.collect::<String>(),
|
||||||
|
base_path
|
||||||
|
.chars()
|
||||||
|
.map(|c| if c.is_alphanumeric() { c } else { '_' })
|
||||||
|
.collect::<String>()
|
||||||
|
);
|
||||||
|
|
||||||
// Generate module definitions and route registrations
|
let routes = parse::collect_route_files(&base_dir, &base_dir);
|
||||||
let mut module_defs = Vec::new();
|
|
||||||
let mut route_registrations = Vec::new();
|
|
||||||
|
|
||||||
for (route_path, rel_path) in routes {
|
if routes.is_empty() {
|
||||||
// Generate module name and axum path
|
return TokenStream::from(quote! {
|
||||||
let (axum_path, mod_name) = path_to_route_info(&rel_path);
|
compile_error!(concat!("No route.rs files found in the specified directory: '",
|
||||||
let mod_ident = format_ident!("{}", mod_name);
|
#base_path,
|
||||||
|
"'. Make sure the path is correct and contains route.rs files."
|
||||||
// Create module path for include!
|
));
|
||||||
let rel_file_path = route_path.strip_prefix(&manifest_dir).unwrap();
|
|
||||||
let rel_file_str = rel_file_path.to_string_lossy().to_string();
|
|
||||||
|
|
||||||
// Add module definition
|
|
||||||
module_defs.push(quote! {
|
|
||||||
#[allow(warnings)]
|
|
||||||
mod #mod_ident {
|
|
||||||
include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", #rel_file_str));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Read the file content to find HTTP methods
|
|
||||||
let file_content = fs::read_to_string(&route_path).unwrap_or_default();
|
|
||||||
let methods = ["get", "post", "put", "delete", "patch", "head", "options"];
|
|
||||||
|
|
||||||
let mut method_registrations = Vec::new();
|
|
||||||
for method in &methods {
|
|
||||||
if file_content.contains(&format!("pub async fn {}(", method)) {
|
|
||||||
let method_ident = format_ident!("{}", method);
|
|
||||||
method_registrations.push((method, method_ident));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !method_registrations.is_empty() {
|
|
||||||
let (_first_method, first_method_ident) = &method_registrations[0];
|
|
||||||
|
|
||||||
let mut builder = quote! {
|
|
||||||
axum::routing::#first_method_ident(#mod_ident::#first_method_ident)
|
|
||||||
};
|
|
||||||
|
|
||||||
for (_method, method_ident) in &method_registrations[1..] {
|
|
||||||
builder = quote! {
|
|
||||||
#builder.#method_ident(#mod_ident::#method_ident)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let registration = quote! {
|
|
||||||
router = router.route(#axum_path, #builder);
|
|
||||||
};
|
|
||||||
route_registrations.push(registration);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate the final code
|
let module_tree = generate::module_tree(&mod_namespace, &base_dir, &routes);
|
||||||
let expanded = quote! {
|
let route_registrations = generate::route_registrations(&mod_namespace, &routes);
|
||||||
{
|
|
||||||
#(#module_defs)*
|
|
||||||
|
|
||||||
let mut router = axum::Router::<#state_type>::new();
|
quote! {
|
||||||
#(#route_registrations)*
|
#module_tree
|
||||||
router
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
expanded.into()
|
#input_item
|
||||||
|
|
||||||
|
impl #struct_name {
|
||||||
|
pub fn into_router() -> axum::Router<#state_type> {
|
||||||
|
let mut router = axum::Router::new();
|
||||||
|
#route_registrations
|
||||||
|
router
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively collect route.rs files
|
// This is a workaround for macrotest behaviour
|
||||||
fn collect_route_files(base_dir: &Path, current_dir: &Path, routes: &mut Vec<(PathBuf, PathBuf)>) {
|
#[cfg(debug_assertions)]
|
||||||
if let Ok(entries) = fs::read_dir(current_dir) {
|
fn get_manifest_dir() -> String {
|
||||||
for entry in entries.filter_map(std::result::Result::ok) {
|
use regex::Regex;
|
||||||
let path = entry.path();
|
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or("./".to_string());
|
||||||
|
let re = Regex::new(r"^(.+)/target/tests/axum-folder-router/[A-Za-z0-9]{42}$").unwrap();
|
||||||
|
|
||||||
if path.is_dir() {
|
if let Some(captures) = re.captures(&dir) {
|
||||||
collect_route_files(base_dir, &path, routes);
|
captures.get(1).unwrap().as_str().to_string()
|
||||||
} else if path.file_name().unwrap_or_default() == "route.rs" {
|
} else {
|
||||||
if let Some(parent) = path.parent() {
|
dir
|
||||||
if let Ok(rel_dir) = parent.strip_prefix(base_dir) {
|
|
||||||
routes.push((path.clone(), rel_dir.to_path_buf()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert a relative path to (axum_path, mod_name)
|
#[cfg(not(debug_assertions))]
|
||||||
fn path_to_route_info(rel_path: &Path) -> (String, String) {
|
fn get_manifest_dir() -> String {
|
||||||
if rel_path.components().count() == 0 {
|
std::env::var("CARGO_MANIFEST_DIR").unwrap_or("./".to_string())
|
||||||
return ("/".to_string(), "root".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut axum_path = String::new();
|
|
||||||
let mut mod_name = String::new();
|
|
||||||
|
|
||||||
for segment in rel_path.iter() {
|
|
||||||
let s = segment.to_str().unwrap_or_default();
|
|
||||||
if s.starts_with('[') && s.ends_with(']') {
|
|
||||||
let inner = &s[1..s.len() - 1];
|
|
||||||
if let Some(param) = inner.strip_prefix("...") {
|
|
||||||
axum_path.push_str(&format!("/*{}", param));
|
|
||||||
mod_name.push_str(&format!("__{}", param));
|
|
||||||
} else {
|
|
||||||
axum_path.push_str(&format!("/{{{}}}", inner));
|
|
||||||
mod_name.push_str(&format!("__{}", inner));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
axum_path.push('/');
|
|
||||||
axum_path.push_str(s);
|
|
||||||
mod_name.push_str("__");
|
|
||||||
mod_name.push_str(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
(axum_path, mod_name.trim_start_matches('_').to_string())
|
|
||||||
}
|
}
|
||||||
|
|
98
src/parse.rs
Normal file
98
src/parse.rs
Normal file
|
@ -0,0 +1,98 @@
|
||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
|
use syn::{
|
||||||
|
parse::{Parse, ParseStream},
|
||||||
|
parse_file,
|
||||||
|
Ident,
|
||||||
|
Item,
|
||||||
|
LitStr,
|
||||||
|
Result,
|
||||||
|
Token,
|
||||||
|
Visibility,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct FolderRouterArgs {
|
||||||
|
pub path: String,
|
||||||
|
pub state_type: Ident,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Parse for FolderRouterArgs {
|
||||||
|
fn parse(input: ParseStream) -> Result<Self> {
|
||||||
|
let path_lit = input.parse::<LitStr>()?;
|
||||||
|
input.parse::<Token![,]>()?;
|
||||||
|
let state_type = input.parse::<Ident>()?;
|
||||||
|
|
||||||
|
Ok(FolderRouterArgs {
|
||||||
|
path: path_lit.value(),
|
||||||
|
state_type,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parses the file at the specified location and returns HTTP verb functions
|
||||||
|
pub fn methods_for_route(route_path: &PathBuf) -> Vec<&'static str> {
|
||||||
|
// Read the file content
|
||||||
|
let Ok(file_content) = fs::read_to_string(route_path) else {
|
||||||
|
return Vec::new();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse the file content into a syn syntax tree
|
||||||
|
let Ok(file) = parse_file(&file_content) else {
|
||||||
|
return Vec::new();
|
||||||
|
};
|
||||||
|
|
||||||
|
// Define HTTP methods we're looking for
|
||||||
|
let allowed_methods = [
|
||||||
|
"any", "get", "post", "put", "delete", "patch", "head", "options", "trace", "connect",
|
||||||
|
];
|
||||||
|
let mut found_methods = Vec::new();
|
||||||
|
|
||||||
|
// Collect all pub & async fn's
|
||||||
|
for item in &file.items {
|
||||||
|
if let Item::Fn(fn_item) = item {
|
||||||
|
let fn_name = fn_item.sig.ident.to_string();
|
||||||
|
let is_public = matches!(fn_item.vis, Visibility::Public(_));
|
||||||
|
let is_async = fn_item.sig.asyncness.is_some();
|
||||||
|
|
||||||
|
if is_public && is_async {
|
||||||
|
found_methods.push(fn_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through methods to ensure consistent order
|
||||||
|
allowed_methods
|
||||||
|
.into_iter()
|
||||||
|
.filter(|elem| {
|
||||||
|
found_methods
|
||||||
|
.clone()
|
||||||
|
.into_iter()
|
||||||
|
.any(|method| method == *elem)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collect route.rs files recursively
|
||||||
|
pub fn collect_route_files(base_dir: &Path, dir: &Path) -> Vec<(PathBuf, PathBuf)> {
|
||||||
|
let mut routes = Vec::new();
|
||||||
|
if let Ok(entries) = fs::read_dir(dir) {
|
||||||
|
for entry in entries.filter_map(std::result::Result::ok) {
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
|
if path.is_dir() {
|
||||||
|
let mut nested_routes = collect_route_files(base_dir, &path);
|
||||||
|
routes.append(&mut nested_routes);
|
||||||
|
} else if path.file_name().unwrap_or_default() == "route.rs" {
|
||||||
|
if let Ok(rel_dir) = path.strip_prefix(base_dir) {
|
||||||
|
routes.push((path.clone(), rel_dir.to_path_buf()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
routes.sort();
|
||||||
|
routes
|
||||||
|
}
|
4
tests/expand.rs
Normal file
4
tests/expand.rs
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#[test]
|
||||||
|
pub fn expand_snapshot_pass() {
|
||||||
|
macrotest::expand("tests/expand/*.rs");
|
||||||
|
}
|
155
tests/expand/advanced.expanded.rs
Normal file
155
tests/expand/advanced.expanded.rs
Normal file
|
@ -0,0 +1,155 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
struct AppState {
|
||||||
|
_foo: String,
|
||||||
|
}
|
||||||
|
#[automatically_derived]
|
||||||
|
impl ::core::clone::Clone for AppState {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> AppState {
|
||||||
|
AppState {
|
||||||
|
_foo: ::core::clone::Clone::clone(&self._foo),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "/home/tristand/code/axum-folder-router/examples/advanced/api"]
|
||||||
|
mod __folder_router__myfolderrouter__examples_advanced_api {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "files"]
|
||||||
|
pub mod files {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "[...path]"]
|
||||||
|
pub mod ___path {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::{extract::Path, response::IntoResponse};
|
||||||
|
pub async fn get(Path(path): Path<String>) -> impl IntoResponse {
|
||||||
|
::alloc::__export::must_use({
|
||||||
|
let res = ::alloc::fmt::format(
|
||||||
|
format_args!("Requested file path: {0}", path),
|
||||||
|
);
|
||||||
|
res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "ping"]
|
||||||
|
pub mod ping {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::Html;
|
||||||
|
use axum::response::IntoResponse;
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>GET Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
pub async fn any() -> impl IntoResponse {
|
||||||
|
Html("<h1>ANY Pong!</h1>").into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "users"]
|
||||||
|
pub mod users {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
pub async fn post() -> impl IntoResponse {
|
||||||
|
"Posted successfully".into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "[id]"]
|
||||||
|
pub mod __id {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::{extract::Path, response::IntoResponse};
|
||||||
|
pub async fn get(Path(id): Path<String>) -> impl IntoResponse {
|
||||||
|
::alloc::__export::must_use({
|
||||||
|
let res = ::alloc::fmt::format(format_args!("User ID: {0}", id));
|
||||||
|
res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct MyFolderRouter();
|
||||||
|
impl MyFolderRouter {
|
||||||
|
pub fn into_router() -> axum::Router<AppState> {
|
||||||
|
let mut router = axum::Router::new();
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/files/{*path}",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::files::___path::route::get,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/files",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::files::route::get,
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::files::route::post,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/ping",
|
||||||
|
axum::routing::any(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::ping::route::any,
|
||||||
|
)
|
||||||
|
.get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::ping::route::get,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::route::get,
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::route::post,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/users/{:id}",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::users::__id::route::get,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/users",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::users::route::get,
|
||||||
|
)
|
||||||
|
.post(
|
||||||
|
__folder_router__myfolderrouter__examples_advanced_api::users::route::post,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router
|
||||||
|
}
|
||||||
|
}
|
9
tests/expand/advanced.rs
Normal file
9
tests/expand/advanced.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState {
|
||||||
|
_foo: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[folder_router("examples/advanced/api", AppState)]
|
||||||
|
struct MyFolderRouter();
|
33
tests/expand/simple.expanded.rs
Normal file
33
tests/expand/simple.expanded.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
struct AppState;
|
||||||
|
#[automatically_derived]
|
||||||
|
impl ::core::clone::Clone for AppState {
|
||||||
|
#[inline]
|
||||||
|
fn clone(&self) -> AppState {
|
||||||
|
AppState
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[path = "/home/tristand/code/axum-folder-router/examples/simple/api"]
|
||||||
|
mod __folder_router__myfolderrouter__examples_simple_api {
|
||||||
|
#[path = "route.rs"]
|
||||||
|
pub mod route {
|
||||||
|
use axum::response::{Html, IntoResponse};
|
||||||
|
pub async fn get() -> impl IntoResponse {
|
||||||
|
Html("<h1>Hello World!</h1>").into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
struct MyFolderRouter();
|
||||||
|
impl MyFolderRouter {
|
||||||
|
pub fn into_router() -> axum::Router<AppState> {
|
||||||
|
let mut router = axum::Router::new();
|
||||||
|
router = router
|
||||||
|
.route(
|
||||||
|
"/",
|
||||||
|
axum::routing::get(
|
||||||
|
__folder_router__myfolderrouter__examples_simple_api::route::get,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
router
|
||||||
|
}
|
||||||
|
}
|
7
tests/expand/simple.rs
Normal file
7
tests/expand/simple.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState;
|
||||||
|
|
||||||
|
#[folder_router("examples/simple/api", AppState)]
|
||||||
|
struct MyFolderRouter();
|
5
tests/failures.rs
Normal file
5
tests/failures.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#[test]
|
||||||
|
fn ui() {
|
||||||
|
let t = trybuild::TestCases::new();
|
||||||
|
t.compile_fail("tests/failures/*.rs");
|
||||||
|
}
|
9
tests/failures/no_files.rs
Normal file
9
tests/failures/no_files.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState;
|
||||||
|
|
||||||
|
#[folder_router("some/non/existing/directory", AppState)]
|
||||||
|
struct MyFolderRouter();
|
||||||
|
|
||||||
|
fn main() {}
|
7
tests/failures/no_files.stderr
Normal file
7
tests/failures/no_files.stderr
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
error: No route.rs files found in the specified directory: 'some/non/existing/directory'. Make sure the path is correct and contains route.rs files.
|
||||||
|
--> tests/failures/no_files.rs:6:1
|
||||||
|
|
|
||||||
|
6 | #[folder_router("some/non/existing/directory", AppState)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in the attribute macro `folder_router` (in Nightly builds, run with -Z macro-backtrace for more info)
|
9
tests/failures/no_routes.rs
Normal file
9
tests/failures/no_routes.rs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
use axum_folder_router::folder_router;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState;
|
||||||
|
|
||||||
|
#[folder_router("../../../../tests/failures/no_routes", AppState)]
|
||||||
|
struct MyFolderRouter();
|
||||||
|
|
||||||
|
fn main() {}
|
8
tests/failures/no_routes.stderr
Normal file
8
tests/failures/no_routes.stderr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
error: No routes defined in your route.rs's !
|
||||||
|
Ensure that at least one `pub async fn` named after an HTTP verb is defined. (e.g. get, post, put, delete)
|
||||||
|
--> tests/failures/no_routes.rs:6:1
|
||||||
|
|
|
||||||
|
6 | #[folder_router("../../../../tests/failures/no_routes", AppState)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in the attribute macro `folder_router` (in Nightly builds, run with -Z macro-backtrace for more info)
|
0
tests/failures/no_routes/route.rs
Normal file
0
tests/failures/no_routes/route.rs
Normal file
Loading…
Add table
Reference in a new issue