1use crate::anchor::Anchor;
4use crate::geom::{DisplayId, LogicalPoint, LogicalRect};
5use crate::ids::AnnotationId;
6use crate::position::Position;
7use crate::validate::{Validate, ValidationError};
8use serde::{Deserialize, Serialize};
9
10fn is_false(b: &bool) -> bool {
11 !*b
12}
13
14fn validate_ttl(ttl_ms: Option<u64>) -> Result<(), ValidationError> {
20 match ttl_ms {
21 Some(0) => Err(ValidationError::ZeroTtl),
22 _ => Ok(()),
23 }
24}
25
26#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28#[serde(tag = "type", rename_all = "snake_case")]
29pub enum ClientMessage {
30 SessionStart(SessionStart),
32 Point(Point),
34 Highlight(Highlight),
36 Textbox(Textbox),
38 Draw(Draw),
40 Clear(Clear),
42 Focus(Focus),
44 AwaitWindow(AwaitWindow),
46 SessionEnd,
48}
49
50impl Validate for ClientMessage {
51 fn validate(&self) -> Result<(), ValidationError> {
52 match self {
53 Self::SessionStart(m) => m.validate(),
54 Self::Point(m) => m.validate(),
55 Self::Highlight(m) => m.validate(),
56 Self::Textbox(m) => m.validate(),
57 Self::Draw(m) => m.validate(),
58 Self::Clear(m) => m.validate(),
59 Self::Focus(m) => m.validate(),
60 Self::AwaitWindow(m) => m.validate(),
61 Self::SessionEnd => Ok(()),
62 }
63 }
64}
65
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
78pub struct Focus {
79 pub app: String,
85}
86
87impl Validate for Focus {
88 fn validate(&self) -> Result<(), ValidationError> {
89 if self.app.trim().is_empty() {
90 return Err(ValidationError::Empty { field: "app" });
91 }
92 Ok(())
93 }
94}
95
96#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
107pub struct AwaitWindow {
108 pub app: String,
110 #[serde(default, skip_serializing_if = "Option::is_none")]
116 pub timeout_ms: Option<u64>,
117}
118
119impl Validate for AwaitWindow {
120 fn validate(&self) -> Result<(), ValidationError> {
121 if self.app.trim().is_empty() {
122 return Err(ValidationError::Empty { field: "app" });
123 }
124 if self.timeout_ms == Some(0) {
127 return Err(ValidationError::ZeroTtl);
128 }
129 Ok(())
130 }
131}
132
133#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
135pub struct SessionStart {
136 pub client_name: String,
138}
139
140impl Validate for SessionStart {
141 fn validate(&self) -> Result<(), ValidationError> {
142 if self.client_name.trim().is_empty() {
143 return Err(ValidationError::Empty {
144 field: "client_name",
145 });
146 }
147 Ok(())
148 }
149}
150
151#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
156pub struct Point {
157 #[serde(default, skip_serializing_if = "Option::is_none")]
159 pub x: Option<f64>,
160 #[serde(default, skip_serializing_if = "Option::is_none")]
162 pub y: Option<f64>,
163 #[serde(default, skip_serializing_if = "Option::is_none")]
168 pub at: Option<String>,
169 #[serde(default, skip_serializing_if = "Option::is_none")]
171 pub query: Option<String>,
172 pub display_id: DisplayId,
174 #[serde(default, skip_serializing_if = "Option::is_none")]
176 pub label: Option<String>,
177 #[serde(default, skip_serializing_if = "Option::is_none")]
181 pub ttl_ms: Option<u64>,
182}
183
184#[derive(Debug, Clone, Copy, PartialEq)]
186pub enum PointTarget<'a> {
187 Coords(LogicalPoint),
189 Named(Position),
191 Query(&'a str),
193}
194
195impl Point {
196 pub const fn at(x: f64, y: f64, display_id: DisplayId) -> Self {
198 Self {
199 x: Some(x),
200 y: Some(y),
201 at: None,
202 query: None,
203 display_id,
204 label: None,
205 ttl_ms: None,
206 }
207 }
208
209 pub fn query(query: impl Into<String>, display_id: DisplayId) -> Self {
211 Self {
212 x: None,
213 y: None,
214 at: None,
215 query: Some(query.into()),
216 display_id,
217 label: None,
218 ttl_ms: None,
219 }
220 }
221
222 pub fn named(at: impl Into<String>, display_id: DisplayId) -> Self {
224 Self {
225 x: None,
226 y: None,
227 at: Some(at.into()),
228 query: None,
229 display_id,
230 label: None,
231 ttl_ms: None,
232 }
233 }
234
235 #[must_use]
237 pub fn with_label(mut self, label: impl Into<String>) -> Self {
238 self.label = Some(label.into());
239 self
240 }
241
242 #[must_use]
244 pub fn with_ttl_ms(mut self, ttl_ms: Option<u64>) -> Self {
245 self.ttl_ms = ttl_ms;
246 self
247 }
248
249 pub fn target(&self) -> Result<PointTarget<'_>, ValidationError> {
255 const EXPECTED: &str = "x and y, or at, or query";
256
257 let coords = self.x.is_some() || self.y.is_some();
258 let named = self.at.is_some();
259 let queried = self.query.is_some();
260
261 match (coords, named, queried) {
262 (false, false, false) => {
263 return Err(ValidationError::MissingTarget {
264 message: "point",
265 expected: EXPECTED,
266 });
267 }
268 (true, true, _) | (true, _, true) | (_, true, true) => {
270 return Err(ValidationError::AmbiguousTarget {
271 message: "point",
272 expected: EXPECTED,
273 });
274 }
275 _ => {}
276 }
277
278 if let Some(at) = self.at.as_deref() {
279 return Position::parse(at).map(PointTarget::Named);
280 }
281
282 if let Some(query) = self.query.as_deref() {
283 return if query.trim().is_empty() {
284 Err(ValidationError::Empty { field: "query" })
285 } else {
286 Ok(PointTarget::Query(query))
287 };
288 }
289
290 match (self.x, self.y) {
291 (Some(x), Some(y)) => {
292 let p = LogicalPoint::new(x, y);
293 if !p.is_finite() {
294 return Err(ValidationError::NonFiniteCoordinate { field: "x, y" });
295 }
296 Ok(PointTarget::Coords(p))
297 }
298 _ => Err(ValidationError::AmbiguousTarget {
301 message: "point",
302 expected: EXPECTED,
303 }),
304 }
305 }
306}
307
308impl Validate for Point {
309 fn validate(&self) -> Result<(), ValidationError> {
310 validate_ttl(self.ttl_ms)?;
311 self.target().map(|_| ())
312 }
313}
314
315#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
317pub struct Highlight {
318 #[serde(default, skip_serializing_if = "Option::is_none")]
320 pub rect: Option<LogicalRect>,
321 #[serde(default, skip_serializing_if = "Option::is_none")]
323 pub query: Option<String>,
324 pub display_id: DisplayId,
326 #[serde(default, skip_serializing_if = "Option::is_none")]
328 pub label: Option<String>,
329 #[serde(default, skip_serializing_if = "Option::is_none")]
333 pub ttl_ms: Option<u64>,
334}
335
336#[derive(Debug, Clone, Copy, PartialEq)]
338pub enum HighlightTarget<'a> {
339 Rect(LogicalRect),
341 Query(&'a str),
343}
344
345impl Highlight {
346 pub const fn over(rect: LogicalRect, display_id: DisplayId) -> Self {
348 Self {
349 rect: Some(rect),
350 query: None,
351 display_id,
352 label: None,
353 ttl_ms: None,
354 }
355 }
356
357 pub fn query(query: impl Into<String>, display_id: DisplayId) -> Self {
359 Self {
360 rect: None,
361 query: Some(query.into()),
362 display_id,
363 label: None,
364 ttl_ms: None,
365 }
366 }
367
368 #[must_use]
370 pub fn with_label(mut self, label: impl Into<String>) -> Self {
371 self.label = Some(label.into());
372 self
373 }
374
375 #[must_use]
377 pub fn with_ttl_ms(mut self, ttl_ms: Option<u64>) -> Self {
378 self.ttl_ms = ttl_ms;
379 self
380 }
381
382 pub fn target(&self) -> Result<HighlightTarget<'_>, ValidationError> {
384 const EXPECTED: &str = "rect or query";
385 match (self.rect, self.query.as_deref()) {
386 (Some(rect), None) => {
387 if !rect.is_valid() {
388 return Err(ValidationError::InvalidRect { field: "rect" });
389 }
390 Ok(HighlightTarget::Rect(rect))
391 }
392 (None, Some(q)) if !q.trim().is_empty() => Ok(HighlightTarget::Query(q)),
393 (None, Some(_)) => Err(ValidationError::Empty { field: "query" }),
394 (None, None) => Err(ValidationError::MissingTarget {
395 message: "highlight",
396 expected: EXPECTED,
397 }),
398 (Some(_), Some(_)) => Err(ValidationError::AmbiguousTarget {
399 message: "highlight",
400 expected: EXPECTED,
401 }),
402 }
403 }
404}
405
406impl Validate for Highlight {
407 fn validate(&self) -> Result<(), ValidationError> {
408 validate_ttl(self.ttl_ms)?;
409 self.target().map(|_| ())
410 }
411}
412
413#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
418pub struct Textbox {
419 #[serde(default, skip_serializing_if = "Option::is_none")]
421 pub anchor: Option<Anchor>,
422 #[serde(default, skip_serializing_if = "Option::is_none")]
424 pub rect: Option<LogicalRect>,
425 #[serde(default, skip_serializing_if = "Option::is_none")]
427 pub display_id: Option<DisplayId>,
428 pub text: String,
430 #[serde(default, skip_serializing_if = "Option::is_none")]
434 pub ttl_ms: Option<u64>,
435}
436
437impl Textbox {
438 pub fn new(anchor: Anchor, text: impl Into<String>) -> Self {
440 Self {
441 anchor: Some(anchor),
442 rect: None,
443 display_id: None,
444 text: text.into(),
445 ttl_ms: None,
446 }
447 }
448
449 #[must_use]
451 pub fn with_ttl_ms(mut self, ttl_ms: Option<u64>) -> Self {
452 self.ttl_ms = ttl_ms;
453 self
454 }
455
456 pub fn resolved_anchor(&self) -> Result<Anchor, ValidationError> {
458 const EXPECTED: &str = "anchor, or rect with display_id";
459 match (&self.anchor, self.rect, self.display_id) {
460 (Some(anchor), None, None) => {
461 if !anchor.screen_rect.is_valid() {
462 return Err(ValidationError::InvalidRect {
463 field: "anchor.screen_rect",
464 });
465 }
466 Ok(anchor.clone())
467 }
468 (None, Some(rect), Some(display_id)) => {
469 if !rect.is_valid() {
470 return Err(ValidationError::InvalidRect { field: "rect" });
471 }
472 Ok(Anchor::new(rect, display_id))
473 }
474 (None, None, _) => Err(ValidationError::MissingTarget {
475 message: "textbox",
476 expected: EXPECTED,
477 }),
478 _ => Err(ValidationError::AmbiguousTarget {
479 message: "textbox",
480 expected: EXPECTED,
481 }),
482 }
483 }
484}
485
486impl Validate for Textbox {
487 fn validate(&self) -> Result<(), ValidationError> {
488 validate_ttl(self.ttl_ms)?;
489 if self.text.trim().is_empty() {
490 return Err(ValidationError::Empty { field: "text" });
491 }
492 self.resolved_anchor().map(|_| ())
493 }
494}
495
496#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
498pub struct Draw {
499 pub display_id: DisplayId,
501 pub path: Vec<[f64; 2]>,
503 #[serde(default, skip_serializing_if = "Option::is_none")]
505 pub style: Option<StrokeStyle>,
506 #[serde(default, skip_serializing_if = "Option::is_none")]
510 pub ttl_ms: Option<u64>,
511}
512
513impl Draw {
514 pub fn new(display_id: DisplayId, path: Vec<[f64; 2]>) -> Self {
516 Self {
517 display_id,
518 path,
519 style: None,
520 ttl_ms: None,
521 }
522 }
523
524 #[must_use]
526 pub fn with_ttl_ms(mut self, ttl_ms: Option<u64>) -> Self {
527 self.ttl_ms = ttl_ms;
528 self
529 }
530
531 pub fn points(&self) -> impl Iterator<Item = LogicalPoint> + '_ {
533 self.path.iter().map(|&[x, y]| LogicalPoint::new(x, y))
534 }
535}
536
537impl Validate for Draw {
538 fn validate(&self) -> Result<(), ValidationError> {
539 validate_ttl(self.ttl_ms)?;
540 if self.path.len() < 2 {
541 return Err(ValidationError::PathTooShort {
542 got: self.path.len(),
543 });
544 }
545 if !self.points().all(|p| p.is_finite()) {
546 return Err(ValidationError::NonFiniteCoordinate { field: "path" });
547 }
548 Ok(())
549 }
550}
551
552#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
554pub struct StrokeStyle {
555 #[serde(default, skip_serializing_if = "Option::is_none")]
557 pub width: Option<f64>,
558 #[serde(default, skip_serializing_if = "Option::is_none")]
563 pub color: Option<String>,
564}
565
566#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
568pub struct Clear {
569 #[serde(default, skip_serializing_if = "Option::is_none")]
571 pub annotation_id: Option<AnnotationId>,
572 #[serde(default, skip_serializing_if = "is_false")]
574 pub all: bool,
575}
576
577impl Clear {
578 pub const fn all() -> Self {
580 Self {
581 annotation_id: None,
582 all: true,
583 }
584 }
585
586 pub const fn one(annotation_id: AnnotationId) -> Self {
588 Self {
589 annotation_id: Some(annotation_id),
590 all: false,
591 }
592 }
593}
594
595impl Validate for Clear {
596 fn validate(&self) -> Result<(), ValidationError> {
597 const EXPECTED: &str = "annotation_id or all";
598 match (&self.annotation_id, self.all) {
599 (Some(_), false) | (None, true) => Ok(()),
600 (None, false) => Err(ValidationError::MissingTarget {
601 message: "clear",
602 expected: EXPECTED,
603 }),
604 (Some(_), true) => Err(ValidationError::AmbiguousTarget {
605 message: "clear",
606 expected: EXPECTED,
607 }),
608 }
609 }
610}