| name | organize-modules |
| description | Apply private modules with public re-exports (barrel export) pattern for clean API design. Includes conditional visibility for docs and tests. Use when creating modules, organizing mod.rs files, or before creating commits. |
Module Organization Best Practices
When to Use
- Creating new Rust modules
- Refactoring module structure
- Organizing mod.rs files
- Reviewing code that exposes internal structure
- Making private types visible to documentation
- Before creating commits with module changes
- When user says "organize modules", "refactor modules", "fix module structure", etc.
Instructions
Follow these patterns for clean, maintainable module organization:
Step 1: Apply the Recommended Pattern
Prefer private modules with public re-exports (also known as the barrel export pattern) as
the default pattern.
This provides a clean API while maintaining flexibility to refactor internal structure.
mod constants;
mod types;
mod helpers;
pub use constants::*;
pub use types::*;
pub use helpers::*;
What this achieves:
- Clean, flat API for users
- Internal structure is hidden and can be refactored freely
- No namespace pollution from module names
Step 2: Control Rustfmt Behavior (When Needed)
For mod.rs files with deliberate manual alignment, prevent rustfmt from reformatting:
#![rustfmt::skip]
mod constants;
mod types;
mod helpers;
pub use constants::*;
pub use types::*;
pub use helpers::*;
When to use rustfmt skip:
- Large
mod.rs files with many exports
- Deliberately structured code alignment for clarity
- Manual grouping of related items (e.g., test fixtures)
- Files where organization conveys semantic meaning
When NOT to use:
- Small, simple mod.rs files
- When automatic formatting is preferred
Step 3: Apply Conditional Visibility for Docs and Tests
When you need a module to be:
- Private in production builds (encapsulation)
- Public for documentation (rustdoc links work)
- Public for tests (test code can access internals)
Use conditional compilation:
#[cfg(any(test, doc))]
pub mod internal_parser;
#[cfg(not(any(test, doc)))]
mod internal_parser;
pub use internal_parser::*;
How this works:
- In doc builds: Module is public → rustdoc can see and link to it
- In test builds: Module is public → tests can access internals
- In production builds: Module is private → internal implementation detail
This pattern is frequently used with the write-documentation skill when fixing documentation
links to private types.
When to Omit the Fallback Branch
You can skip the #[cfg(not(any(test, doc)))] fallback when the module has no code to compile
in production:
#[cfg(any(test, doc))]
pub mod integration_tests_docs;
#[cfg(any(test, doc))]
pub mod integration_tests;
Keep the fallback when the module contains code that must compile in production (even if
private):
#[cfg(any(test, doc))]
pub mod internal_parser;
#[cfg(not(any(test, doc)))]
mod internal_parser;
pub use internal_parser::*;
Platform-Specific Modules with Cross-Platform Docs
For modules that are platform-specific but should have docs generated on all platforms, use
any(doc, ...) to separate documentation from runtime requirements:
#[cfg(any(doc, all(target_os = "linux", test)))]
pub mod input;
#[cfg(all(target_os = "linux", not(any(test, doc))))]
mod input;
#[cfg(any(target_os = "linux", doc))]
pub use input::*;
Key insight: rustdoc runs the Rust compiler internally. When you write
#[cfg(all(target_os = "linux", any(test, doc)))], the target_os = "linux" check still excludes
macOS/Windows even during doc builds. The doc cfg flag doesn't override other conditions—it's
just another flag you can check.
The fix: Use any(doc, ...) to make doc an alternative path:
any(doc, all(target_os = "linux", test)) means: "docs on any platform OR tests on Linux"
all(target_os = "linux", any(test, doc)) means: "Linux AND (tests OR docs)" — still requires
Linux!
When you see broken doc links for platform-specific modules:
#[cfg(all(target_os = "linux", any(test, doc)))]
pub mod linux_only_module;
#[cfg(any(doc, all(target_os = "linux", test)))]
pub mod linux_only_module;
#[cfg(all(target_os = "linux", not(any(test, doc))))]
mod linux_only_module;
⚠️ Unix Dependency Caveat
The cfg(any(doc, ...)) pattern above assumes the module's code compiles on all platforms.
When the module uses Unix-only APIs (e.g., mio::unix::SourceFd, signal_hook,
std::os::fd::AsRawFd), restrict doc builds to Unix:
#[cfg(any(all(unix, doc), all(target_os = "linux", test)))]
pub mod input;
#[cfg(all(target_os = "linux", not(any(test, doc))))]
mod input;
#[cfg(any(target_os = "linux", all(unix, doc)))]
pub use input::*;
Three-tier hierarchy:
| Module dependencies | Pattern | Docs: Linux | Docs: macOS | Docs: Windows |
|---|
| Platform-agnostic | cfg(any(doc, ...)) | ✅ | ✅ | ✅ |
| Unix APIs | cfg(any(all(unix, doc), ...)) | ✅ | ✅ | excluded |
| Linux-only APIs | cfg(any(all(target_os = "linux", doc), ...)) | ✅ | excluded | excluded |
Rule of thumb: Match your doc cfg guard to your dependency's cfg guard in Cargo.toml.
Apply at all levels — If the module is nested, both parent and child need the visibility
change. Also update any re-exports:
#[cfg(any(doc, all(target_os = "linux", test)))]
pub mod integration_tests;
#[cfg(any(doc, all(target_os = "linux", test)))]
pub mod pty_input_test;
#[cfg(any(target_os = "linux", doc))]
pub use integration_tests::*;
Step 4: Handle Transitive Visibility
Important: If a conditionally public module links to another module in its documentation,
that target module must also be conditionally public.
#[cfg(any(test, doc))]
pub mod paint_impl;
#[cfg(not(any(test, doc)))]
mod paint_impl;
#[cfg(any(test, doc))]
pub mod diff_chunks;
#[cfg(not(any(test, doc)))]
mod diff_chunks;
pub use paint_impl::*;
pub use diff_chunks::*;
Why: Rustdoc needs to resolve all links in documentation. If paint_impl docs link to
diff_chunks, rustdoc must be able to see diff_chunks.
Step 5: Reference in Rustdoc
When linking to conditionally public modules in documentation, use the mod@ prefix:
See the write-documentation skill for complete details on rustdoc links.
Step 6: Multi-Level Barrel Exports and Rustdoc Search
When rustdoc generates documentation, the search index includes all public items and
modules. For multi-level barrel exports (pub mod intermediate; pub use intermediate::*;),
the search index resolves the "shortest public path" for items. But rustdoc only generates
HTML pages at the canonical definition path, not at the flattened re-export path.
This means searching for an item re-exported via a barrel might produce a link to a page
that doesn't exist (e.g., core/ansi/csi/index.html instead of
core/ansi/constants/csi/index.html).
The fix: Use #[doc(inline)] to re-export submodules at the parent level, but only when
the intermediate module is a well-documented organizational hub (has module-level //! docs,
organization tables, etc.) AND its submodules are pub mod.
| Intermediate module characteristics | Action | Why |
|---|
| Public, has module docs, organization tables | Add #[doc(inline)] | Submodules are discoverable via search; pages must exist |
Private with pub use *; only (pure barrel) | No action needed | Submodules aren't in the search index at all |
| Public but no module docs (structural only) | No action needed | Not worth the doc noise; users won't search for these |
In the parent module's mod.rs, add explicit #[doc(inline)] re-exports alongside the
existing glob re-export:
pub use constants::*;
#[doc(inline)]
pub use constants::{csi, dsr};
Always include the inline comment on #[doc(inline)] lines:
#[doc(inline)]
If the re-exported modules use conditional visibility (#[cfg(any(test, doc))]), add the
guard with its own inline comment:
#[cfg(any(test, doc))]
#[doc(inline)]
pub use constants::{csi, dsr};
This creates pages at both paths (ansi/csi/ AND ansi/constants/csi/), so rustdoc
search links work regardless of which path the search index resolves.
Benefits of This Pattern
1. Clean, Flat API
Users import directly without unnecessary nesting:
✅ Good (flat, ergonomic):
use my_module::MyType;
use my_module::CONSTANT;
❌ Bad (exposes internal structure):
use my_module::types::MyType;
use my_module::constants::CONSTANT;
2. Refactoring Freedom
Internal reorganization doesn't break external code:
3. Avoid Naming Conflicts
Private module names don't pollute the namespace:
mod constants;
pub use constants::*;
mod constants;
4. Encapsulation
Module structure is an implementation detail, not part of the API:
Decision Trees
When to Use Private Modules + Public Re-exports
✅ Use this pattern when:
- Module structure is an implementation detail
- You want a flat, ergonomic API surface
- Avoiding potential name collisions across the crate
- Working with small to medium-sized modules with clear responsibilities
- Building a library with a stable public API
Example scenarios:
- Utility modules with helpers, types, constants
- Internal parser implementation
- Data structure implementations
When NOT to Use This Pattern
❌ Keep modules public when:
1. Module Structure IS the API
Different domains should be explicit:
pub mod frontend;
pub mod backend;
Why: The separation is meaningful to users. They WANT to know if they're using frontend or
backend APIs.
2. Large Feature Domains
When namespacing provides clarity for 100+ items:
pub mod graphics;
pub mod audio;
pub mod physics;
Why: Flat re-export of 300+ items would be overwhelming. Namespacing aids discovery.
3. Optional/Conditional Features
Make feature boundaries explicit:
#[cfg(feature = "async")]
pub mod async_api;
#[cfg(feature = "serde")]
pub mod serialization;
Why: Users need to know which features enable which APIs.
Inner Modules vs. Separate Files
When organizing code into logical groups, choose between inner modules (same file) and
separate files based on file size and complexity.
Inner Modules (Same File)
✅ Use inner modules when:
- File is small-to-medium (under ~300 lines total)
- Groups are logically related and benefit from proximity
- Comment banners (
// ======) are being used to separate sections
- Each group is relatively small (~20-50 lines)
pub struct AnsiSequenceGenerator;
mod cursor_movement {
use super::*;
impl AnsiSequenceGenerator {
pub fn cursor_position(...) -> String { ... }
pub fn cursor_to_column(...) -> String { ... }
}
}
mod screen_clearing {
use super::*;
impl AnsiSequenceGenerator {
pub fn clear_screen() -> String { ... }
pub fn clear_current_line() -> String { ... }
}
}
mod color_ops {
use super::*;
impl AnsiSequenceGenerator {
pub fn fg_color(...) -> String { ... }
pub fn bg_color(...) -> String { ... }
}
}
Benefits:
- Single-file cohesion - everything related stays together
- Easier navigation - no jumping between files
- Clear grouping -
mod keyword is more formal than comment banners
- Scoped imports - each inner mod can import only what it needs
Separate Files
✅ Use separate files when:
- Individual groups exceed ~100 lines each
- Groups have distinct dependencies (different imports)
- File would exceed ~500 lines total
- Groups are conceptually independent (could be tested separately)
generator/
├── mod.rs # Re-exports + struct definition
├── cursor_movement.rs # impl AnsiSequenceGenerator { cursor_* }
├── screen_clearing.rs # impl AnsiSequenceGenerator { clear_* }
├── color_ops.rs # impl AnsiSequenceGenerator { colors }
└── terminal_modes.rs # impl AnsiSequenceGenerator { modes }
Code Smell: Comment Banners
If you find yourself writing comment banners like this:
impl MyStruct {
fn method_a1() { ... }
fn method_a2() { ... }
fn method_b1() { ... }
fn method_b2() { ... }
}
This is a signal to formalize the grouping using either inner modules (small file) or
separate files (large file). Comment banners are informal and don't provide the same benefits
as actual module boundaries (scoped imports, clear boundaries, IDE navigation).
Complete Examples
Example 1: Simple Module Organization
#![rustfmt::skip]
mod position;
mod size;
mod style;
mod cursor;
mod buffer;
pub use position::*;
pub use size::*;
pub use style::*;
pub use cursor::*;
pub use buffer::*;
Usage:
use terminal::{Position, Size, Style, Cursor, Buffer};
Example 2: Conditional Visibility for Docs
#[cfg(any(test, doc))]
pub mod vt_100;
#[cfg(not(any(test, doc)))]
mod vt_100;
#[cfg(any(test, doc))]
pub mod escape_sequences;
#[cfg(not(any(test, doc)))]
mod escape_sequences;
pub use vt_100::*;
pub use escape_sequences::*;
Now rustdoc can link to these modules:
Example 3: Mixed Public and Private Modules
pub mod backends;
pub mod widgets;
mod buffer;
mod diff_engine;
pub use buffer::RenderBuffer;
Usage:
use rendering::backends::Crossterm;
use rendering::widgets::Button;
use rendering::RenderBuffer;
Common Mistakes
❌ Mistake 1: Everything Public
pub mod constants;
pub mod types;
pub mod helpers;
Problem: Exposes internal structure, hard to refactor later.
❌ Mistake 2: Forgetting Transitive Visibility
#[cfg(any(test, doc))]
pub mod a;
mod b;
Problem: Rustdoc can't resolve links from a to b.
Fix:
#[cfg(any(test, doc))]
pub mod a;
#[cfg(any(test, doc))]
pub mod b;
#[cfg(not(any(test, doc)))]
mod b;
❌ Mistake 3: Using Conditional Visibility Everywhere
#[cfg(any(test, doc))]
pub mod utils;
#[cfg(not(any(test, doc)))]
mod utils;
Problem: Only use conditional visibility when:
- Linking to the module in rustdoc, OR
- Accessing the module from test code
Simple case: If module items are re-exported and you don't need to link to the module itself,
just use private modules.
Reporting Results
After organizing modules:
- ✅ Organized successfully → "Module structure organized with private modules and public re-exports!"
- 🔧 Made conditionally public → Report which modules got conditional visibility
- 📝 Manual review needed → List modules that may need public exposure for API reasons
Supporting Files in This Skill
This skill includes additional reference material:
examples.md - 6 complete, working examples of module organization for different scenarios: simple library with internal structure, conditional visibility for documentation, large crate with domain separation, test-only module visibility, gradual refactoring strategy, and avoiding naming conflicts. Each example shows full file structure and implementation. Read this when:
- Simple library module organization → Example 1
- Need conditional visibility for docs/tests → Example 2
- Large crate with multiple domains (graphics/audio/physics) → Example 3
- Test utilities that should only exist in test builds → Example 4
- Refactoring from public modules to private + re-exports → Example 5
- Avoiding module naming conflicts → Example 6
- Decision tree for when to use which pattern → End of file
Related Skills
write-documentation - For documenting module organization and fixing intra-doc links (uses conditional visibility for linking private types)
run-clippy - Ensures mod.rs follows patterns
Related Commands
No dedicated command, but used by:
/clippy - Checks module organization as part of code quality
/fix-intradoc-links - Uses conditional visibility patterns from this skill
Related Agents
clippy-runner - Invokes this skill to enforce module patterns