ppt-rs 0.2.37

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
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
//! Comments and review annotations for slides
//!
//! Supports adding review comments to slides with author, date, and position.
//! Generates proper OOXML `<p:cmLst>` and `<p:cmAuthorLst>` XML.

use std::collections::HashMap;

/// A comment author
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CommentAuthor {
    pub id: u32,
    pub name: String,
    pub initials: String,
    pub color_index: u32,
}

impl CommentAuthor {
    pub fn new(id: u32, name: &str, initials: &str) -> Self {
        Self {
            id,
            name: name.to_string(),
            initials: initials.to_string(),
            color_index: id,
        }
    }

    pub fn color_index(mut self, idx: u32) -> Self {
        self.color_index = idx;
        self
    }

    /// Generate XML for `<p:cmAuthor>` element
    pub fn to_xml(&self) -> String {
        format!(
            r#"<p:cmAuthor id="{}" name="{}" initials="{}" lastIdx="1" clrIdx="{}"/>"#,
            self.id,
            xml_escape(&self.name),
            xml_escape(&self.initials),
            self.color_index,
        )
    }
}

/// A single comment on a slide
#[derive(Clone, Debug)]
pub struct Comment {
    pub author_id: u32,
    pub text: String,
    pub date: String,
    pub x: u32,
    pub y: u32,
    pub index: u32,
}

impl Comment {
    /// Create a new comment at a position (x, y in EMU)
    pub fn new(author_id: u32, text: &str) -> Self {
        Self {
            author_id,
            text: text.to_string(),
            date: "2025-01-01T00:00:00.000".to_string(),
            x: 0,
            y: 0,
            index: 1,
        }
    }

    /// Set comment position (in EMU)
    pub fn position(mut self, x: u32, y: u32) -> Self {
        self.x = x;
        self.y = y;
        self
    }

    /// Set comment date (ISO 8601 format)
    pub fn date(mut self, date: &str) -> Self {
        self.date = date.to_string();
        self
    }

    /// Set comment index
    pub fn index(mut self, idx: u32) -> Self {
        self.index = idx;
        self
    }

    /// Generate XML for `<p:cm>` element
    pub fn to_xml(&self) -> String {
        format!(
            r#"<p:cm authorId="{}" dt="{}" idx="{}"><p:pos x="{}" y="{}"/><p:text>{}</p:text></p:cm>"#,
            self.author_id,
            xml_escape(&self.date),
            self.index,
            self.x,
            self.y,
            xml_escape(&self.text),
        )
    }
}

/// Manages comment authors across the presentation
#[derive(Clone, Debug, Default)]
pub struct CommentAuthorList {
    authors: Vec<CommentAuthor>,
    name_to_id: HashMap<String, u32>,
    next_id: u32,
}

impl CommentAuthorList {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add or get an author by name. Returns the author ID.
    pub fn get_or_add(&mut self, name: &str, initials: &str) -> u32 {
        if let Some(&id) = self.name_to_id.get(name) {
            return id;
        }
        let id = self.next_id;
        self.next_id += 1;
        let author = CommentAuthor::new(id, name, initials);
        self.authors.push(author);
        self.name_to_id.insert(name.to_string(), id);
        id
    }

    /// Get author by ID
    pub fn get_by_id(&self, id: u32) -> Option<&CommentAuthor> {
        self.authors.iter().find(|a| a.id == id)
    }

    /// Get all authors
    pub fn authors(&self) -> &[CommentAuthor] {
        &self.authors
    }

    /// Number of authors
    pub fn len(&self) -> usize {
        self.authors.len()
    }

    /// Whether the list is empty
    pub fn is_empty(&self) -> bool {
        self.authors.is_empty()
    }

    /// Register an author parsed from an imported deck, preserving its id.
    pub fn import_author(&mut self, author: CommentAuthor) {
        if !self.authors.iter().any(|a| a.id == author.id) {
            self.authors.push(author.clone());
            self.authors.sort_by_key(|a| a.id);
        }
        self.name_to_id
            .entry(author.name.clone())
            .or_insert(author.id);
        self.next_id = self.next_id.max(author.id.saturating_add(1));
    }

