Skip to content

Commit 632477c

Browse files
committed
Remove Float on the Python-facing side
Format generated output with rustfmt
1 parent 932db13 commit 632477c

6 files changed

Lines changed: 278 additions & 161 deletions

File tree

Cargo.lock

Lines changed: 87 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codegen/main.rs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
use eyre::ContextCompat;
1+
use eyre::{Context, ContextCompat};
22
use planus_types::{ast::IntegerType, intermediate::DeclarationKind};
3-
use std::{env::set_current_dir, fs, path::Path};
3+
use std::{
4+
env::set_current_dir,
5+
fs,
6+
io::Write,
7+
path::Path,
8+
process::{Command, Stdio},
9+
};
410

511
use crate::{
612
enums::EnumBindGenerator, structs::StructBindGenerator, table::TableBindGenerator,
@@ -80,6 +86,46 @@ fn camel_to_snake(input: &str) -> String {
8086
snake_case
8187
}
8288

89+
/// Taken from <https://github.com/planus-org/planus/blob/main/crates/planus-codegen/src/rust/mod.rs#L1014>
90+
///
91+
/// This formats a string using `rustfmt` (using Rust 2024 and not 2021)
92+
fn format_string(s: &str) -> eyre::Result<String> {
93+
let mut child = Command::new("rustfmt");
94+
95+
child
96+
.arg("--edition=2024")
97+
.stdin(Stdio::piped())
98+
.stdout(Stdio::piped())
99+
.stderr(Stdio::piped());
100+
101+
let mut child = child
102+
.spawn()
103+
.wrap_err("Unable to spawn rustfmt. Perhaps it is not installed?")?;
104+
105+
{
106+
let child_stdin = child.stdin.as_mut().unwrap();
107+
child_stdin
108+
.write_all(s.as_bytes())
109+
.wrap_err("Unable to write the file to rustfmt")?;
110+
}
111+
112+
let output = child
113+
.wait_with_output()
114+
.wrap_err("Unable to get the formatted file back from rustfmt")?;
115+
116+
if output.status.success() && output.stderr.is_empty() {
117+
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
118+
} else if output.stderr.is_empty() {
119+
eyre::bail!("rustfmt failed with exit code {}", output.status);
120+
} else {
121+
eyre::bail!(
122+
"rustfmt failed with exit code {} and message:\n{}",
123+
output.status,
124+
String::from_utf8_lossy(&output.stderr).into_owned(),
125+
)
126+
}
127+
}
128+
83129
fn main() -> eyre::Result<()> {
84130
set_current_dir(env!("CARGO_MANIFEST_DIR")).unwrap();
85131

@@ -121,6 +167,11 @@ fn main() -> eyre::Result<()> {
121167
// generate custom code
122168
for (path, item) in &declarations.declarations {
123169
let item_name = path.0.last().unwrap().as_str();
170+
if item_name == "Float" {
171+
// Special case for Float (we always inline Float into Py<PyFloat>)
172+
continue;
173+
}
174+
124175
let mut file_name = camel_to_snake(item_name);
125176

126177
let file_contents = match &item.kind {
@@ -153,7 +204,10 @@ fn main() -> eyre::Result<()> {
153204
);
154205
file_name.push_str(".rs");
155206

156-
fs::write(python_folder.join(&file_name), file_contents.join("\n"))?;
207+
fs::write(
208+
python_folder.join(&file_name),
209+
format_string(&file_contents.join("\n"))?,
210+
)?;
157211
python_files.push(file_name);
158212
}
159213

@@ -185,7 +239,7 @@ fn main() -> eyre::Result<()> {
185239
.replace("::serde::Serialize,", "")
186240
.replace("::serde::Deserialize,", "");
187241

188-
fs::write(OUT_FILE, generated_planus.as_bytes())?;
242+
fs::write(OUT_FILE, format_string(&generated_planus)?.as_bytes())?;
189243

190244
class_inject::classes_to_lib_rs(class_names)?;
191245
pyi::generator(&declarations.declarations)?;

0 commit comments

Comments
 (0)