1use crate::geom::{DisplayInfo, LogicalPoint};
4use crate::ids::{AnnotationId, SessionId};
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9#[serde(tag = "type", rename_all = "snake_case")]
10pub enum DaemonMessage {
11 Ack(Ack),
13 Invalidated(Invalidated),
15 Error(ProtocolError),
17}
18
19#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
25pub struct Ack {
26 #[serde(default, skip_serializing_if = "Option::is_none")]
28 pub session_id: Option<SessionId>,
29 #[serde(default, skip_serializing_if = "Option::is_none")]
31 pub annotation_id: Option<AnnotationId>,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub resolved_coords: Option<LogicalPoint>,
35 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub confidence: Option<f64>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub display: Option<DisplayInfo>,
44}
45
46impl Ack {
47 pub fn session(session_id: SessionId) -> Self {
49 Self {
50 session_id: Some(session_id),
51 ..Self::default()
52 }
53 }
54
55 pub fn annotation(annotation_id: AnnotationId) -> Self {
57 Self {
58 annotation_id: Some(annotation_id),
59 ..Self::default()
60 }
61 }
62
63 #[must_use]
65 pub fn with_display(mut self, display: DisplayInfo) -> Self {
66 self.display = Some(display);
67 self
68 }
69
70 #[must_use]
72 pub fn with_resolution(mut self, coords: LogicalPoint, confidence: f64) -> Self {
73 self.resolved_coords = Some(coords);
74 self.confidence = Some(confidence);
75 self
76 }
77}
78
79#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81pub struct Invalidated {
82 #[serde(default, skip_serializing_if = "Option::is_none")]
84 pub annotation_id: Option<AnnotationId>,
85 pub reason: InvalidationReason,
87}
88
89impl Invalidated {
90 pub fn one(annotation_id: AnnotationId, reason: InvalidationReason) -> Self {
92 Self {
93 annotation_id: Some(annotation_id),
94 reason,
95 }
96 }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
101#[serde(rename_all = "snake_case")]
102pub enum InvalidationReason {
103 Scroll,
108 DisplayChange,
110 SessionEnd,
112 Ttl,
114 Cleared,
119 #[serde(untagged)]
121 Unknown(String),
122}
123
124#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
126pub struct ProtocolError {
127 pub code: ErrorCode,
129 pub msg: String,
131}
132
133impl ProtocolError {
134 pub fn new(code: ErrorCode, msg: impl Into<String>) -> Self {
136 Self {
137 code,
138 msg: msg.into(),
139 }
140 }
141}
142
143#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
145#[serde(rename_all = "snake_case")]
146pub enum ErrorCode {
147 BadSchema,
149 UnknownType,
151 PayloadTooLarge,
153 NoResolver,
155 ResolveFailed,
157 UnknownDisplay,
159 NotOwner,
161 VersionUnsupported,
163 #[serde(untagged)]
165 Unknown(String),
166}
167
168impl ErrorCode {
169 pub fn as_str(&self) -> &str {
171 match self {
172 Self::BadSchema => "bad_schema",
173 Self::UnknownType => "unknown_type",
174 Self::PayloadTooLarge => "payload_too_large",
175 Self::NoResolver => "no_resolver",
176 Self::ResolveFailed => "resolve_failed",
177 Self::UnknownDisplay => "unknown_display",
178 Self::NotOwner => "not_owner",
179 Self::VersionUnsupported => "version_unsupported",
180 Self::Unknown(s) => s,
181 }
182 }
183}
184
185impl std::fmt::Display for ErrorCode {
186 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
187 f.write_str(self.as_str())
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194
195 #[test]
196 fn unknown_codes_survive_a_round_trip() {
197 let json = r#""teleported_into_the_sun""#;
198 let code: ErrorCode = serde_json::from_str(json).unwrap();
199 assert_eq!(code, ErrorCode::Unknown("teleported_into_the_sun".into()));
200 assert_eq!(serde_json::to_string(&code).unwrap(), json);
201 }
202
203 #[test]
204 fn known_codes_take_priority_over_the_fallback() {
205 assert_eq!(
206 serde_json::from_str::<ErrorCode>(r#""not_owner""#).unwrap(),
207 ErrorCode::NotOwner
208 );
209 assert_eq!(
210 serde_json::from_str::<InvalidationReason>(r#""display_change""#).unwrap(),
211 InvalidationReason::DisplayChange
212 );
213 }
214}