rig_core/conversation/
aside.rs1use std::fmt;
4use std::str::FromStr;
5
6use serde::{Deserialize, Serialize};
7
8use super::{Author, MessageId, ParseIdError, parse_id};
9
10#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
14pub struct Aside {
15 pub kind: String,
19 #[serde(default, skip_serializing_if = "Option::is_none")]
23 pub after: Option<MessageId>,
24 pub author: Author,
26 #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
28 pub value: serde_json::Value,
29 #[serde(default = "yes", skip_serializing_if = "is_yes")]
32 pub saved: bool,
33}
34
35fn yes() -> bool {
36 true
37}
38
39fn is_yes(saved: &bool) -> bool {
40 *saved
41}
42
43impl Aside {
44 pub fn new(kind: impl Into<String>, author: Author, value: serde_json::Value) -> Self {
47 Self {
48 kind: kind.into(),
49 after: None,
50 author,
51 value,
52 saved: true,
53 }
54 }
55
56 #[must_use]
58 pub fn after(self, after: Option<MessageId>) -> Self {
59 Self { after, ..self }
60 }
61
62 #[must_use]
64 pub fn unsaved(self) -> Self {
65 Self {
66 saved: false,
67 ..self
68 }
69 }
70}
71
72#[derive(
75 Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
76)]
77#[serde(transparent)]
78pub struct AsideId(pub(super) u64);
79
80impl AsideId {
81 pub const fn get(self) -> u64 {
83 self.0
84 }
85}
86
87impl fmt::Display for AsideId {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 write!(f, "a{}", self.0)
91 }
92}
93
94impl FromStr for AsideId {
96 type Err = ParseIdError;
97
98 fn from_str(text: &str) -> Result<Self, ParseIdError> {
99 parse_id(text, 'a').map(Self)
100 }
101}