cdx-core 0.7.1

Core library for reading, writing, and validating Codex Document Format (.cdx) files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Core annotations for minimal annotation support without extensions.
//!
//! This module provides lightweight annotation support for frozen/published documents
//! when the collaboration extension is not available. Core annotations are stored in
//! `security/annotations.json` and provide basic comment functionality without the
//! full feature set of the collaboration extension.
//!
//! # When to Use
//!
//! - For simple read-only annotations on frozen documents
//! - When the collaboration extension is not available or overkill
//! - For implementations that don't need threaded discussions or suggestions
//!
//! # Example
//!
//! ```rust
//! use cdx_core::security::{Annotation, AnnotationType, AnnotationsFile};
//! use cdx_core::anchor::ContentAnchor;
//!
//! let annotation = Annotation::new(
//!     "anno-1",
//!     AnnotationType::Comment,
//!     ContentAnchor::block("para-1"),
//!     "Reviewer",
//!     "This section needs clarification.",
//! );
//!
//! let annotations = AnnotationsFile::new(vec![annotation]);
//! ```

use serde::{Deserialize, Serialize};

use crate::anchor::ContentAnchor;

/// Core annotations file for `security/annotations.json`.
///
/// Provides minimal annotation support for implementations that don't use
/// the collaboration extension.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AnnotationsFile {
    /// Format version (e.g., "0.1").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,

    /// Array of annotations.
    pub annotations: Vec<Annotation>,
}

impl AnnotationsFile {
    /// Create a new annotations file.
    #[must_use]
    pub fn new(annotations: Vec<Annotation>) -> Self {
        Self {
            version: Some(crate::SPEC_VERSION.to_string()),
            annotations,
        }
    }

    /// Create an empty annotations file.
    #[must_use]
    pub fn empty() -> Self {
        Self::new(Vec::new())
    }

    /// Add an annotation.
    pub fn add(&mut self, annotation: Annotation) {
        self.annotations.push(annotation);
    }

    /// Get an annotation by ID.
    #[must_use]
    pub fn get(&self, id: &str) -> Option<&Annotation> {
        self.annotations.iter().find(|a| a.id == id)
    }

    /// Get all annotations for a specific block.
    #[must_use]
    pub fn for_block(&self, block_id: &str) -> Vec<&Annotation> {
        self.annotations
            .iter()
            .filter(|a| a.anchor.block_id == block_id)
            .collect()
    }

    /// Check if there are any annotations.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.annotations.is_empty()
    }

    /// Get the number of annotations.
    #[must_use]
    pub fn len(&self) -> usize {
        self.annotations.len()
    }
}

impl Default for AnnotationsFile {
    fn default() -> Self {
        Self::empty()
    }
}

/// A core annotation.
///
/// Core annotations are simpler than collaboration comments - they use a plain
/// string author field instead of the full `Collaborator` object, and don't
/// support threading, suggestions, or reactions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Annotation {
    /// Unique annotation identifier.
    pub id: String,

    /// Annotation type.
    #[serde(rename = "type")]
    pub annotation_type: AnnotationType,

    /// Anchor to document content.
    pub anchor: ContentAnchor,

    /// Author name or identifier.
    pub author: String,

    /// ISO 8601 creation timestamp.
    pub created: String,

    /// Annotation content (text).
    pub content: String,
}

