use super::extensible::Extensible;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::convert::From;
use uuid::Uuid;
#[derive(Clone, Debug, Deserialize, PartialEq, Default,Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Note {
#[serde(flatten)]
pub extensible: Extensible,
#[serde(rename = "@baseType")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub base_type: Option<String>,
#[serde(rename = "@schemaLocation")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema_location: Option<String>,
#[serde(rename = "@type")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub author: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub date: Option<crate::DateTime>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub text: Option<String>,
}
impl std::fmt::Display for Note {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{}", serde_json::to_string(self).unwrap())
}
}
impl Note {
pub fn new(text: impl Into<String>) -> Note {
let id = Uuid::new_v4().simple().to_string();
let now = Utc::now();
let time = chrono::DateTime::from_timestamp(now.timestamp(), 0).unwrap();
Note {
id: Some(id),
author: None,
date: Some(time.to_string()),
text: Some(text.into()),
..Default::default()
}
}
pub fn author(mut self, author: &str) -> Note {
self.author = Some(author.to_string());
self
}
}
impl From<&str> for Note {
fn from(value: &str) -> Self {
Note::new(value)
}
}
impl std::ops::Deref for Note {
type Target = Extensible;
fn deref(&self) -> &Self::Target {
&self.extensible
}
}
impl std::ops::DerefMut for Note {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.extensible
}
}
#[cfg(test)]
mod test {
use super::Note;
#[test]
fn test_note_create_str() {
let note = Note::from("StringSlice");
assert_eq!(note.text.is_some(), true);
assert_eq!(note.text.unwrap(), "StringSlice");
}
#[test]
fn test_note_create_author() {
let note = Note::from("StringSlice").author("AnAuthor");
assert_eq!(note.author.is_some(), true);
assert_eq!(note.author.unwrap(), "AnAuthor".to_string());
}
}