For the Party Corgi Network Discord bot that we built in Rust we got to the point where
main.rs was no longer serving it's purpose. We had implemented a few commands, one of which was taking up a lot of space in
main.rs. We needed to pull the code for the moderator-runnable commands into their own file.
The module tree and the filesystem are not a 1-to-1 match in Rust. This means that you don't necessarily get an import path that matches a filesystem path. We have to specify the module tree manually.
An important note on Rust modules is that they are declared from the parent. So in
main.rs we added a module declaration.
This declaration introduces the
commands module, so now we need to put the commands module code somewhere. We chose to do this in
./commands/mod.rs although Rust would also look for a
./commands.rs file. The mod name is special. It's like
index.js in node.js projects.
We chose to treat the
./commands/mods.rs file like an
index.js because we're going to have a few command groups.
The only declaration in the
mod.rs file is to declare a public module
mod_group. In the future here is where we'll also place the
general_group module declaration to contain commands everyone can run.
Now that we've declared the
commands module and the sub-module
mod_group we have a module tree that looks like this:
./commands/mod_group.rs (note that we didn't use the
mod.rs approach this time) we put all of the code related to moderator commands. There is nothing special about this file. It doesn't contain any more module declaration code.
And that's it, you have a couple declarations and a couple files.
With this code we also stumbled upon one more thing. We have some macros on the
These macros create a
MOD_GROUP struct that we need to use in main. So we use
use to access it.