    /// Generate `commentAuthors.xml` content
    pub fn to_xml(&self) -> String {
        let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#);
        xml.push_str(
            r#"<p:cmAuthorLst xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">"#,
        );
        for author in &self.authors {
            xml.push_str(&author.to_xml());
        }
        xml.push_str("</p:cmAuthorLst>");
        xml
    }
}

/// Comments for a single slide
#[derive(Clone, Debug, Default)]
pub struct SlideComments {
    comments: Vec<Comment>,
}

impl SlideComments {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a comment
    pub fn add(&mut self, comment: Comment) {
        self.comments.push(comment);
    }

    /// Add a comment with auto-incrementing index
    pub fn add_comment(&mut self, author_id: u32, text: &str, x: u32, y: u32) {
        let idx = self.comments.len() as u32 + 1;
        self.comments
            .push(Comment::new(author_id, text).position(x, y).index(idx));
    }

    /// Get all comments
    pub fn comments(&self) -> &[Comment] {
        &self.comments
    }

    /// Number of comments
    pub fn len(&self) -> usize {
        self.comments.len()
    }

    /// Whether there are no comments
    pub fn is_empty(&self) -> bool {
        self.comments.is_empty()
    }

    /// Generate `commentN.xml` content for a slide
    pub fn to_xml(&self) -> String {
        let mut xml = String::from(r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>"#);
        xml.push_str(
            r#"<p:cmLst xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">"#,
        );
        for comment in &self.comments {
            xml.push_str(&comment.to_xml());
        }
        xml.push_str("</p:cmLst>");
        xml
    }
}

fn xml_escape(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
        .replace('"', "&quot;")
        .replace('\'', "&apos;")
}

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

    #[test]
    fn test_comment_author_new() {
        let author = CommentAuthor::new(0, "John Doe", "JD");
        assert_eq!(author.id, 0);
        assert_eq!(author.name, "John Doe");
        assert_eq!(author.initials, "JD");
        assert_eq!(author.color_index, 0);
    }

    #[test]
    fn test_comment_author_color_index() {
        let author = CommentAuthor::new(0, "Jane", "J").color_index(5);
        assert_eq!(author.color_index, 5);
    }

