Prevent catchall schema types from trying to incorporate the version field during deserialization

This commit is contained in:
xenofem 2022-08-16 03:37:09 -04:00
parent 1a6bb519ef
commit ebffd7a922
2 changed files with 6 additions and 6 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "jsondb" name = "jsondb"
version = "0.3.0" version = "0.3.1"
edition = "2021" edition = "2021"
authors = ["xenofem <xenofem@xeno.science>"] authors = ["xenofem <xenofem@xeno.science>"]
license = "MIT" license = "MIT"

View file

@ -74,7 +74,7 @@ pub trait Schema: Send + Sync + Debug + DeserializeOwned + Serialize + 'static {
let Version { version } = serde_json::from_str(s)?; let Version { version } = serde_json::from_str(s)?;
match version.cmp(&Self::VERSION) { match version.cmp(&Self::VERSION) {
Ordering::Less => Ok(Self::Prev::parse(s)?.into()), Ordering::Less => Ok(Self::Prev::parse(s)?.into()),
Ordering::Equal => Ok(serde_json::from_str(s)?), Ordering::Equal => Ok(serde_json::from_str::<VersionedData<Self>>(s)?.data),
Ordering::Greater => Err(Error::UnknownVersion(version)), Ordering::Greater => Err(Error::UnknownVersion(version)),
} }
} }
@ -110,11 +110,11 @@ struct Version {
version: u32, version: u32,
} }
#[derive(Serialize)] #[derive(Deserialize, Serialize)]
struct Repr<'a, T: Schema> { struct VersionedData<T> {
version: u32, version: u32,
#[serde(flatten)] #[serde(flatten)]
data: &'a T, data: T,
} }
/// Errors that can occur while working with [`JsonDb`]. /// Errors that can occur while working with [`JsonDb`].
@ -145,7 +145,7 @@ async fn save<T: Schema>(data: &T, path: &Path) -> Result<(), Error> {
{ {
let mut temp_file = File::create(&temp_file_path).await?; let mut temp_file = File::create(&temp_file_path).await?;
temp_file temp_file
.write_all(&serde_json::to_vec_pretty(&Repr { .write_all(&serde_json::to_vec_pretty(&VersionedData {
version: T::VERSION, version: T::VERSION,
data, data,
})?) })?)