슬기로운 개발자생활/Backend

Dealing with sub-packages (or sub-modules) in Rust

개발자 소신 2024. 3. 8. 14:48
반응형

When dealing with sub-packages (or sub-modules) in Rust, especially in larger projects like an axum web application, structuring your code effectively is key to maintaining clarity and manageability. Let's address your example structure and how you can organize and reference these modules and sub-modules.

Given your project structure:

  • src/main.rs
  • src/photo/handler.rs
  • src/photo/usecase.rs
  • src/photo/dto/req.rs (used in handler.rs or usecase.rs)

And your existing src/photo.rs which seems to aim at declaring modules within the photo domain:

pub mod entity;
pub mod handler;
pub mod repository;
pub mod route;
pub mod usecase;

pub mod req;

However, there seems to be a misunderstanding in your example regarding how to declare the req module if it's inside a dto sub-directory. To correctly structure and reference sub-modules in Rust, follow these guidelines:

Step 1: Structuring Sub-Modules

Given your project setup, your photo module is attempting to declare various sub-modules like handler, usecase, and a nested req module inside a dto directory. The structure inside the photo directory should reflect this hierarchy. Assuming dto is only meant for data transfer objects, and req is one of those objects, your directory structure would ideally be:

src/
|-- main.rs
|-- photo/
    |-- mod.rs (previously photo.rs)
    |-- handler.rs
    |-- usecase.rs
    |-- dto/
        |-- mod.rs
        |-- req.rs

Step 2: Declaring Sub-Modules

To correctly declare these sub-modules, you should adjust your photo/mod.rs and photo/dto/mod.rs files as follows:

In src/photo/mod.rs, you adjust the module declarations to include the dto submodule:

pub mod entity;
pub mod handler;
pub mod repository;
pub mod route;
pub mod usecase;
pub mod dto; // Declare the dto module here

Then, in src/photo/dto/mod.rs, you declare the req module:

pub mod req;

Step 3: Using Nested Modules

To use these nested modules (e.g., req in handler or usecase), you reference them by their path through the module hierarchy. For example, in handler.rs or usecase.rs, you can use:

use crate::photo::dto::req::YourReqStruct;

This assumes you have a structure or enum named YourReqStruct inside req.rs that you want to use.

Step 4: Visibility

Ensure that the visibility of your modules and their items (functions, structs, enums) matches their intended use. Using pub makes a module or item public, allowing it to be used outside of its current module. For deeply nested structures, you might need pub(crate) for crate-wide visibility or pub(super) to make items visible to the parent module.

Conclusion

Rust's module system offers a flexible way to organize your code into a clear hierarchy, enhancing maintainability and readability. By structuring your project with clear module declarations and respecting Rust's visibility rules, you can build complex applications with well-organized code bases. This approach to managing sub-packages and sub-modules in Rust ensures that your project remains scalable and easy to navigate, even as it grows in size and complexity.

반응형