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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Comments and annotations for Codex documents.

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A comment or annotation on document content.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Comment {
    /// Unique identifier.
    pub id: String,

    /// Type of comment.
    pub comment_type: CommentType,

    /// Reference to the block being commented on.
    pub block_ref: String,

    /// Text range within the block (if applicable).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub range: Option<TextRange>,

    /// Author of the comment.
    pub author: Collaborator,

    /// When the comment was created.
    pub created: DateTime<Utc>,

    /// Comment content.
    pub content: String,

    /// Whether the comment has been resolved.
    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
    pub resolved: bool,

    /// Who resolved the comment.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resolved_by: Option<Collaborator>,

    /// When the comment was resolved.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resolved_at: Option<DateTime<Utc>>,

    /// Replies to this comment.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub replies: Vec<Comment>,

    /// Parent comment ID (for nested replies).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<String>,

    /// Priority level.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub priority: Option<Priority>,

    /// Tags or labels.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
}

impl Comment {
    /// Create a new comment.
    #[must_use]
    pub fn new(
        id: impl Into<String>,
        block_ref: impl Into<String>,
        author: Collaborator,
        content: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            comment_type: CommentType::Comment,
            block_ref: block_ref.into(),
            range: None,
            author,
            created: Utc::now(),
            content: content.into(),
            resolved: false,
            resolved_by: None,
            resolved_at: None,
            replies: Vec::new(),
            parent_id: None,
            priority: None,
            tags: Vec::new(),
        }
    }

    /// Create a new highlight.
    #[must_use]
    pub fn highlight(
        id: impl Into<String>,
        block_ref: impl Into<String>,
        range: TextRange,
        author: Collaborator,
        color: HighlightColor,
    ) -> Self {
        Self {
            id: id.into(),
            comment_type: CommentType::Highlight { color },
            block_ref: block_ref.into(),
            range: Some(range),
            author,
            created: Utc::now(),
            content: String::new(),
            resolved: false,
            resolved_by: None,
            resolved_at: None,
            replies: Vec::new(),
            parent_id: None,
            priority: None,
            tags: Vec::new(),
        }
    }

    /// Create a new suggestion.
    #[must_use]
    pub fn suggestion(
        id: impl Into<String>,
        block_ref: impl Into<String>,
        range: TextRange,
        author: Collaborator,
        original: impl Into<String>,
        suggested: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            comment_type: CommentType::Suggestion {
                original: original.into(),
                suggested: suggested.into(),
                status: SuggestionStatus::Pending,
            },
            block_ref: block_ref.into(),
            range: Some(range),
            author,
            created: Utc::now(),
            content: String::new(),
            resolved: false,
            resolved_by: None,
            resolved_at: None,
            replies: Vec::new(),
            parent_id: None,
            priority: None,
            tags: Vec::new(),
        }
    }

    /// Create a reaction.
    #[must_use]
    pub fn reaction(
        id: impl Into<String>,
        block_ref: impl Into<String>,
        author: Collaborator,
        emoji: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            comment_type: CommentType::Reaction {
                emoji: emoji.into(),
            },
            block_ref: block_ref.into(),
            range: None,
            author,
            created: Utc::now(),
            content: String::new(),
            resolved: false,
            resolved_by: None,
            resolved_at: None,
            replies: Vec::new(),
            parent_id: None,
            priority: None,
            tags: Vec::new(),
        }
    }

    /// Set the text range.
    #[must_use]
    pub fn with_range(mut self, range: TextRange) -> Self {
        self.range = Some(range);
        self
    }

    /// Set priority.
    #[must_use]
    pub fn with_priority(mut self, priority: Priority) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Add a tag.
    #[must_use]
    pub fn with_tag(mut self, tag: impl Into<String>) -> Self {
        self.tags.push(tag.into());
        self
    }

    /// Add a reply.
    pub fn add_reply(&mut self, mut reply: Comment) {
        reply.parent_id = Some(self.id.clone());
        self.replies.push(reply);
    }

    /// Resolve the comment.
    pub fn resolve(&mut self, by: Collaborator) {
        self.resolved = true;
        self.resolved_by = Some(by);
        self.resolved_at = Some(Utc::now());
    }

    /// Unresolve the comment.
    pub fn unresolve(&mut self) {
        self.resolved = false;
        self.resolved_by = None;
        self.resolved_at = None;
    }

    /// Check if this is a suggestion.
    #[must_use]
    pub fn is_suggestion(&self) -> bool {
        matches!(self.comment_type, CommentType::Suggestion { .. })
    }

    /// Get the suggestion status if this is a suggestion.
    #[must_use]
    pub fn suggestion_status(&self) -> Option<SuggestionStatus> {
        match &self.comment_type {
            CommentType::Suggestion { status, .. } => Some(*status),
            _ => None,
        }
    }

    /// Accept a suggestion.
    ///
    /// Returns `true` if the suggestion was accepted, `false` if this is not a suggestion.
    pub fn accept_suggestion(&mut self) -> bool {
        if let CommentType::Suggestion { status, .. } = &mut self.comment_type {
            *status = SuggestionStatus::Accepted;
            true
        } else {
            false
        }
    }

    /// Reject a suggestion.
    ///
    /// Returns `true` if the suggestion was rejected, `false` if this is not a suggestion.
    pub fn reject_suggestion(&mut self) -> bool {
        if let CommentType::Suggestion { status, .. } = &mut self.comment_type {
            *status = SuggestionStatus::Rejected;
            true
        } else {
            false
        }
    }
}