impl Annotation {
    /// Create a new annotation.
    #[must_use]
    pub fn new(
        id: impl Into<String>,
        annotation_type: AnnotationType,
        anchor: ContentAnchor,
        author: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            annotation_type,
            anchor,
            author: author.into(),
            created: chrono_now(),
            content: content.into(),
        }
    }

    /// Create a comment annotation.
    #[must_use]
    pub fn comment(
        id: impl Into<String>,
        anchor: ContentAnchor,
        author: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(id, AnnotationType::Comment, anchor, author, content)
    }

    /// Create a highlight annotation.
    #[must_use]
    pub fn highlight(
        id: impl Into<String>,
        anchor: ContentAnchor,
        author: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(id, AnnotationType::Highlight, anchor, author, content)
    }

    /// Create a note annotation.
    #[must_use]
    pub fn note(
        id: impl Into<String>,
        anchor: ContentAnchor,
        author: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(id, AnnotationType::Note, anchor, author, content)
    }

    /// Create a reaction annotation.
    #[must_use]
    pub fn reaction(
        id: impl Into<String>,
        anchor: ContentAnchor,
        author: impl Into<String>,
        emoji: impl Into<String>,
    ) -> Self {
        Self::new(id, AnnotationType::Reaction, anchor, author, emoji)
    }

    /// Set the creation timestamp.
    #[must_use]
    pub fn with_created(mut self, created: impl Into<String>) -> Self {
        self.created = created.into();
        self
    }
}

/// Annotation type for core annotations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum AnnotationType {
    /// General comment on content.
    Comment,
    /// Highlighted text with optional note.
    Highlight,
    /// A note attached to content.
    Note,
    /// Emoji reaction.
    Reaction,
}

impl AnnotationType {
    /// Get the type as a string.
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::Comment => "comment",
            Self::Highlight => "highlight",
            Self::Note => "note",
            Self::Reaction => "reaction",
        }
    }
}

