Skip to content

Commit

Permalink
Merge pull request #398 from stepchowfun/dry-rust
Browse files Browse the repository at this point in the history
Pull a string out into a constant
  • Loading branch information
stepchowfun authored Jan 11, 2022
2 parents 1fc2b14 + ae93b5b commit 9fac934
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.1] - 2022-01-11

### Changed
- The generated Rust code is now slightly more DRY.

## [0.9.0] - 2022-01-11

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typical"
version = "0.9.0"
version = "0.9.1"
authors = ["Stephan Boyer <[email protected]>"]
edition = "2021"
description = "Algebraic data types for data interchange."
Expand Down
5 changes: 4 additions & 1 deletion src/generate_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ use std::{{
mem::transmute,
}};
const MISSING_FIELDS_ERROR_MESSAGE: &str = \"Struct missing one or more required field(s).\";
pub trait Serialize {{
fn size(&self) -> usize;
Expand Down Expand Up @@ -638,7 +640,8 @@ fn write_schema<T: Write>(
write_indentation(buffer, indentation + 4)?;
writeln!(buffer, "::std::io::ErrorKind::InvalidData,")?;
write_indentation(buffer, indentation + 4)?;
writeln!(buffer, "\"Struct missing one or more field(s).\",")?;
write_supers(buffer, indentation)?;
writeln!(buffer, "MISSING_FIELDS_ERROR_MESSAGE,")?;
write_indentation(buffer, indentation + 3)?;
writeln!(buffer, "));")?;
write_indentation(buffer, indentation + 2)?;
Expand Down
14 changes: 8 additions & 6 deletions test_data/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
mem::transmute,
};

const MISSING_FIELDS_ERROR_MESSAGE: &str = "Struct missing one or more required field(s).";

pub trait Serialize {
fn size(&self) -> usize;

Expand Down Expand Up @@ -306,7 +308,7 @@ pub mod circular_dependency {
if _x.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down Expand Up @@ -470,7 +472,7 @@ pub mod circular_dependency {
|| _fallback.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down Expand Up @@ -2755,7 +2757,7 @@ pub mod comprehensive {
|| _aa_required.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down Expand Up @@ -7902,7 +7904,7 @@ pub mod schema_evolution {
|| _optional_to_required.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down Expand Up @@ -8620,7 +8622,7 @@ pub mod schema_evolution {
|| _required_to_nonexistent.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down Expand Up @@ -9197,7 +9199,7 @@ pub mod schema_evolution {
if _x.is_none() {
return Err(::std::io::Error::new(
::std::io::ErrorKind::InvalidData,
"Struct missing one or more field(s).",
super::super::MISSING_FIELDS_ERROR_MESSAGE,
));
}

Expand Down

0 comments on commit 9fac934

Please sign in to comment.