    #[test]
    fn test_comment_author_xml() {
        let author = CommentAuthor::new(0, "John Doe", "JD");
        let xml = author.to_xml();
        assert!(xml.contains(r#"id="0""#));
        assert!(xml.contains(r#"name="John Doe""#));
        assert!(xml.contains(r#"initials="JD""#));
    }

    #[test]
    fn test_comment_new() {
        let comment = Comment::new(0, "Review this slide");
        assert_eq!(comment.author_id, 0);
        assert_eq!(comment.text, "Review this slide");
        assert_eq!(comment.x, 0);
        assert_eq!(comment.y, 0);
    }

    #[test]
    fn test_comment_position() {
        let comment = Comment::new(0, "Note").position(100, 200);
        assert_eq!(comment.x, 100);
        assert_eq!(comment.y, 200);
    }

    #[test]
    fn test_comment_date() {
        let comment = Comment::new(0, "Note").date("2025-06-15T10:30:00.000");
        assert_eq!(comment.date, "2025-06-15T10:30:00.000");
    }

    #[test]
    fn test_comment_xml() {
        let comment = Comment::new(0, "Fix this").position(100, 200).index(1);
        let xml = comment.to_xml();
        assert!(xml.contains(r#"authorId="0""#));
        assert!(xml.contains(r#"idx="1""#));
        assert!(xml.contains(r#"x="100""#));
        assert!(xml.contains(r#"y="200""#));
        assert!(xml.contains("Fix this"));
    }

    #[test]
    fn test_comment_xml_escaping() {
        let comment = Comment::new(0, "Use <b> & \"quotes\"");
        let xml = comment.to_xml();
        assert!(xml.contains("&lt;b&gt;"));
        assert!(xml.contains("&amp;"));
        assert!(xml.contains("&quot;quotes&quot;"));
    }

    #[test]
    fn test_comment_author_list_new() {
        let list = CommentAuthorList::new();
        assert!(list.is_empty());
        assert_eq!(list.len(), 0);
    }

    #[test]
    fn test_comment_author_list_add() {
        let mut list = CommentAuthorList::new();
        let id1 = list.get_or_add("Alice", "A");
        let id2 = list.get_or_add("Bob", "B");
        assert_eq!(id1, 0);
        assert_eq!(id2, 1);
        assert_eq!(list.len(), 2);
    }

    #[test]
    fn test_comment_author_list_import_author_preserves_id() {
        let mut list = CommentAuthorList::new();
        list.import_author(CommentAuthor::new(5, "Imported", "IM"));
        assert_eq!(list.get_by_id(5).unwrap().name, "Imported");
        assert_eq!(list.len(), 1);
    }

    #[test]
    fn test_comment_author_list_dedup() {
        let mut list = CommentAuthorList::new();
        let id1 = list.get_or_add("Alice", "A");
        let id2 = list.get_or_add("Alice", "A");
        assert_eq!(id1, id2);
        assert_eq!(list.len(), 1);
    }

    #[test]
    fn test_comment_author_list_get_by_id() {
        let mut list = CommentAuthorList::new();
        list.get_or_add("Alice", "A");
        let author = list.get_by_id(0);
        assert!(author.is_some());
        assert_eq!(author.unwrap().name, "Alice");
        assert!(list.get_by_id(99).is_none());
    }

    #[test]
    fn test_comment_author_list_xml() {
        let mut list = CommentAuthorList::new();
        list.get_or_add("Alice", "A");
        let xml = list.to_xml();
        assert!(xml.contains("<p:cmAuthorLst"));
        assert!(xml.contains("Alice"));
        assert!(xml.contains("</p:cmAuthorLst>"));
    }

    #[test]
    fn test_slide_comments_new() {
        let comments = SlideComments::new();
        assert!(comments.is_empty());
        assert_eq!(comments.len(), 0);
    }

    #[test]
    fn test_slide_comments_add() {
        let mut comments = SlideComments::new();
        comments.add(Comment::new(0, "First comment").position(10, 20));
        comments.add(Comment::new(1, "Second comment").position(30, 40));
        assert_eq!(comments.len(), 2);
    }

    #[test]
    fn test_slide_comments_add_comment() {
        let mut comments = SlideComments::new();
        comments.add_comment(0, "Auto-indexed", 100, 200);
        comments.add_comment(0, "Second", 300, 400);
        assert_eq!(comments.comments()[0].index, 1);
        assert_eq!(comments.comments()[1].index, 2);
    }

    #[test]
    fn test_slide_comments_xml() {
        let mut comments = SlideComments::new();
        comments.add_comment(0, "Review needed", 100, 200);
        let xml = comments.to_xml();
        assert!(xml.contains("<p:cmLst"));
        assert!(xml.contains("Review needed"));
        assert!(xml.contains("</p:cmLst>"));
    }

    #[test]
    fn test_slide_comments_xml_empty() {
        let comments = SlideComments::new();
        let xml = comments.to_xml();
        assert!(xml.contains("<p:cmLst"));
        assert!(xml.contains("</p:cmLst>"));
    }

    #[test]
    fn test_comment_author_list_xml_multiple() {
        let mut list = CommentAuthorList::new();
        list.get_or_add("Alice", "A");
        list.get_or_add("Bob", "B");
        let xml = list.to_xml();
        assert!(xml.contains("Alice"));
        assert!(xml.contains("Bob"));
        assert!(xml.matches("<p:cmAuthor ").count() == 2);
    }
}