From ebffd7a9220f1ef56244ba8f458c2105d2a2a46b Mon Sep 17 00:00:00 2001 From: xenofem Date: Tue, 16 Aug 2022 03:37:09 -0400 Subject: [PATCH] Prevent catchall schema types from trying to incorporate the version field during deserialization --- Cargo.toml | 2 +- src/lib.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 71fa146..e1939ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsondb" -version = "0.3.0" +version = "0.3.1" edition = "2021" authors = ["xenofem "] license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index 4ddc9af..2b2b3e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,7 +74,7 @@ pub trait Schema: Send + Sync + Debug + DeserializeOwned + Serialize + 'static { let Version { version } = serde_json::from_str(s)?; match version.cmp(&Self::VERSION) { Ordering::Less => Ok(Self::Prev::parse(s)?.into()), - Ordering::Equal => Ok(serde_json::from_str(s)?), + Ordering::Equal => Ok(serde_json::from_str::>(s)?.data), Ordering::Greater => Err(Error::UnknownVersion(version)), } } @@ -110,11 +110,11 @@ struct Version { version: u32, } -#[derive(Serialize)] -struct Repr<'a, T: Schema> { +#[derive(Deserialize, Serialize)] +struct VersionedData { version: u32, #[serde(flatten)] - data: &'a T, + data: T, } /// Errors that can occur while working with [`JsonDb`]. @@ -145,7 +145,7 @@ async fn save(data: &T, path: &Path) -> Result<(), Error> { { let mut temp_file = File::create(&temp_file_path).await?; temp_file - .write_all(&serde_json::to_vec_pretty(&Repr { + .write_all(&serde_json::to_vec_pretty(&VersionedData { version: T::VERSION, data, })?)