diff options
author | aviac <aviac@mailbox.org> | 2024-01-22 09:21:46 +0100 |
---|---|---|
committer | aviac <aviac@mailbox.org> | 2024-01-22 09:21:46 +0100 |
commit | 9566d232bf1f0333be3a89b2766713e0b74f3b61 (patch) | |
tree | 81a333cbf6715affcd870b863d10ed8974ceac0c | |
parent | chore(cleanup): consolidate `writeln`s (diff) | |
download | forgejo-api-9566d232bf1f0333be3a89b2766713e0b74f3b61.tar.xz forgejo-api-9566d232bf1f0333be3a89b2766713e0b74f3b61.zip |
feat(formatting): auto run rustfmt on generated file
Authored-by: Aviac <aviac@mailbox.org>
-rw-r--r-- | generator/src/main.rs | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/generator/src/main.rs b/generator/src/main.rs index 4e56d5e..7e2d290 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1,4 +1,4 @@ -use std::ffi::OsString; +use std::ffi::{OsStr, OsString}; mod openapi; @@ -34,7 +34,7 @@ fn main() -> eyre::Result<()> { s.push_str(&strukt); } s.push_str("\n}"); - save_generated(&mut s)?; + save_generated(&s)?; Ok(()) } @@ -49,10 +49,23 @@ fn get_spec() -> eyre::Result<OpenApiV2> { 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, contents)?; + std::fs::write(path.as_os_str(), contents)?; + run_rustfmt_on(path.as_os_str()); Ok(()) } +fn run_rustfmt_on(path: &OsStr) { + let mut rustfmt = std::process::Command::new("rustfmt"); + + rustfmt.arg(path); + rustfmt.args(["--edition", "2021"]); + + if let Err(e) = rustfmt.status() { + println!("Tried to format {path:?}, but failed to do so! :("); + println!("Error:\n{e}"); + } +} + fn create_methods_for_path( spec: &OpenApiV2, path: &str, @@ -1049,9 +1062,7 @@ impl std::fmt::Display for {op_name}Query {{ Ok(()) }} }} -", - fields = fields.replace('\n', "\n ").trim(), - imp = imp.replace('\n', "\n ").trim() +" ) }; |