use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use super::{Author, MessageId, ParseIdError, parse_id};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Aside {
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub after: Option<MessageId>,
pub author: Author,
#[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
pub value: serde_json::Value,
#[serde(default = "yes", skip_serializing_if = "is_yes")]
pub saved: bool,
}
fn yes() -> bool {
true
}
fn is_yes(saved: &bool) -> bool {
*saved
}
impl Aside {
pub fn new(kind: impl Into<String>, author: Author, value: serde_json::Value) -> Self {
Self {
kind: kind.into(),
after: None,
author,
value,
saved: true,
}
}
#[must_use]
pub fn after(self, after: Option<MessageId>) -> Self {
Self { after, ..self }
}
#[must_use]
pub fn unsaved(self) -> Self {
Self {
saved: false,
..self
}
}
}
#[derive(
Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
)]
#[serde(transparent)]
pub struct AsideId(pub(super) u64);
impl AsideId {
pub const fn get(self) -> u64 {
self.0
}
}
impl fmt::Display for AsideId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "a{}", self.0)
}
}
impl FromStr for AsideId {
type Err = ParseIdError;
fn from_str(text: &str) -> Result<Self, ParseIdError> {
parse_id(text, 'a').map(Self)
}
}