/// Get current timestamp in ISO 8601 / RFC 3339 format.
fn chrono_now() -> String {
    chrono::Utc::now().to_rfc3339()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_annotations_file_new() {
        let file = AnnotationsFile::new(vec![]);
        assert!(file.is_empty());
        assert_eq!(file.len(), 0);
        assert_eq!(file.version, Some("0.1".to_string()));
    }

    #[test]
    fn test_annotations_file_add() {
        let mut file = AnnotationsFile::empty();
        let anno = Annotation::comment("a1", ContentAnchor::block("block-1"), "Author", "Comment");
        file.add(anno);

        assert_eq!(file.len(), 1);
        assert!(!file.is_empty());
    }

    #[test]
    fn test_annotations_file_get() {
        let anno1 = Annotation::comment("a1", ContentAnchor::block("b1"), "Auth", "Text 1");
        let anno2 = Annotation::note("a2", ContentAnchor::block("b2"), "Auth", "Text 2");
        let file = AnnotationsFile::new(vec![anno1, anno2]);

        assert!(file.get("a1").is_some());
        assert!(file.get("a2").is_some());
        assert!(file.get("nonexistent").is_none());
    }

    #[test]
    fn test_annotations_file_for_block() {
        let anno1 = Annotation::comment("a1", ContentAnchor::block("b1"), "Auth", "Text 1");
        let anno2 = Annotation::note("a2", ContentAnchor::block("b1"), "Auth", "Text 2");
        let anno3 = Annotation::highlight("a3", ContentAnchor::block("b2"), "Auth", "Text 3");
        let file = AnnotationsFile::new(vec![anno1, anno2, anno3]);

        let b1_annos = file.for_block("b1");
        assert_eq!(b1_annos.len(), 2);

        let b2_annos = file.for_block("b2");
        assert_eq!(b2_annos.len(), 1);
    }

    #[test]
    fn test_annotation_new() {
        let anchor = ContentAnchor::range("block-1", 10, 25);
        let anno = Annotation::new(
            "anno-1",
            AnnotationType::Comment,
            anchor,
            "Alice",
            "A comment",
        );

        assert_eq!(anno.id, "anno-1");
        assert_eq!(anno.annotation_type, AnnotationType::Comment);
        assert_eq!(anno.author, "Alice");
        assert_eq!(anno.content, "A comment");
    }

    #[test]
    fn test_annotation_convenience_constructors() {
        let anchor = ContentAnchor::block("b1");

        let comment = Annotation::comment("c1", anchor.clone(), "Auth", "Comment text");
        assert_eq!(comment.annotation_type, AnnotationType::Comment);

        let highlight = Annotation::highlight("h1", anchor.clone(), "Auth", "Highlight note");
        assert_eq!(highlight.annotation_type, AnnotationType::Highlight);

        let note = Annotation::note("n1", anchor.clone(), "Auth", "Note text");
        assert_eq!(note.annotation_type, AnnotationType::Note);

        let reaction = Annotation::reaction("r1", anchor, "Auth", "thumbsup");
        assert_eq!(reaction.annotation_type, AnnotationType::Reaction);
        assert_eq!(reaction.content, "thumbsup");
    }

    #[test]
    fn test_annotation_serialization() {
        let anchor = ContentAnchor::range("para-1", 0, 10);
        let anno = Annotation::comment("a1", anchor, "Reviewer", "Needs work")
            .with_created("2025-01-15T10:00:00Z");

        let json = serde_json::to_string(&anno).unwrap();
        assert!(json.contains("\"id\":\"a1\""));
        assert!(json.contains("\"type\":\"comment\""));
        assert!(json.contains("\"author\":\"Reviewer\""));
        assert!(json.contains("\"content\":\"Needs work\""));
        assert!(json.contains("\"blockId\":\"para-1\""));
    }

    #[test]
    fn test_annotation_deserialization() {
        let json = r#"{
            "id": "anno-1",
            "type": "highlight",
            "anchor": {"blockId": "block-1", "start": 5, "end": 15},
            "author": "Bob",
            "created": "2025-01-15T12:00:00Z",
            "content": "Important section"
        }"#;

        let anno: Annotation = serde_json::from_str(json).unwrap();
        assert_eq!(anno.id, "anno-1");
        assert_eq!(anno.annotation_type, AnnotationType::Highlight);
        assert_eq!(anno.anchor.block_id, "block-1");
        assert_eq!(anno.anchor.start, Some(5));
        assert_eq!(anno.anchor.end, Some(15));
        assert_eq!(anno.author, "Bob");
        assert_eq!(anno.content, "Important section");
    }

    #[test]
    fn test_annotations_file_serialization() {
        let anno = Annotation::comment("a1", ContentAnchor::block("b1"), "Auth", "Comment")
            .with_created("2025-01-15T10:00:00Z");
        let file = AnnotationsFile::new(vec![anno]);

        let json = serde_json::to_string_pretty(&file).unwrap();
        assert!(json.contains("\"version\": \"0.1\""));
        assert!(json.contains("\"annotations\""));
        assert!(json.contains("\"type\": \"comment\""));
    }

    #[test]
    fn test_annotations_file_deserialization() {
        let json = r#"{
            "version": "0.1",
            "annotations": [
                {
                    "id": "a1",
                    "type": "note",
                    "anchor": {"blockId": "intro"},
                    "author": "Editor",
                    "created": "2025-01-15T10:00:00Z",
                    "content": "Consider rephrasing."
                }
            ]
        }"#;

        let file: AnnotationsFile = serde_json::from_str(json).unwrap();
        assert_eq!(file.version, Some("0.1".to_string()));
        assert_eq!(file.len(), 1);

        let anno = &file.annotations[0];
        assert_eq!(anno.id, "a1");
        assert_eq!(anno.annotation_type, AnnotationType::Note);
    }

    #[test]
    fn test_annotation_type_display() {
        assert_eq!(AnnotationType::Comment.to_string(), "comment");
        assert_eq!(AnnotationType::Highlight.to_string(), "highlight");
        assert_eq!(AnnotationType::Note.to_string(), "note");
        assert_eq!(AnnotationType::Reaction.to_string(), "reaction");
    }

    #[test]
    fn test_annotation_type_as_str() {
        assert_eq!(AnnotationType::Comment.as_str(), "comment");
        assert_eq!(AnnotationType::Highlight.as_str(), "highlight");
        assert_eq!(AnnotationType::Note.as_str(), "note");
        assert_eq!(AnnotationType::Reaction.as_str(), "reaction");
    }
}