Private
Public Access
1
0
Files
Kordophone/kordophone/src/model/message.rs

80 lines
1.6 KiB
Rust

use serde::Deserialize;
use time::OffsetDateTime;
use uuid::Uuid;
use super::Identifiable;
pub type MessageID = <Message as Identifiable>::ID;
#[derive(Debug, Clone, Deserialize)]
pub struct Message {
pub guid: String,
#[serde(rename = "text")]
pub text: String,
#[serde(rename = "sender")]
pub sender: Option<String>,
#[serde(with = "time::serde::iso8601")]
pub date: OffsetDateTime,
}
impl Message {
pub fn builder() -> MessageBuilder {
MessageBuilder::new()
}
}
impl Identifiable for Message {
type ID = String;
fn id(&self) -> &Self::ID {
&self.guid
}
}
#[derive(Default)]
pub struct MessageBuilder {
guid: Option<String>,
text: Option<String>,
sender: Option<String>,
date: Option<OffsetDateTime>,
}
impl MessageBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn guid(mut self, guid: String) -> Self {
self.guid = Some(guid);
self
}
pub fn text(mut self, text: String) -> Self {
self.text = Some(text);
self
}
pub fn sender(mut self, sender: String) -> Self {
self.sender = Some(sender);
self
}
pub fn date(mut self, date: OffsetDateTime) -> Self {
self.date = Some(date);
self
}
pub fn build(self) -> Message {
Message {
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
text: self.text.unwrap_or("".to_string()),
sender: self.sender,
date: self.date.unwrap_or(OffsetDateTime::now_utc()),
}
}
}