shared-code-check
Ensures shared logic goes to loh-libs instead of being duplicated
Shared Code Guardrail
Code Placement Decision Tree
When writing new code, ask:
1. Is this SHARED between loh-game AND loh-backend?
└─> YES → loh-libs/rust/shared-protocol/
2. Is this UI-specific (Egui, Bevy rendering, input handling)?
└─> YES → loh-game/src/systems/ui/ or relevant system
3. Is this backend-specific (API handlers, database, auth)?
└─> YES → loh-backend/Infrastructure Context
What Goes to loh-libs
- DTOs (Data Transfer Objects) for API communication
- Enums shared between client/server (e.g.,
OrderSide,SkillType) - Validation logic that must be consistent
- Math/calculation utilities used by both
- Combat damage formulas, XP tables, loot calculations
What Stays in loh-game
- Bevy-specific:
Component,Resource,SystemParam - UI rendering (Egui widgets, panels, themes)
- Input handling, camera controls
- VFX, audio, rendering systems
- Game loop, player state
What Stays in loh-backend
- sqlx derives (
FromRow,Type) - Cloudflare Worker handlers
- Database migrations, API routes
- Authentication (JWT, sessions)
- Payment processing
Check Before Adding Types
- Does this type appear in both codebases?
- Does it need to serialize/deserialize for network communication?
- Is validation logic duplicated?
If YES to any → move to
loh-libs/rust/shared-protocol/src/types/Directory Structure
loh-libs/rust/shared-protocol/src/types/
├── combat.rs # Combat stats, damage types
├── economy.rs # Market orders, snapshots (OrderSide, MarketSnapshot)
├── item.rs # Item definitions
├── loot.rs # Loot tables
├── npc.rs # NPC data
└── skill.rs # Skill typesAdding New Shared Types
- Create/edit file in
shared-protocol/src/types/ - Add
pub mod X;totypes/mod.rs - Add
pub use types::X::*;tolib.rs - Update both
loh-game/Cargo.tomlandloh-backend/*/Cargo.tomlif needed
Automated Check
Run the guardrail script before committing:
// turbo
python3 scripts/check_shared_code.pyThis script scans for duplicate file names across
loh-game and loh-backend.
If duplicates are found, consider moving shared logic to loh-libs.