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

main
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]
name = "jsondb"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
authors = ["xenofem <xenofem@xeno.science>"]
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)?;
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::<VersionedData<Self>>(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<T> {
version: u32,
#[serde(flatten)]
data: &'a T,
data: T,
}
/// 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?;
temp_file
.write_all(&serde_json::to_vec_pretty(&Repr {
.write_all(&serde_json::to_vec_pretty(&VersionedData {
version: T::VERSION,
data,
})?)