How to organize modules

Hi!

I'm working on a project, and I am struggling to find a module file structure that works for me personally. There's a module of services:

services/
services/auth/mod.rs, provider1.rs, .... [auth internals]
services/log/mod.rs, .. [log internals]
services/db/mod.rs, ... [db internals]

The issue is, I do not want to put any code into mod.rs because then I just have dozen mod.rs files opened which is very annoying. Yeah, my IDE sees that, and does auth/mod.rs, log/mod.rs, db/mod.rs as the tab name instead of just 3x mod.rs but it still feels annoying.

So I tend to put code into services/serviceName/serviceName.rs, ie

services/
services/auth/mod.rs, auth.rs, .... [auth internals]
services/log/mod.rs, log.rs, .. [log internals]
services/db/mod.rs, db.rs, ... [db internals]

This, however triggers the Clippy lint. Clippy Lints

I understand that what I am doing is sub-optimal, not only because I have to re-export a bunch of things in mod.rs to avoid the exact scenario the lint is about. But again, I really don't want to have things in mod.rs.

This page of the rust book Control Scope and Privacy with Modules - The Rust Programming Language, seems to suggest another style:

services/
services/auth/..[auth internals]...
services/log/..[log internals]...
services/db/..[db internals]..
services/auth.rs
services/log.rs
services/db.rs

I do not like that this one is breaking the encapsulation a little bit, ie, auth stuff is spread in two places, services/auth.rs (file) and services/auth/ (directory).

So do is this just a personal taste issue or am I missing something?

If you don't like this, use the other approach in the 2nd half of your message. This is a truly matter of personal taste.

The clippy lint says you have two mod mod_name, and these are probably meant to be the same module. This won't work. There must be exactly one mod mod_name for each module.

1 Like

Yeah, I guess you're right. Somewhat unsatisfying but if I am not missing anything that seems like the best solution for me

Instead of using mod.rs in the child directory, you can use <modname>.rs in the parent directory. I find that better for editing and generally more convenient.

1 Like