|
|
|
use super::*;
|
|
|
|
use cryptohelpers::sha256::Sha256Hash;
|
|
|
|
|
|
|
|
use hard_format::formats::{
|
|
|
|
PEMFormattedString,
|
|
|
|
PEMFormattedStr,
|
|
|
|
MaxLenString,
|
|
|
|
};
|
|
|
|
use tripcode::Tripcode;
|
|
|
|
|
|
|
|
id_type!(PostID; "A unique post ID");
|
|
|
|
|
|
|
|
/// Max length of `email` or `name` feild
|
|
|
|
const ID_MAX_LEN: usize = defaults::POST_ID_MAX_LEN;
|
|
|
|
|
|
|
|
/// String type that limits its bytes to the ID string max limit.
|
|
|
|
pub type IDMaxString = MaxLenString<ID_MAX_LEN>;
|
|
|
|
|
|
|
|
/// A single completed post
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
|
|
|
pub struct Post
|
|
|
|
{
|
|
|
|
id: PostID,
|
|
|
|
|
|
|
|
name: Option<IDMaxString>,
|
|
|
|
tripcode: Option<Tripcode>,
|
|
|
|
email: Option<IDMaxString>,
|
|
|
|
|
|
|
|
/// The client-side encrypted body string
|
|
|
|
body: PEMFormattedString,
|
|
|
|
/// Signature of the body (optional).
|
|
|
|
signature: Option<PEMFormattedString>,
|
|
|
|
|
|
|
|
/// Hash of the body
|
|
|
|
hash: Sha256Hash,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Post
|
|
|
|
{
|
|
|
|
/// The user-set name for this post if there is one.
|
|
|
|
#[inline] pub fn own_name(&self) -> Option<&str>
|
|
|
|
{
|
|
|
|
self.name.as_ref().map(|x| x.as_str())
|
|
|
|
}
|
|
|
|
/// The name for this post.
|
|
|
|
///
|
|
|
|
/// If no name is set, returns the default anon name.
|
|
|
|
pub fn name(&self) -> &str
|
|
|
|
{
|
|
|
|
self.own_name().unwrap_or(defaults::ANON_NAME)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The email set for this post, if there is one.
|
|
|
|
pub fn email(&self) -> Option<&str>
|
|
|
|
{
|
|
|
|
self.email.as_ref().map(|x| x.as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the tripcode of this post, if there is one.
|
|
|
|
pub fn tripcode(&self) -> Option<&Tripcode>
|
|
|
|
{
|
|
|
|
self.tripcode.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The body of this post
|
|
|
|
pub fn body(&self) -> &PEMFormattedStr
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|