post serialise okay

new-idea
Avril 4 years ago
parent 4e0f6945bf
commit 80bd992e72
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
Cargo.lock generated

@ -1877,6 +1877,7 @@ dependencies = [
"once_cell",
"pretty_env_logger",
"serde",
"serde_json",
"sha2",
"smallmap",
"smallvec",

@ -25,6 +25,7 @@ mopa = "0.2.2"
once_cell = "1.5.2"
pretty_env_logger = "0.4.0"
serde = {version = "1.0.118", features=["derive"]}
serde_json = "1.0.61"
sha2 = "0.9.2"
smallmap = "1.3.0"
smallvec = {version = "1.6.0", features= ["union", "serde", "write"]}

@ -13,7 +13,7 @@ pub trait FormatSpec
/// A strongly validated string slice
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[repr(transparent)]
pub struct FormattedStr<F: FormatSpec + ?Sized>(PhantomData<*const F>, str);
@ -22,7 +22,8 @@ unsafe impl<F: FormatSpec + ?Sized> std::marker::Send for FormattedStr<F>{}
unsafe impl<F: FormatSpec + ?Sized> std::marker::Sync for FormattedStr<F>{}
/// A strongly validated string
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
// TODO: How to perform validation on deserialise?
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct FormattedString<F: FormatSpec + ?Sized>(String, PhantomData<F>);
impl<F: FormatSpec + ?Sized> FormattedStr<F>

@ -17,7 +17,7 @@ const ID_MAX_LEN: usize = defaults::POST_ID_MAX_LEN;
pub type IDMaxString = MaxLenString<ID_MAX_LEN>;
/// A single completed post
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Post
{
id: PostID,
@ -68,3 +68,25 @@ impl Post
self.body.as_ref()
}
}
#[cfg(test)]
mod tests
{
#[test]
fn post_serialise()
{
let post = super::Post {
id: super::PostID::id_new(),
name: None,
email: None,
tripcode: Some(super::Tripcode::generate("uhh hello").unwrap()),
body: unsafe { super::PEMFormattedStr::new_unchecked("test").to_owned() }, //temporary
signature: None,
hash: Default::default(),
};
let post_json = serde_json::to_vec(&post).expect("Serialise");
println!("Post json: {:?}", post_json);
let post2: super::Post = serde_json::from_slice(&post_json[..]).expect("Deserialise");
assert_eq!(post, post2);
}
}

@ -4,7 +4,7 @@ use khash::salt::Salt;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Tripcode(String);
lazy_static!{

Loading…
Cancel
Save