arin_protocol/validate.rs
1//! Semantic validation, beyond what the type system and serde already enforce.
2//!
3//! Serde rejects structurally wrong JSON. This catches the rest: messages that parse but
4//! cannot be acted on, such as a `point` carrying neither coordinates nor a query.
5
6use crate::daemon::ErrorCode;
7
8/// A message that parsed but cannot be acted on.
9#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
10pub enum ValidationError {
11 /// Neither form of a two-form message was supplied.
12 #[error("{message} requires one of: {expected}")]
13 MissingTarget {
14 /// The message type at fault.
15 message: &'static str,
16 /// The alternatives that would have been accepted.
17 expected: &'static str,
18 },
19
20 /// Both forms were supplied and they cannot be reconciled.
21 #[error("{message} accepts only one of: {expected}")]
22 AmbiguousTarget {
23 /// The message type at fault.
24 message: &'static str,
25 /// The mutually exclusive alternatives.
26 expected: &'static str,
27 },
28
29 /// A rect was non-finite or had a non-positive extent.
30 #[error("{field} is not a drawable rect: width and height must be finite and positive")]
31 InvalidRect {
32 /// The offending field.
33 field: &'static str,
34 },
35
36 /// A coordinate was NaN or infinite.
37 #[error("{field} contains a non-finite coordinate")]
38 NonFiniteCoordinate {
39 /// The offending field.
40 field: &'static str,
41 },
42
43 /// A field that must carry content was empty or whitespace.
44 #[error("{field} must not be empty")]
45 Empty {
46 /// The offending field.
47 field: &'static str,
48 },
49
50 /// A freehand path had too few points to draw.
51 #[error("path needs at least 2 points, got {got}")]
52 PathTooShort {
53 /// How many points were supplied.
54 got: usize,
55 },
56
57 /// A named position was not one this daemon knows.
58 #[error("{got:?} is not a position: use a name like `top-left`, or `50%,30%`")]
59 UnknownPosition {
60 /// What was sent, so the client can see what was read.
61 got: String,
62 },
63
64 /// A percentage fell outside the display.
65 ///
66 /// Carries the text rather than the parsed number so that [`ValidationError`] stays
67 /// `Eq`, which every other variant already is and which the wire tests rely on.
68 #[error("{got} is outside the display: a percentage must be between 0% and 100%")]
69 PositionOutOfRange {
70 /// The offending component, as it was written.
71 got: String,
72 },
73
74 /// A time to live was zero.
75 ///
76 /// Its own error rather than a silent clamp, because a zero here is a unit mistake
77 /// often enough that drawing a mark and removing it in the same breath is more likely
78 /// to be a bug than an intent.
79 #[error("ttl_ms must be greater than zero, or omitted to draw until cleared")]
80 ZeroTtl,
81}
82
83impl ValidationError {
84 /// The wire error code this maps to.
85 ///
86 /// Every validation failure is a schema failure from the client's point of view.
87 pub const fn code(&self) -> ErrorCode {
88 ErrorCode::BadSchema
89 }
90}
91
92/// Messages that can be checked beyond their parsed shape.
93pub trait Validate {
94 /// Check the message. Called by the daemon before anything is rendered.
95 fn validate(&self) -> Result<(), ValidationError>;
96}