From 0753fa86213fe9436c122dbada5db2380b27385c Mon Sep 17 00:00:00 2001 From: Jeb Bearer Date: Fri, 29 Mar 2024 13:07:14 -0400 Subject: [PATCH 1/2] Improve Display for Version The previous implementation simply printed the struct's docstring, which is not very useful. The new implementation prints a specific version in {major}.{minor} format, which makes for better error messages when deserialization fails due to version mismatch. --- Cargo.toml | 4 ++-- src/version.rs | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0778fb3..cb4c22b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "vbs" -version = "0.1.3" +version = "0.1.4" edition = "2021" rust-version = "1.75.0" authors = ["Espresso Systems "] @@ -12,7 +12,7 @@ license = "MIT" [dependencies] anyhow = "1.0" +derive_more = "0.99" bincode = "1.3" -displaydoc = "0.2" serde = { version = "1.0", features = ["derive"] } serde_with = "3.4" diff --git a/src/version.rs b/src/version.rs index 44d6019..7b1104d 100644 --- a/src/version.rs +++ b/src/version.rs @@ -1,10 +1,11 @@ use anyhow::anyhow; use core::fmt::Debug; -use displaydoc::Display; +use derive_more::Display; use serde::{Deserialize, Serialize}; #[derive(Clone, Copy, Debug, Deserialize, Display, Eq, Hash, PartialEq, Serialize)] /// Type for protocol version number +#[display(fmt = "{major}.{minor}")] pub struct Version { /// major version number pub major: u16, @@ -82,3 +83,13 @@ mod private { // Implement for those same types, but no others. impl Sealed for super::StaticVersion {} } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_version_display() { + assert_eq!("1.2", Version { major: 1, minor: 2 }.to_string()); + } +} From bd81bd0430d3f3c09089193740f4a067f90554c3 Mon Sep 17 00:00:00 2001 From: Jeb Bearer Date: Fri, 29 Mar 2024 15:24:39 -0400 Subject: [PATCH 2/2] Fix display for StaticVersion --- src/version.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/version.rs b/src/version.rs index 7b1104d..f2ed797 100644 --- a/src/version.rs +++ b/src/version.rs @@ -50,6 +50,7 @@ pub trait StaticVersionType: Sync + Send + Clone + Copy + Debug + private::Seale } #[derive(Clone, Copy, Display)] +#[display(fmt = "{MAJOR}.{MINOR}")] pub struct StaticVersion {} impl StaticVersionType for StaticVersion { @@ -92,4 +93,9 @@ mod test { fn test_version_display() { assert_eq!("1.2", Version { major: 1, minor: 2 }.to_string()); } + + #[test] + fn test_static_version_display() { + assert_eq!("1.2", StaticVersion::<1, 2> {}.to_string()); + } }