Compare commits
No commits in common. "14fd399403f15949b694ccb84aa2d97316890ee6" and "ebffd7a9220f1ef56244ba8f458c2105d2a2a46b" have entirely different histories.
14fd399403
...
ebffd7a922
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "jsondb"
|
name = "jsondb"
|
||||||
version = "0.3.2"
|
version = "0.3.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
authors = ["xenofem <xenofem@xeno.science>"]
|
authors = ["xenofem <xenofem@xeno.science>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
99
src/lib.rs
99
src/lib.rs
|
@ -100,10 +100,8 @@ impl<T: SchemaV0> Schema for T {
|
||||||
if version != 0 {
|
if version != 0 {
|
||||||
return Err(Error::UnknownVersion(version));
|
return Err(Error::UnknownVersion(version));
|
||||||
}
|
}
|
||||||
Ok(serde_json::from_str::<VersionedData<Self>>(s)?.data)
|
|
||||||
} else {
|
|
||||||
Ok(serde_json::from_str(s)?)
|
|
||||||
}
|
}
|
||||||
|
Ok(serde_json::from_str(s)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -299,7 +297,7 @@ impl<T: Schema> JsonDb<T> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{collections::HashMap, fs::File};
|
use std::fs::File;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
@ -522,97 +520,4 @@ mod tests {
|
||||||
assert_eq!(&value["last_updated"], "2022-08-15T17:47:18Z");
|
assert_eq!(&value["last_updated"], "2022-08-15T17:47:18Z");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn catchall_schema_v0() {
|
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
struct Catchall(HashMap<String, String>);
|
|
||||||
impl SchemaV0 for Catchall {}
|
|
||||||
|
|
||||||
let mut data = HashMap::new();
|
|
||||||
data.insert("hello".into(), "world".into());
|
|
||||||
data.insert("catch".into(), "all".into());
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Catchall::parse(r#"{"version":0,"hello":"world","catch":"all"}"#).unwrap(),
|
|
||||||
Catchall(data)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn catchall_schema_v1_v2() {
|
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
struct Catchall0(HashMap<String, String>);
|
|
||||||
impl SchemaV0 for Catchall0 {}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
struct Catchall1 {
|
|
||||||
count: usize,
|
|
||||||
#[serde(flatten)]
|
|
||||||
data: HashMap<String, String>,
|
|
||||||
}
|
|
||||||
impl Schema for Catchall1 {
|
|
||||||
type Prev = Catchall0;
|
|
||||||
}
|
|
||||||
impl From<Catchall0> for Catchall1 {
|
|
||||||
fn from(old: Catchall0) -> Self {
|
|
||||||
Catchall1 {
|
|
||||||
data: old.0,
|
|
||||||
count: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
struct Catchall2 {
|
|
||||||
count: usize,
|
|
||||||
name: String,
|
|
||||||
#[serde(flatten)]
|
|
||||||
data: HashMap<String, String>,
|
|
||||||
}
|
|
||||||
impl Schema for Catchall2 {
|
|
||||||
type Prev = Catchall1;
|
|
||||||
}
|
|
||||||
impl From<Catchall1> for Catchall2 {
|
|
||||||
fn from(old: Catchall1) -> Self {
|
|
||||||
Catchall2 {
|
|
||||||
data: old.data,
|
|
||||||
count: old.count,
|
|
||||||
name: String::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut data = HashMap::new();
|
|
||||||
data.insert("hello".into(), "world".into());
|
|
||||||
data.insert("catch".into(), "all".into());
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
Catchall2::parse(r#"{"version":1,"count":42,"hello":"world","catch":"all"}"#).unwrap(),
|
|
||||||
Catchall2 {
|
|
||||||
data,
|
|
||||||
count: 42,
|
|
||||||
name: String::new(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn unversioned() {
|
|
||||||
#[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
||||||
struct Data {
|
|
||||||
number: usize,
|
|
||||||
truth: bool,
|
|
||||||
}
|
|
||||||
impl SchemaV0 for Data {
|
|
||||||
const EXPECT_VERSION_NUMBER: bool = false;
|
|
||||||
}
|
|
||||||
assert_eq!(
|
|
||||||
Data::parse(r#"{"number":42,"truth":true}"#).unwrap(),
|
|
||||||
Data {
|
|
||||||
number: 42,
|
|
||||||
truth: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue