split generation into method and struct files
This commit is contained in:
parent
07806529f9
commit
7938d81544
|
@ -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);
|
||||
|
|
|
@ -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())?);
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load diff
2
src/generated/mod.rs
Normal file
2
src/generated/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod methods;
|
||||
pub mod structs;
|
6021
src/generated/structs.rs
Normal file
6021
src/generated/structs.rs
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue