summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
Diffstat (limited to 'generator')
-rw-r--r--generator/src/main.rs30
-rw-r--r--generator/src/methods.rs2
-rw-r--r--generator/src/structs.rs3
3 files changed, 21 insertions, 14 deletions
diff --git a/generator/src/main.rs b/generator/src/main.rs
index 3c384c3..986c21f 100644
--- a/generator/src/main.rs
+++ b/generator/src/main.rs
@@ -1,4 +1,4 @@
-use std::ffi::{OsStr, OsString};
+use std::{ffi::OsString, path::PathBuf};
mod methods;
mod openapi;
@@ -9,10 +9,12 @@ use openapi::*;
fn main() -> eyre::Result<()> {
let spec = get_spec()?;
- let mut s = String::new();
- s.push_str(&methods::create_methods(&spec)?);
- s.push_str(&structs::create_structs(&spec)?);
- save_generated(&s)?;
+ let files = [
+ ("mod.rs".into(), "pub mod structs;\npub mod methods;".into()),
+ ("methods.rs".into(), methods::create_methods(&spec)?),
+ ("structs.rs".into(), structs::create_structs(&spec)?),
+ ];
+ save_generated(&files)?;
Ok(())
}
@@ -25,15 +27,21 @@ fn get_spec() -> eyre::Result<OpenApiV2> {
Ok(spec)
}
-fn save_generated(contents: &str) -> eyre::Result<()> {
- let path = std::env::var_os("FORGEJO_API_GENERATED_PATH")
- .unwrap_or_else(|| OsString::from("./src/generated.rs"));
- std::fs::write(path.as_os_str(), contents)?;
- run_rustfmt_on(path.as_os_str());
+fn save_generated(files: &[(String, String)]) -> eyre::Result<()> {
+ let root_path = PathBuf::from(
+ std::env::var_os("FORGEJO_API_GENERATED_PATH")
+ .unwrap_or_else(|| OsString::from("./src/generated/")),
+ );
+ for (path, file) in files {
+ let path = root_path.join(path);
+ std::fs::create_dir_all(path.parent().ok_or_else(|| eyre::eyre!("no parent dir"))?)?;
+ std::fs::write(&path, file)?;
+ run_rustfmt_on(&path);
+ }
Ok(())
}
-fn run_rustfmt_on(path: &OsStr) {
+fn run_rustfmt_on(path: &std::path::Path) {
let mut rustfmt = std::process::Command::new("rustfmt");
rustfmt.arg(path);
diff --git a/generator/src/methods.rs b/generator/src/methods.rs
index 70ebe99..c0f614c 100644
--- a/generator/src/methods.rs
+++ b/generator/src/methods.rs
@@ -6,6 +6,8 @@ use std::fmt::Write;
pub fn create_methods(spec: &OpenApiV2) -> eyre::Result<String> {
let mut s = String::new();
s.push_str("use crate::ForgejoError;\n");
+ s.push_str("use super::structs::*;\n");
+ s.push_str("\n");
s.push_str("impl crate::Forgejo {\n");
for (path, item) in &spec.paths {
s.push_str(&create_methods_for_path(&spec, path, item).wrap_err_with(|| path.clone())?);
diff --git a/generator/src/structs.rs b/generator/src/structs.rs
index 84655e9..04d3039 100644
--- a/generator/src/structs.rs
+++ b/generator/src/structs.rs
@@ -5,8 +5,6 @@ use std::fmt::Write;
pub fn create_structs(spec: &OpenApiV2) -> eyre::Result<String> {
let mut s = String::new();
- s.push_str("use structs::*;\n");
- s.push_str("pub mod structs {\n");
s.push_str("use crate::StructureError;");
if let Some(definitions) = &spec.definitions {
for (name, schema) in definitions {
@@ -32,7 +30,6 @@ pub fn create_structs(spec: &OpenApiV2) -> eyre::Result<String> {
let strukt = create_response_structs(spec, item)?;
s.push_str(&strukt);
}
- s.push_str("\n}");
Ok(s)
}