Skip to content

Commit

Permalink
Improve error reporting (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
TethysSvensson authored Feb 4, 2023
1 parent b871e31 commit 3b4a697
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
18 changes: 11 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- \[Rust\]: Bump the Minimum Support Rust Version (MSRV) to 1.61.0.
- \[Rust\]: Add license files to crates
- \[Rust\]: Implement a builder pattern for serializing tables and unions
- \[Rust\]: rustfmt runs twice on generated code to ensure it is formatted correctly
- \[Rust\]: Fix an issue with using structs across different flatbuffer namespaces.
- Add support for docstrings, and add them to the Rust output.
- Update the `README` with information about our Discord server.
- Split up `planus-cli` into multiple crates
- Fix planus failing with "Unexpected token" on CLRF
- Add support for docstrings, and add them to the Rust output. [#152](https://github.com/planus-org/planus/pull/152)
- Update the `README` with information about our Discord server [#138](https://github.com/planus-org/planus/pull/138)
- Split up `planus-cli` into multiple crates [#161](https://github.com/planus-org/planus/pull/161)

### Fixed
- Fix planus failing with "Unexpected token" on CLRF [#166](https://github.com/planus-org/planus/pull/166)
- \[Rust\]: rustfmt runs twice on generated code to ensure it is formatted correctly [#168](https://github.com/planus-org/planus/pull/168)
- \[Rust\]: Fix an issue with using structs across different flatbuffer namespaces [#172](https://github.com/planus-org/planus/pull/172)
- \[Rust\]: Fix bad error reporting when `rustfmt` was not installed [#173](https://github.com/planus-org/planus/pull/173)

### Removed
- \[Rust\]: The old ways of serializing tables and unions using `create`-functions have been removed.
Expand Down Expand Up @@ -66,7 +69,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.1.0] - 2021-12-30
- Initial release

[Unreleased]: https://github.com/planus-org/planus/compare/v0.3.0...HEAD
[Unreleased]: https://github.com/planus-org/planus/compare/v0.3.1...HEAD
[0.3.1]: https://github.com/planus-org/planus/compare/v0.3.0...v0.3.1
[0.3.0]: https://github.com/planus-org/planus/compare/v0.2.0...v0.3.0
[0.2.0]: https://github.com/planus-org/planus/compare/v0.1.0...v0.2.0
[0.1.0]: https://github.com/planus-org/planus/releases/tag/v0.1.0
2 changes: 1 addition & 1 deletion RELEASE-CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* `planus/src/lib.rs`
* `planus-cli/src/codegen/templates/rust/namespace.template`
* `examples/rust/Cargo.toml`
* Update CHANGELOG.md
* Update CHANGELOG.md, including links at the bottom
* Commit changes to a branch and make a PR
* Run `cargo publish --dry-run` on PR branch
* Wait for CI and merge PR to main branch
Expand Down
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 3b4a697

Please sign in to comment.