Skip to main content

pptx/enums/
misc.rs

1//! Miscellaneous enumerations.
2
3/// Identifies a particular OLE program identifier for embedded objects.
4#[non_exhaustive]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum ProgId {
7    /// Microsoft Excel worksheet.
8    ExcelWorksheet,
9    /// Microsoft Excel chart.
10    ExcelChart,
11    /// Microsoft Word document.
12    WordDocument,
13    /// Microsoft `PowerPoint` presentation.
14    PowerPointPresentation,
15    /// Microsoft `PowerPoint` slide.
16    PowerPointSlide,
17    /// Microsoft Visio drawing.
18    VisioDrawing,
19    /// Adobe Acrobat PDF.
20    AcrobatDocument,
21    /// Package (generic embedded file).
22    Package,
23}
24
25impl ProgId {
26    /// Return the OLE `ProgId` string for this variant.
27    #[must_use]
28    pub const fn to_prog_id_str(self) -> &'static str {
29        match self {
30            Self::ExcelWorksheet => "Excel.Sheet.12",
31            Self::ExcelChart => "Excel.Chart.8",
32            Self::WordDocument => "Word.Document.12",
33            Self::PowerPointPresentation => "PowerPoint.Show.12",
34            Self::PowerPointSlide => "PowerPoint.Slide.12",
35            Self::VisioDrawing => "Visio.Drawing.15",
36            Self::AcrobatDocument => "AcroExch.Document",
37            Self::Package => "Package",
38        }
39    }
40
41    /// Parse an OLE `ProgId` string.
42    #[must_use]
43    pub fn from_prog_id_str(s: &str) -> Option<Self> {
44        match s {
45            "Excel.Sheet.12" => Some(Self::ExcelWorksheet),
46            "Excel.Chart.8" => Some(Self::ExcelChart),
47            "Word.Document.12" => Some(Self::WordDocument),
48            "PowerPoint.Show.12" => Some(Self::PowerPointPresentation),
49            "PowerPoint.Slide.12" => Some(Self::PowerPointSlide),
50            "Visio.Drawing.15" => Some(Self::VisioDrawing),
51            "AcroExch.Document" => Some(Self::AcrobatDocument),
52            "Package" => Some(Self::Package),
53            _ => None,
54        }
55    }
56}
57
58// ---------------------------------------------------------------------------
59// PP_MEDIA_TYPE
60// ---------------------------------------------------------------------------
61
62/// Specifies the type of media in a shape.
63#[non_exhaustive]
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum PpMediaType {
66    /// A movie (video) clip.
67    Movie,
68    /// A sound (audio) clip.
69    Sound,
70    /// Another media type not covered by Movie or Sound.
71    Other,
72}
73
74impl PpMediaType {
75    /// Return the XML attribute value for this media type.
76    #[must_use]
77    pub const fn to_xml_str(self) -> &'static str {
78        match self {
79            Self::Movie => "movie",
80            Self::Sound => "sound",
81            Self::Other => "other",
82        }
83    }
84
85    /// Parse an XML media type attribute value.
86    #[must_use]
87    pub fn from_xml_str(s: &str) -> Option<Self> {
88        match s {
89            "movie" => Some(Self::Movie),
90            "sound" => Some(Self::Sound),
91            "other" => Some(Self::Other),
92            _ => None,
93        }
94    }
95}
96
97// ---------------------------------------------------------------------------
98// ExcelNumFormat - common Excel number format strings
99// ---------------------------------------------------------------------------
100
101/// Common Excel number format strings used in chart data labels and table cells.
102///
103/// These are the standard format codes recognized by Excel and `PowerPoint`.
104pub struct ExcelNumFormat;
105
106impl ExcelNumFormat {
107    /// General format (no specific formatting).
108    pub const GENERAL: &str = "General";
109    /// Number with two decimal places: `0.00`
110    pub const NUMBER: &str = "0.00";
111    /// Number with no decimal places: `0`
112    pub const NUMBER_NO_DECIMAL: &str = "0";
113    /// Currency with two decimal places: `$#,##0.00`
114    pub const CURRENCY: &str = "$#,##0.00";
115    /// Percentage with no decimal places: `0%`
116    pub const PERCENTAGE: &str = "0%";
117    /// Percentage with two decimal places: `0.00%`
118    pub const PERCENTAGE_DECIMAL: &str = "0.00%";
119    /// Date format: `m/d/yyyy`
120    pub const DATE: &str = "m/d/yyyy";
121    /// Time format: `h:mm:ss`
122    pub const TIME: &str = "h:mm:ss";
123}
124
125// ---------------------------------------------------------------------------
126// PrintColorMode
127// ---------------------------------------------------------------------------
128
129/// Specifies how a presentation should be printed with respect to color.
130#[non_exhaustive]
131#[derive(Debug, Clone, Copy, PartialEq, Eq)]
132pub enum PrintColorMode {
133    /// Print in full color.
134    Color,
135    /// Print in grayscale.
136    Grayscale,
137    /// Print in pure black and white.
138    PureBlackWhite,
139}
140
141impl PrintColorMode {
142    /// Return the XML attribute value for this color mode.
143    #[must_use]
144    pub const fn to_xml_str(self) -> &'static str {
145        match self {
146            Self::Color => "clr",
147            Self::Grayscale => "gray",
148            Self::PureBlackWhite => "pureBlkWht",
149        }
150    }
151
152    /// Parse an XML color mode attribute value.
153    #[must_use]
154    pub fn from_xml_str(s: &str) -> Option<Self> {
155        match s {
156            "clr" => Some(Self::Color),
157            "gray" => Some(Self::Grayscale),
158            "pureBlkWht" => Some(Self::PureBlackWhite),
159            _ => None,
160        }
161    }
162}
163
164// ---------------------------------------------------------------------------
165// PrintWhat
166// ---------------------------------------------------------------------------
167
168/// Specifies what content should be printed.
169#[non_exhaustive]
170#[derive(Debug, Clone, Copy, PartialEq, Eq)]
171pub enum PrintWhat {
172    /// Print slides.
173    Slides,
174    /// Print handouts.
175    Handouts,
176    /// Print notes pages.
177    Notes,
178    /// Print outline view.
179    Outline,
180}
181
182impl PrintWhat {
183    /// Return the XML attribute value for this print target.
184    #[must_use]
185    pub const fn to_xml_str(self) -> &'static str {
186        match self {
187            Self::Slides => "slides",
188            Self::Handouts => "handouts",
189            Self::Notes => "notes",
190            Self::Outline => "outline",
191        }
192    }
193
194    /// Parse an XML print target attribute value.
195    #[must_use]
196    pub fn from_xml_str(s: &str) -> Option<Self> {
197        match s {
198            "slides" => Some(Self::Slides),
199            "handouts" => Some(Self::Handouts),
200            "notes" => Some(Self::Notes),
201            "outline" => Some(Self::Outline),
202            _ => None,
203        }
204    }
205}
206
207// ---------------------------------------------------------------------------
208// HandoutLayout
209// ---------------------------------------------------------------------------
210
211/// Specifies the number of slides per page for handout printing.
212#[non_exhaustive]
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
214pub enum HandoutLayout {
215    /// One slide per page.
216    One,
217    /// Two slides per page.
218    Two,
219    /// Three slides per page.
220    Three,
221    /// Four slides per page.
222    Four,
223    /// Six slides per page.
224    Six,
225    /// Nine slides per page.
226    Nine,
227}
228
229impl HandoutLayout {
230    /// Return the XML attribute value for this handout layout.
231    #[must_use]
232    pub const fn to_xml_str(self) -> &'static str {
233        match self {
234            Self::One => "handouts1",
235            Self::Two => "handouts2",
236            Self::Three => "handouts3",
237            Self::Four => "handouts4",
238            Self::Six => "handouts6",
239            Self::Nine => "handouts9",
240        }
241    }
242
243    /// Parse an XML handout layout attribute value.
244    #[must_use]
245    pub fn from_xml_str(s: &str) -> Option<Self> {
246        match s {
247            "handouts1" => Some(Self::One),
248            "handouts2" => Some(Self::Two),
249            "handouts3" => Some(Self::Three),
250            "handouts4" => Some(Self::Four),
251            "handouts6" => Some(Self::Six),
252            "handouts9" => Some(Self::Nine),
253            _ => None,
254        }
255    }
256}
257
258// ---------------------------------------------------------------------------
259// PrintOrientation
260// ---------------------------------------------------------------------------
261
262/// Specifies the orientation for printing.
263#[non_exhaustive]
264#[derive(Debug, Clone, Copy, PartialEq, Eq)]
265pub enum PrintOrientation {
266    /// Portrait orientation.
267    Portrait,
268    /// Landscape orientation.
269    Landscape,
270}
271
272impl PrintOrientation {
273    /// Return the XML attribute value for this orientation.
274    #[must_use]
275    pub const fn to_xml_str(self) -> &'static str {
276        match self {
277            Self::Portrait => "portrait",
278            Self::Landscape => "landscape",
279        }
280    }
281
282    /// Parse an XML orientation attribute value.
283    #[must_use]
284    pub fn from_xml_str(s: &str) -> Option<Self> {
285        match s {
286            "portrait" => Some(Self::Portrait),
287            "landscape" => Some(Self::Landscape),
288            _ => None,
289        }
290    }
291}
292
293#[cfg(test)]
294mod tests {
295    use super::*;
296
297    #[test]
298    fn test_prog_id_roundtrip() {
299        let ids = [
300            ProgId::ExcelWorksheet,
301            ProgId::ExcelChart,
302            ProgId::WordDocument,
303            ProgId::PowerPointPresentation,
304            ProgId::Package,
305        ];
306        for id in ids {
307            let s = id.to_prog_id_str();
308            assert_eq!(ProgId::from_prog_id_str(s), Some(id));
309        }
310    }
311
312    #[test]
313    fn test_unknown_prog_id() {
314        assert_eq!(ProgId::from_prog_id_str("Unknown.App"), None);
315    }
316
317    #[test]
318    fn test_pp_media_type_roundtrip() {
319        let types = [PpMediaType::Movie, PpMediaType::Sound, PpMediaType::Other];
320        for t in types {
321            let s = t.to_xml_str();
322            assert_eq!(PpMediaType::from_xml_str(s), Some(t));
323        }
324    }
325
326    #[test]
327    fn test_pp_media_type_unknown() {
328        assert_eq!(PpMediaType::from_xml_str("unknown"), None);
329    }
330
331    #[test]
332    fn test_pp_media_type_values() {
333        assert_eq!(PpMediaType::Movie.to_xml_str(), "movie");
334        assert_eq!(PpMediaType::Sound.to_xml_str(), "sound");
335        assert_eq!(PpMediaType::Other.to_xml_str(), "other");
336    }
337
338    #[test]
339    fn test_excel_num_format_constants() {
340        assert_eq!(ExcelNumFormat::GENERAL, "General");
341        assert_eq!(ExcelNumFormat::NUMBER, "0.00");
342        assert_eq!(ExcelNumFormat::NUMBER_NO_DECIMAL, "0");
343        assert_eq!(ExcelNumFormat::CURRENCY, "$#,##0.00");
344        assert_eq!(ExcelNumFormat::PERCENTAGE, "0%");
345        assert_eq!(ExcelNumFormat::PERCENTAGE_DECIMAL, "0.00%");
346        assert_eq!(ExcelNumFormat::DATE, "m/d/yyyy");
347        assert_eq!(ExcelNumFormat::TIME, "h:mm:ss");
348    }
349
350    #[test]
351    fn test_print_color_mode_roundtrip() {
352        let modes = [
353            PrintColorMode::Color,
354            PrintColorMode::Grayscale,
355            PrintColorMode::PureBlackWhite,
356        ];
357        for m in modes {
358            let s = m.to_xml_str();
359            assert_eq!(PrintColorMode::from_xml_str(s), Some(m));
360        }
361    }
362
363    #[test]
364    fn test_print_color_mode_unknown() {
365        assert_eq!(PrintColorMode::from_xml_str("unknown"), None);
366    }
367
368    #[test]
369    fn test_print_what_roundtrip() {
370        let targets = [
371            PrintWhat::Slides,
372            PrintWhat::Handouts,
373            PrintWhat::Notes,
374            PrintWhat::Outline,
375        ];
376        for t in targets {
377            let s = t.to_xml_str();
378            assert_eq!(PrintWhat::from_xml_str(s), Some(t));
379        }
380    }
381
382    #[test]
383    fn test_print_what_unknown() {
384        assert_eq!(PrintWhat::from_xml_str("unknown"), None);
385    }
386
387    #[test]
388    fn test_handout_layout_roundtrip() {
389        let layouts = [
390            HandoutLayout::One,
391            HandoutLayout::Two,
392            HandoutLayout::Three,
393            HandoutLayout::Four,
394            HandoutLayout::Six,
395            HandoutLayout::Nine,
396        ];
397        for l in layouts {
398            let s = l.to_xml_str();
399            assert_eq!(HandoutLayout::from_xml_str(s), Some(l));
400        }
401    }
402
403    #[test]
404    fn test_handout_layout_unknown() {
405        assert_eq!(HandoutLayout::from_xml_str("unknown"), None);
406    }
407
408    #[test]
409    fn test_print_orientation_roundtrip() {
410        let orientations = [PrintOrientation::Portrait, PrintOrientation::Landscape];
411        for o in orientations {
412            let s = o.to_xml_str();
413            assert_eq!(PrintOrientation::from_xml_str(s), Some(o));
414        }
415    }
416
417    #[test]
418    fn test_print_orientation_unknown() {
419        assert_eq!(PrintOrientation::from_xml_str("unknown"), None);
420    }
421}