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 pub fn read<T: serde::de::DeserializeOwned>(&self, kind: &str) -> Option<T> {
74 (self.kind == kind)
75 .then(|| crate::json_utils::from_value(&self.value).ok())
76 .flatten()
77 }
78}
79
80#[derive(
83 Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
84)]
85#[serde(transparent)]
86pub struct AsideId(pub(super) u64);
87
88impl AsideId {
89 pub const fn get(self) -> u64 {
91 self.0
92 }
93}
94
95impl fmt::Display for AsideId {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 write!(f, "a{}", self.0)
99 }
100}
101
102impl FromStr for AsideId {
104 type Err = ParseIdError;
105
106 fn from_str(text: &str) -> Result<Self, ParseIdError> {
107 parse_id(text, 'a').map(Self)
108 }
109}