Spaceship DSL
A type-safe Domain Specific Language (DSL) for building spaceships in Rust with compile-time validation.
Environment
Note
This project is using nightly Rust features (for compile-time slot checking). Make sure to use a nightly toolchain. Although there is a
rust-toolchain.tomlfile included in the project, you can also set the override manually by running:rustup override set nightly
Usage
The create_spaceship! Macro
The create_spaceship! macro provides a declarative way to build spaceship blueprints with compile-time safety guarantees. It ensures that all required modules are installed and validates slot availability and module compatibility.
Syntax
let spaceship = create_spaceship!(
core {
<slot_name> <ModuleImpl>,
...
}
optional {
<slot_name> <ModuleImpl>,
...
}
)
Or without optional modules:
let spaceship = create_spaceship!(
core {
<slot_name> <ModuleImpl>,
...
}
)
Module Types
Core Modules (Required)
All core modules must be specified for a valid spaceship:
-
reactor- Power generation system (occupies 3 slots)- Example:
AntimatterReactor,FusionReactor
- Example:
-
engine- Propulsion system (occupies 2 slots)- Example:
IonEngine,WarpDrive
- Example:
-
life_support- Life support system (occupies 2 slots)- Example:
AdvancedLifeSupport,BasicLifeSupport
- Example:
-
bridge- Command and control center (occupies 1 slot)- Example:
CommandBridge,NavigationBridge
- Example:
Optional Modules
These modules can be added to enhance spaceship capabilities:
-
shield- Defensive system (occupies 1 slot)- Example:
PhaseShield,EnergyShield - Note: May require specific reactor types
- Example:
-
sensors- Detection and scanning system (occupies 1 slot)- Example:
AdvancedSensors,BasicSensors
- Example:
Available Modules
Reactors
AntimatterReactorFusionReactor
Engines
IonEngineWarpDrive
Life Support Systems
AdvancedLifeSupportBasicLifeSupport
Bridges
CommandBridgeNavigationBridge
Shields
PhaseShieldEnergyShield
Sensors
AdvancedSensorsBasicSensors
Example
use spaceship::{
blueprint::module::{
bridge::CommandBridge,
engine::IonEngine,
life_support::AdvancedLifeSupport,
reactor::AntimatterReactor,
shield::PhaseShield,
},
create_spaceship,
};
fn main() {
let spaceship = create_spaceship!(
core {
reactor AntimatterReactor,
engine IonEngine,
life_support AdvancedLifeSupport,
bridge CommandBridge,
}
optional {
shield PhaseShield,
}
);
spaceship.print_spec();
}
Features
- Type-Safe: The macro enforces module installation order and validates compatibility at compile time
- Slot Management: Automatically tracks and validates slot usage across modules
- Power Balance: Ensures power generation meets consumption requirements
- Staged Construction: Uses a type-state pattern to prevent invalid configurations
Blueprint Specification
After creating a spaceship, you can call .print_spec() to view:
- Total, used, and available slots
- Power output vs power consumption
- Total mass and thrust
- Thrust-to-weight ratio
Compile-Time Guarantees
The macro provides several compile-time guarantees:
- All core modules must be installed - Missing any core module will result in a compile error
- Slot availability - Exceeding slot limits is caught at compile time
- Module compatibility - Certain modules may require specific reactors or other dependencies
- Installation order - Core modules must be installed before optional modules