Skip to content

Commit

Permalink
Fixes #171
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson committed Feb 4, 2023
1 parent b871e31 commit 53a7d8f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
1 change: 1 addition & 0 deletions crates/planus-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ heck = "0.4.0"
vec_map = "0.8.2"
random_color = "0.6.1"
thiserror = "1.0.38"
eyre = "0.6.8"
13 changes: 3 additions & 10 deletions crates/planus-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ mod dot;
mod rust;
mod templates;

#[derive(thiserror::Error, Debug)]
pub enum CodegenError {
#[error("io error")]
Io(#[from] std::io::Error),
#[error("codegen error: {0}")]
Other(String),
}

pub fn generate_rust(declarations: &Declarations) -> Result<String, CodegenError> {
pub fn generate_rust(declarations: &Declarations) -> eyre::Result<String> {
let default_analysis = run_analysis(declarations, &mut rust::analysis::DefaultAnalysis);
let eq_analysis = run_analysis(declarations, &mut rust::analysis::EqAnalysis);
let infallible_analysis = run_analysis(
Expand All @@ -43,7 +35,8 @@ pub fn generate_rust(declarations: &Declarations) -> Result<String, CodegenError
declarations,
);
let res = templates::rust::Namespace(&output).render().unwrap();
let res = rust::format_string(&rust::format_string(&res, Some(1_000_000))?, None)?;
let res = rust::format_string(&res, Some(1_000_000))?;
let res = rust::format_string(&res, None)?;
Ok(res)
}

Expand Down
24 changes: 14 additions & 10 deletions crates/planus-codegen/src/rust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
process::{Command, Stdio},
};

use eyre::Context;
use heck::{ToSnakeCase, ToUpperCamelCase};
use planus_types::{
ast::{FloatType, IntegerType},
Expand Down Expand Up @@ -959,7 +960,7 @@ fn float_type(type_: &FloatType) -> &'static str {
}
}

pub fn format_string(s: &str, max_width: Option<u64>) -> Result<String, crate::CodegenError> {
pub fn format_string(s: &str, max_width: Option<u64>) -> eyre::Result<String> {
let mut child = Command::new("rustfmt");

child
Expand All @@ -973,27 +974,30 @@ pub fn format_string(s: &str, max_width: Option<u64>) -> Result<String, crate::C
child.arg(format!("max_width={max_width}"));
}

let mut child = child.spawn()?;
let mut child = child
.spawn()
.wrap_err("Unable to spawn rustfmt. Perhaps it is not installed?")?;

{
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin.write_all(s.as_bytes())?;
child_stdin
.write_all(s.as_bytes())
.wrap_err("Unable to write the file to rustfmt")?;
}

let output = child.wait_with_output()?;
let output = child
.wait_with_output()
.wrap_err("Unable to get the formatted file back from rustfmt")?;

if output.status.success() && output.stderr.is_empty() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else if output.stderr.is_empty() {
Err(crate::CodegenError::Other(format!(
"rustfmt failed with exit code {}",
output.status
)))
eyre::bail!("rustfmt failed with exit code {}", output.status);
} else {
Err(crate::CodegenError::Other(format!(
eyre::bail!(
"rustfmt failed with exit code {} and message:\n{}",
output.status,
String::from_utf8_lossy(&output.stderr).into_owned(),
)))
)
}
}

0 comments on commit 53a7d8f

Please sign in to comment.