/// Type of comment or annotation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum CommentType {
    /// Standard comment.
    Comment,

    /// Text highlight with color.
    Highlight {
        /// Highlight color.
        color: HighlightColor,
    },

    /// Suggested text change.
    Suggestion {
        /// Original text being replaced.
        original: String,
        /// Suggested replacement text.
        suggested: String,
        /// Current status of the suggestion.
        status: SuggestionStatus,
    },

    /// Emoji reaction.
    Reaction {
        /// Emoji character or shortcode.
        emoji: String,
    },
}

/// Highlight color.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum HighlightColor {
    /// Yellow highlight.
    #[default]
    Yellow,
    /// Green highlight.
    Green,
    /// Blue highlight.
    Blue,
    /// Pink highlight.
    Pink,
    /// Orange highlight.
    Orange,
    /// Purple highlight.
    Purple,
    /// Red highlight.
    Red,
}

/// Status of a suggestion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum SuggestionStatus {
    /// Suggestion is pending review.
    #[default]
    Pending,
    /// Suggestion has been accepted.
    Accepted,
    /// Suggestion has been rejected.
    Rejected,
}

/// Priority level for comments.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::Display)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum Priority {
    /// Low priority.
    Low,
    /// Normal priority.
    Normal,
    /// High priority.
    High,
    /// Critical priority.
    Critical,
}

/// A text range within a block.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TextRange {
    /// Start offset (inclusive).
    pub start: usize,
    /// End offset (exclusive).
    pub end: usize,
}

impl TextRange {
    /// Create a new text range.
    #[must_use]
    pub const fn new(start: usize, end: usize) -> Self {
        Self { start, end }
    }

    /// Get the length of the range.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.end.saturating_sub(self.start)
    }

    /// Check if the range is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.start >= self.end
    }

    /// Check if this range contains a position.
    #[must_use]
    pub const fn contains(&self, pos: usize) -> bool {
        pos >= self.start && pos < self.end
    }

    /// Check if this range overlaps with another.
    #[must_use]
    pub const fn overlaps(&self, other: &Self) -> bool {
        self.start < other.end && other.start < self.end
    }

    /// Check if this range fully contains another.
    #[must_use]
    pub const fn contains_range(&self, other: &Self) -> bool {
        self.start <= other.start && other.end <= self.end
    }
}

/// Collaborator information for comments and changes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Collaborator {
    /// Display name.
    pub name: String,

    /// Email address.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,

    /// Avatar URL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub avatar: Option<String>,

    /// User ID in an external system.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,

    /// Color for real-time cursor coloring (e.g., "#FF5733" or "blue").
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub color: Option<String>,
}

impl Collaborator {
    /// Create a new author.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            email: None,
            avatar: None,
            user_id: None,
            color: None,
        }
    }

    /// Set email.
    #[must_use]
    pub fn with_email(mut self, email: impl Into<String>) -> Self {
        self.email = Some(email.into());
        self
    }

    /// Set avatar URL.
    #[must_use]
    pub fn with_avatar(mut self, avatar: impl Into<String>) -> Self {
        self.avatar = Some(avatar.into());
        self
    }

    /// Set user ID.
    #[must_use]
    pub fn with_user_id(mut self, user_id: impl Into<String>) -> Self {
        self.user_id = Some(user_id.into());
        self
    }

    /// Set color for real-time cursor coloring.
    #[must_use]
    pub fn with_color(mut self, color: impl Into<String>) -> Self {
        self.color = Some(color.into());
        self
    }
}