[{"data":1,"prerenderedAt":851},["ShallowReactive",2],{"$fdpo6Kfl6XqGAzO7-GUGE-_9VCne71wTl4nwVitU78yY":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":837,"editUrl":845,"prev":846,"next":849},"overview\u002Fmodules","Modules","Encapsulate related functionality with","---\ntitle: Modules\ndescription: Encapsulate related functionality with #[module] — imports, providers, controllers and exports.\n---\n\n# Modules\n\nModules are the primary way to organize an application into coherent units. A module brings together **controllers**, **providers**, and dependencies on **other modules**, and controls what it exposes to the rest of the world.\n\n## `#[module]`\n\n```rust\nuse ferrite_framework::{module, Module};\n\n#[module(\n    imports = [AuthModule],\n    controllers = [UsersController],\n    providers = [UsersService, UsersRepository],\n    exports = [UsersService],\n)]\npub struct UsersModule;\n```\n\n| Key | Meaning |\n| --- | --- |\n| `imports` | Other modules whose exported providers become resolvable here |\n| `controllers` | Controller structs whose routes are registered |\n| `providers` | Providers instantiated and made available within this module |\n| `exports` | Providers re-exported to modules that import this one |\n| `global` | Flag marking all exports as globally available |\n\nOnly unit structs are supported:\n\n```rust\npub struct UsersModule;\n```\n\nThe macro type-asserts everything: imported names must implement `Module`, and providers\u002Fcontrollers\u002Fexports must implement `Injectable`. Mistakes become compiler errors.\n\n## The root module\n\nEvery application has one root module that imports everything else:\n\n```rust\nuse ferrite_framework::{module, Ferrite};\n\n#[module(\n    imports = [AuthModule, HealthModule, SwaggerModule, UsersModule],\n)]\npub struct AppModule;\n\n#[bootstrap]\nasync fn main() {\n    let app = Ferrite::create::\u003CAppModule>().await;\n    app.listen(\"0.0.0.0:3000\").await.expect(\"failed to bind\");\n}\n```\n\n## The module descriptor\n\nAt compile time the macro emits a `ModuleDescriptor`:\n\n```rust\npub struct ModuleDescriptor {\n    pub name: String,\n    pub providers: Vec\u003CTypeId>,\n    pub controllers: Vec\u003CTypeId>,\n    pub imports: Vec\u003CTypeId>,\n    pub exports: Vec\u003CTypeId>,\n    pub middleware: Vec\u003CTypeId>,\n    pub global: bool,\n}\n```\n\nDiscovery helpers walk these descriptors at runtime:\n\n```rust\nuse ferrite_framework::{all_modules, find_module, all_providers, lookup_provider};\n\nall_modules()                       \u002F\u002F Vec\u003C&ModuleDescriptor>\nfind_module(\"UsersModule\")          \u002F\u002F Option\u003C&ModuleDescriptor>\nall_providers()                     \u002F\u002F Vec\u003C&ProviderEntry>\nlookup_provider::\u003CUsersService>()   \u002F\u002F Unique lookup by type\n```\n\n## Module providers and global registration\n\nWhen a module lists a provider in `providers` or `exports`, it contributes a `ProviderEntry` to the inventory registry. Providers with no module — like infrastructure singletons — register themselves globally.\n\nModules from ecosystem crates generally expose a ready-made struct:\n\n```rust\n#[module(imports = [AuthModule, HealthModule, SwaggerModule, CacheModule, ThrottlerModule])]\npub struct AppModule;\n```\n\n## Extending modules with ecosystem modules\n\nFeatures ship as **modules + providers** together. For example:\n\n- `AuthModule` — JWT `JwtService`, `AuthGuard`, `CurrentUser`\n- `HealthModule` — `HealthService`, `HealthController` at `\u002Fhealth`\n- `SwaggerModule` — `SwaggerService`, Swagger UI at `\u002Fdocs`\n- `CacheModule` — `CacheService`\n- `ThrottlerModule` — `ThrottlerService`, `ThrottlerGuard`\n\n## Next steps\n\n- [Bootstrap](\u002Fdocs\u002Foverview\u002Fbootstrap) — how `Ferrite::create` compiles the graph.\n- [Dependency injection](\u002Fdocs\u002Ffundamentals\u002Fdependency-injection).\n- [Testing](\u002Fdocs\u002Ffundamentals\u002Ftesting) — override providers in a `TestingModule`.",{"frontmatter":9,"meta":10,"nodes":34},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31],{"id":17,"depth":13,"text":18},"module","#[module]",{"id":20,"depth":13,"text":21},"the-root-module","The root module",{"id":23,"depth":13,"text":24},"the-module-descriptor","The module descriptor",{"id":26,"depth":13,"text":27},"module-providers-and-global-registration","Module providers and global registration",{"id":29,"depth":13,"text":30},"extending-modules-with-ecosystem-modules","Extending modules with ecosystem modules",{"id":32,"depth":13,"text":33},"next-steps","Next steps",[35,39,56,62,186,254,257,271,281,283,286,448,450,457,595,598,671,673,686,689,735,737,744,805,807],[36,37,5],"h1",{"id":38},"modules",[40,41,42,43,47,48,51,52,55],"p",{},"Modules are the primary way to organize an application into coherent units. A module brings together ",[44,45,46],"strong",{},"controllers",", ",[44,49,50],{},"providers",", and dependencies on ",[44,53,54],{},"other modules",", and controls what it exposes to the rest of the world.",[57,58,59],"h2",{"id":17},[60,61,18],"code",{},[63,64,67],"pre",{"language":65,"class":66},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[60,68,70,96,97,96,99,96,104,96,121,96,135,96,154,96,167,96,172],{"class":69},"language-rust",[71,72,75,79,83,86,90,93],"span",{"class":73,"style":74},"line","display: inline",[71,76,78],{"style":77},"color:#D73A49;--shiki-dark:#F97583","use",[71,80,82],{"style":81},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[71,84,85],{"style":77},"::",[71,87,89],{"style":88},"color:#24292E;--shiki-dark:#E1E4E8","{module, ",[71,91,92],{"style":81},"Module",[71,94,95],{"style":88},"};","\n",[71,98],{"class":73,"style":74},[71,100,101],{"class":73,"style":74},[71,102,103],{"style":88},"#[module(",[71,105,106,109,112,115,118],{"class":73,"style":74},[71,107,108],{"style":88},"    imports ",[71,110,111],{"style":77},"=",[71,113,114],{"style":88}," [",[71,116,117],{"style":81},"AuthModule",[71,119,120],{"style":88},"],",[71,122,123,126,128,130,133],{"class":73,"style":74},[71,124,125],{"style":88},"    controllers ",[71,127,111],{"style":77},[71,129,114],{"style":88},[71,131,132],{"style":81},"UsersController",[71,134,120],{"style":88},[71,136,137,140,142,144,147,149,152],{"class":73,"style":74},[71,138,139],{"style":88},"    providers ",[71,141,111],{"style":77},[71,143,114],{"style":88},[71,145,146],{"style":81},"UsersService",[71,148,47],{"style":88},[71,150,151],{"style":81},"UsersRepository",[71,153,120],{"style":88},[71,155,156,159,161,163,165],{"class":73,"style":74},[71,157,158],{"style":88},"    exports ",[71,160,111],{"style":77},[71,162,114],{"style":88},[71,164,146],{"style":81},[71,166,120],{"style":88},[71,168,169],{"class":73,"style":74},[71,170,171],{"style":88},")]",[71,173,174,177,180,183],{"class":73,"style":74},[71,175,176],{"style":77},"pub",[71,178,179],{"style":77}," struct",[71,181,182],{"style":81}," UsersModule",[71,184,185],{"style":88},";",[187,188,189,202],"table",{},[190,191,192],"thead",{},[193,194,195,199],"tr",{},[196,197,198],"th",{},"Key",[196,200,201],{},"Meaning",[203,204,205,216,225,234,244],"tbody",{},[193,206,207,213],{},[208,209,210],"td",{},[60,211,212],{},"imports",[208,214,215],{},"Other modules whose exported providers become resolvable here",[193,217,218,222],{},[208,219,220],{},[60,221,46],{},[208,223,224],{},"Controller structs whose routes are registered",[193,226,227,231],{},[208,228,229],{},[60,230,50],{},[208,232,233],{},"Providers instantiated and made available within this module",[193,235,236,241],{},[208,237,238],{},[60,239,240],{},"exports",[208,242,243],{},"Providers re-exported to modules that import this one",[193,245,246,251],{},[208,247,248],{},[60,249,250],{},"global",[208,252,253],{},"Flag marking all exports as globally available",[40,255,256],{},"Only unit structs are supported:",[63,258,259],{"language":65,"class":66},[60,260,261],{"class":69},[71,262,263,265,267,269],{"class":73,"style":74},[71,264,176],{"style":77},[71,266,179],{"style":77},[71,268,182],{"style":81},[71,270,185],{"style":88},[40,272,273,274,276,277,280],{},"The macro type-asserts everything: imported names must implement ",[60,275,92],{},", and providers\u002Fcontrollers\u002Fexports must implement ",[60,278,279],{},"Injectable",". Mistakes become compiler errors.",[57,282,21],{"id":20},[40,284,285],{},"Every application has one root module that imports everything else:",[63,287,288],{"language":65,"class":66},[60,289,290,96,305,96,307,96,311,96,338,96,342,96,353,96,355,96,360,96,374,96,408,96,443],{"class":69},[71,291,292,294,296,298,300,303],{"class":73,"style":74},[71,293,78],{"style":77},[71,295,82],{"style":81},[71,297,85],{"style":77},[71,299,89],{"style":88},[71,301,302],{"style":81},"Ferrite",[71,304,95],{"style":88},[71,306],{"class":73,"style":74},[71,308,309],{"class":73,"style":74},[71,310,103],{"style":88},[71,312,313,315,317,319,321,323,326,328,331,333,336],{"class":73,"style":74},[71,314,108],{"style":88},[71,316,111],{"style":77},[71,318,114],{"style":88},[71,320,117],{"style":81},[71,322,47],{"style":88},[71,324,325],{"style":81},"HealthModule",[71,327,47],{"style":88},[71,329,330],{"style":81},"SwaggerModule",[71,332,47],{"style":88},[71,334,335],{"style":81},"UsersModule",[71,337,120],{"style":88},[71,339,340],{"class":73,"style":74},[71,341,171],{"style":88},[71,343,344,346,348,351],{"class":73,"style":74},[71,345,176],{"style":77},[71,347,179],{"style":77},[71,349,350],{"style":81}," AppModule",[71,352,185],{"style":88},[71,354],{"class":73,"style":74},[71,356,357],{"class":73,"style":74},[71,358,359],{"style":88},"#[bootstrap]",[71,361,362,365,368,371],{"class":73,"style":74},[71,363,364],{"style":77},"async",[71,366,367],{"style":77}," fn",[71,369,370],{"style":81}," main",[71,372,373],{"style":88},"() {",[71,375,376,379,382,384,387,389,392,394,397,400,403,406],{"class":73,"style":74},[71,377,378],{"style":77},"    let",[71,380,381],{"style":88}," app ",[71,383,111],{"style":77},[71,385,386],{"style":81}," Ferrite",[71,388,85],{"style":77},[71,390,391],{"style":81},"create",[71,393,85],{"style":77},[71,395,396],{"style":88},"\u003C",[71,398,399],{"style":81},"AppModule",[71,401,402],{"style":88},">()",[71,404,405],{"style":77},".await",[71,407,185],{"style":88},[71,409,410,413,416,419,422,426,429,432,435,437,440],{"class":73,"style":74},[71,411,412],{"style":88},"    app",[71,414,415],{"style":77},".",[71,417,418],{"style":81},"listen",[71,420,421],{"style":88},"(",[71,423,425],{"style":424},"color:#032F62;--shiki-dark:#9ECBFF","\"0.0.0.0:3000\"",[71,427,428],{"style":88},")",[71,430,431],{"style":77},".await.",[71,433,434],{"style":81},"expect",[71,436,421],{"style":88},[71,438,439],{"style":424},"\"failed to bind\"",[71,441,442],{"style":88},");",[71,444,445],{"class":73,"style":74},[71,446,447],{"style":88},"}",[57,449,24],{"id":23},[40,451,452,453,456],{},"At compile time the macro emits a ",[60,454,455],{},"ModuleDescriptor",":",[63,458,459],{"language":65,"class":66},[60,460,461,96,473,96,489,96,509,96,526,96,543,96,560,96,577,96,591],{"class":69},[71,462,463,465,467,470],{"class":73,"style":74},[71,464,176],{"style":77},[71,466,179],{"style":77},[71,468,469],{"style":81}," ModuleDescriptor",[71,471,472],{"style":88}," {",[71,474,475,478,481,483,486],{"class":73,"style":74},[71,476,477],{"style":77},"    pub",[71,479,480],{"style":88}," name",[71,482,456],{"style":77},[71,484,485],{"style":81}," String",[71,487,488],{"style":88},",",[71,490,491,493,496,498,501,503,506],{"class":73,"style":74},[71,492,477],{"style":77},[71,494,495],{"style":88}," providers",[71,497,456],{"style":77},[71,499,500],{"style":81}," Vec",[71,502,396],{"style":88},[71,504,505],{"style":81},"TypeId",[71,507,508],{"style":88},">,",[71,510,511,513,516,518,520,522,524],{"class":73,"style":74},[71,512,477],{"style":77},[71,514,515],{"style":88}," controllers",[71,517,456],{"style":77},[71,519,500],{"style":81},[71,521,396],{"style":88},[71,523,505],{"style":81},[71,525,508],{"style":88},[71,527,528,530,533,535,537,539,541],{"class":73,"style":74},[71,529,477],{"style":77},[71,531,532],{"style":88}," imports",[71,534,456],{"style":77},[71,536,500],{"style":81},[71,538,396],{"style":88},[71,540,505],{"style":81},[71,542,508],{"style":88},[71,544,545,547,550,552,554,556,558],{"class":73,"style":74},[71,546,477],{"style":77},[71,548,549],{"style":88}," exports",[71,551,456],{"style":77},[71,553,500],{"style":81},[71,555,396],{"style":88},[71,557,505],{"style":81},[71,559,508],{"style":88},[71,561,562,564,567,569,571,573,575],{"class":73,"style":74},[71,563,477],{"style":77},[71,565,566],{"style":88}," middleware",[71,568,456],{"style":77},[71,570,500],{"style":81},[71,572,396],{"style":88},[71,574,505],{"style":81},[71,576,508],{"style":88},[71,578,579,581,584,586,589],{"class":73,"style":74},[71,580,477],{"style":77},[71,582,583],{"style":88}," global",[71,585,456],{"style":77},[71,587,588],{"style":81}," bool",[71,590,488],{"style":88},[71,592,593],{"class":73,"style":74},[71,594,447],{"style":88},[40,596,597],{},"Discovery helpers walk these descriptors at runtime:",[63,599,600],{"language":65,"class":66},[60,601,602,96,613,96,615,96,627,96,643,96,654],{"class":69},[71,603,604,606,608,610],{"class":73,"style":74},[71,605,78],{"style":77},[71,607,82],{"style":81},[71,609,85],{"style":77},[71,611,612],{"style":88},"{all_modules, find_module, all_providers, lookup_provider};",[71,614],{"class":73,"style":74},[71,616,617,620,623],{"class":73,"style":74},[71,618,619],{"style":81},"all_modules",[71,621,622],{"style":88},"()                       ",[71,624,626],{"style":625},"color:#6A737D;--shiki-dark:#6A737D","\u002F\u002F Vec\u003C&ModuleDescriptor>",[71,628,629,632,634,637,640],{"class":73,"style":74},[71,630,631],{"style":81},"find_module",[71,633,421],{"style":88},[71,635,636],{"style":424},"\"UsersModule\"",[71,638,639],{"style":88},")          ",[71,641,642],{"style":625},"\u002F\u002F Option\u003C&ModuleDescriptor>",[71,644,645,648,651],{"class":73,"style":74},[71,646,647],{"style":81},"all_providers",[71,649,650],{"style":88},"()                     ",[71,652,653],{"style":625},"\u002F\u002F Vec\u003C&ProviderEntry>",[71,655,656,659,661,663,665,668],{"class":73,"style":74},[71,657,658],{"style":81},"lookup_provider",[71,660,85],{"style":77},[71,662,396],{"style":88},[71,664,146],{"style":81},[71,666,667],{"style":88},">()   ",[71,669,670],{"style":625},"\u002F\u002F Unique lookup by type",[57,672,27],{"id":26},[40,674,675,676,678,679,681,682,685],{},"When a module lists a provider in ",[60,677,50],{}," or ",[60,680,240],{},", it contributes a ",[60,683,684],{},"ProviderEntry"," to the inventory registry. Providers with no module — like infrastructure singletons — register themselves globally.",[40,687,688],{},"Modules from ecosystem crates generally expose a ready-made struct:",[63,690,691],{"language":65,"class":66},[60,692,693,96,725],{"class":69},[71,694,695,698,700,702,704,706,708,710,712,714,717,719,722],{"class":73,"style":74},[71,696,697],{"style":88},"#[module(imports ",[71,699,111],{"style":77},[71,701,114],{"style":88},[71,703,117],{"style":81},[71,705,47],{"style":88},[71,707,325],{"style":81},[71,709,47],{"style":88},[71,711,330],{"style":81},[71,713,47],{"style":88},[71,715,716],{"style":81},"CacheModule",[71,718,47],{"style":88},[71,720,721],{"style":81},"ThrottlerModule",[71,723,724],{"style":88},"])]",[71,726,727,729,731,733],{"class":73,"style":74},[71,728,176],{"style":77},[71,730,179],{"style":77},[71,732,350],{"style":81},[71,734,185],{"style":88},[57,736,30],{"id":29},[40,738,739,740,743],{},"Features ship as ",[44,741,742],{},"modules + providers"," together. For example:",[745,746,747,762,777,788,795],"ul",{},[748,749,750,752,753,47,756,47,759],"li",{},[60,751,117],{}," — JWT ",[60,754,755],{},"JwtService",[60,757,758],{},"AuthGuard",[60,760,761],{},"CurrentUser",[748,763,764,766,767,47,770,773,774],{},[60,765,325],{}," — ",[60,768,769],{},"HealthService",[60,771,772],{},"HealthController"," at ",[60,775,776],{},"\u002Fhealth",[748,778,779,766,781,784,785],{},[60,780,330],{},[60,782,783],{},"SwaggerService",", Swagger UI at ",[60,786,787],{},"\u002Fdocs",[748,789,790,766,792],{},[60,791,716],{},[60,793,794],{},"CacheService",[748,796,797,766,799,47,802],{},[60,798,721],{},[60,800,801],{},"ThrottlerService",[60,803,804],{},"ThrottlerGuard",[57,806,33],{"id":32},[745,808,809,821,827],{},[748,810,811,816,817,820],{},[812,813,815],"a",{"href":814},"\u002Fdocs\u002Foverview\u002Fbootstrap","Bootstrap"," — how ",[60,818,819],{},"Ferrite::create"," compiles the graph.",[748,822,823,415],{},[812,824,826],{"href":825},"\u002Fdocs\u002Ffundamentals\u002Fdependency-injection","Dependency injection",[748,828,829,833,834,415],{},[812,830,832],{"href":831},"\u002Fdocs\u002Ffundamentals\u002Ftesting","Testing"," — override providers in a ",[60,835,836],{},"TestingModule",{"title":12,"searchDepth":13,"depth":14,"links":838},[839,840,841,842,843,844],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Foverview\u002Fmodules.md",{"title":847,"slug":848},"Providers","overview\u002Fproviders",{"title":815,"slug":850},"overview\u002Fbootstrap",1789565440441]