Skip to main content

arin_protocol/
anchor.rs

1//! Anchor descriptors.
2
3use crate::geom::{DisplayId, LogicalRect};
4use serde::{Deserialize, Serialize};
5
6/// Where an annotation is pinned.
7///
8/// Every annotation carries one. In 0.1 only `screen_rect` and `display_id` are read.
9/// `content_hash` is reserved and always null: scroll tracking populates it later so
10/// annotations can be matched to content after it moves. It is serialized now, even as
11/// null, so that filling it in is an additive change rather than a new field.
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13pub struct Anchor {
14    /// The region in logical points, on `display_id`.
15    pub screen_rect: LogicalRect,
16    /// The display the rect belongs to.
17    pub display_id: DisplayId,
18    /// Reserved. Always `None` in 0.1.
19    #[serde(default)]
20    pub content_hash: Option<String>,
21}
22
23impl Anchor {
24    /// Anchor to a region of a display.
25    pub const fn new(screen_rect: LogicalRect, display_id: DisplayId) -> Self {
26        Self {
27            screen_rect,
28            display_id,
29            content_hash: None,
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn reserved_field_stays_on_the_wire() {
40        let anchor = Anchor::new(LogicalRect::new(100.0, 200.0, 340.0, 90.0), DisplayId(1));
41        let json = serde_json::to_value(&anchor).unwrap();
42        assert!(
43            json.get("content_hash")
44                .is_some_and(serde_json::Value::is_null),
45            "content_hash must serialize as null, not vanish"
46        );
47    }
48
49    #[test]
50    fn absent_reserved_field_deserializes() {
51        let json = r#"{"screen_rect":[100,200,340,90],"display_id":1}"#;
52        let anchor: Anchor = serde_json::from_str(json).unwrap();
53        assert_eq!(anchor.content_hash, None);
54    }
55}