Skip to content

Commit 2a9a9a4

Browse files
Merge pull request #17 from rohas-dev/feat/rust-runtime
feat(rust): add Rust language support for code generation, including handler registration and runtime execution; update dependencies and configuration for Rust integration
2 parents f0b6454 + a001ccb commit 2a9a9a4

40 files changed

Lines changed: 4950 additions & 43 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ target
1313
# Contains mutation testing data
1414
**/mutants.out*/
1515

16+
# MacOS specific files
17+
.DS_Store
18+
1619
# RustRover
1720
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
1821
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore

Cargo.lock

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/rohas-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = { workspace = true }
44
edition = { workspace = true }
55
authors = { workspace = true }
66
license = { workspace = true }
7+
default-run = "rohas"
78

89
[[bin]]
910
name = "rohas"

crates/rohas-cli/src/commands/codegen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn engine_language_to_codegen_language(lang: EngineLanguage) -> Language {
1515
match lang {
1616
EngineLanguage::TypeScript => Language::TypeScript,
1717
EngineLanguage::Python => Language::Python,
18+
EngineLanguage::Rust => Language::Rust,
1819
}
1920
}
2021

@@ -35,6 +36,7 @@ pub async fn execute(
3536
let language = match lang.as_deref() {
3637
Some("typescript") | Some("ts") => Language::TypeScript,
3738
Some("python") | Some("py") => Language::Python,
39+
Some("rust") | Some("rs") => Language::Rust,
3840
None => match &config_path {
3941
Some(config_path) => match EngineConfig::from_file(config_path) {
4042
Ok(config) => {

crates/rohas-cli/src/commands/dev.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,48 @@ pub async fn execute(
1414
) -> Result<()> {
1515
info!("Starting development server...");
1616

17-
let mut config = match EngineConfig::from_project_root() {
18-
Ok(config) => {
19-
info!("Loaded configuration from config/rohas.toml");
20-
config
21-
}
22-
Err(e) => {
23-
info!("Using default configuration ({})", e);
24-
EngineConfig::default()
25-
}
26-
};
27-
config.project_root = std::env::current_dir()?;
2817
let actual_path = if !schema_path.exists() && schema_path.ends_with("index.ro") {
2918
schema_path
3019
.parent()
3120
.map(|p| p.to_path_buf())
3221
.unwrap_or(schema_path)
3322
} else {
34-
schema_path
23+
schema_path.clone()
24+
};
25+
26+
let project_root = if actual_path.file_name()
27+
.and_then(|s| s.to_str())
28+
.map(|s| s == "schema")
29+
.unwrap_or(false)
30+
{
31+
actual_path
32+
.parent()
33+
.map(|p| p.to_path_buf())
34+
.unwrap_or_else(|| std::env::current_dir().unwrap_or_default())
35+
} else {
36+
actual_path.clone()
37+
};
38+
39+
let config_path = project_root.join("config").join("rohas.toml");
40+
let mut config = if config_path.exists() {
41+
match EngineConfig::from_file(&config_path) {
42+
Ok(mut cfg) => {
43+
cfg.project_root = project_root.clone();
44+
info!("Loaded configuration from {}", config_path.display());
45+
cfg
46+
}
47+
Err(e) => {
48+
info!("Failed to load config from {}: {}. Using defaults.", config_path.display(), e);
49+
let mut cfg = EngineConfig::default();
50+
cfg.project_root = project_root.clone();
51+
cfg
52+
}
53+
}
54+
} else {
55+
info!("Config file not found: {}. Using default configuration.", config_path.display());
56+
let mut cfg = EngineConfig::default();
57+
cfg.project_root = project_root.clone();
58+
cfg
3559
};
3660

3761
let dev_server = DevServer::new(actual_path, config.clone(), watch);

crates/rohas-codegen/src/config.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ target-version = "py39"
145145
Ok(())
146146
}
147147

148+
pub fn generate_cargo_toml(_schema: &Schema, output_dir: &Path) -> Result<()> {
149+
let project_root = get_project_root(output_dir);
150+
let project_name = extract_project_name(&project_root);
151+
152+
let lib_name = project_name.replace('-', "_");
153+
154+
let content = format!(
155+
r#"[package]
156+
name = "{}"
157+
version = "0.1.0"
158+
edition = "2021"
159+
160+
[workspace]
161+
162+
[lib]
163+
name = "{}"
164+
path = "src/lib.rs"
165+
166+
[dependencies]
167+
rohas-runtime = {{ path = "../../crates/rohas-runtime" }}
168+
serde = {{ version = "1.0", features = ["derive"] }}
169+
serde_json = "1.0"
170+
tokio = {{ version = "1.0", features = ["full"] }}
171+
chrono = {{ version = "0.4", features = ["serde"] }}
172+
tracing = "0.1"
173+
174+
[dev-dependencies]
175+
tokio-test = "0.4"
176+
"#,
177+
project_name,
178+
lib_name
179+
);
180+
181+
fs::write(project_root.join("Cargo.toml"), content)?;
182+
Ok(())
183+
}
184+
148185
pub fn generate_gitignore(_schema: &Schema, output_dir: &Path) -> Result<()> {
149186
let project_root = get_project_root(output_dir);
150187
let content = r#"# Dependencies

crates/rohas-codegen/src/generator.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::Result;
2-
use crate::{config, python, typescript, Language};
2+
use crate::{config, python, rust, typescript, Language};
33
use rohas_parser::Schema;
44
use std::fs;
55
use std::path::Path;
@@ -28,6 +28,7 @@ impl Generator {
2828
match self.language {
2929
Language::TypeScript => self.generate_typescript(schema, output_dir)?,
3030
Language::Python => self.generate_python(schema, output_dir)?,
31+
Language::Rust => self.generate_rust(schema, output_dir)?,
3132
}
3233

3334
info!("Code generation completed successfully");
@@ -109,4 +110,25 @@ impl Generator {
109110

110111
Ok(())
111112
}
113+
114+
fn generate_rust(&self, schema: &Schema, output_dir: &Path) -> Result<()> {
115+
rust::generate_state(output_dir)?;
116+
rust::generate_models(schema, output_dir)?;
117+
rust::generate_dtos(schema, output_dir)?;
118+
rust::generate_apis(schema, output_dir)?;
119+
rust::generate_events(schema, output_dir)?;
120+
rust::generate_crons(schema, output_dir)?;
121+
rust::generate_websockets(schema, output_dir)?;
122+
rust::generate_middlewares(schema, output_dir)?;
123+
rust::generate_lib_rs(schema, output_dir)?;
124+
125+
info!("Generating Rust configuration files");
126+
config::generate_cargo_toml(schema, output_dir)?;
127+
128+
if rust::is_in_rohas_workspace(output_dir) {
129+
rust::generate_dev_scripts(output_dir)?;
130+
}
131+
132+
Ok(())
133+
}
112134
}

crates/rohas-codegen/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod config;
22
pub mod error;
33
pub mod generator;
44
pub mod python;
5+
pub mod rust;
56
pub mod templates;
67
pub mod typescript;
78

@@ -15,6 +16,7 @@ use std::path::Path;
1516
pub enum Language {
1617
TypeScript,
1718
Python,
19+
Rust,
1820
}
1921

2022
pub fn generate(schema: &Schema, output_dir: &Path, lang: Language) -> Result<()> {

0 commit comments

Comments
 (0)