pptx_to_md/parser_config.rs
1use std::path::PathBuf;
2
3/// Determines how images are handled during content export.
4///
5/// # Members
6///
7/// | Member | Description |
8/// |-----------------------|-----------------------------------------------------------------------------------------------------------------------------------|
9/// | `InMarkdown` | Images are embedded directly in the Markdown output using standard syntax as `base64` data (`![]()`) |
10/// | `Manually` | Image handling is delegated to the user, requiring manual copying or referencing (as `base64` encoded string) |
11/// | `Save` | Images will be saved in a provided output directory and integrated using `<a>` tag syntax (`<a href="file:///<abs_path>"></a>`) |
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub enum ImageHandlingMode {
14 InMarkdown,
15 Manually,
16 Save,
17}
18
19/// Configuration options for the PPTX parser.
20///
21/// Use [`ParserConfig::builder()`] to create a configuration instance.
22/// This allows you to customize only the desired fields while falling back to sensible defaults for the rest.
23///
24/// # Configuration Options
25///
26/// | Parameter | Type | Default | Description |
27/// |---------------------------|-----------------------|---------------|-----------------------------------------------------------------------------------------------------------|
28/// | `extract_images` | `bool` | `true` | Whether images are extracted from slides or not. If false, images can not be extracted manually either |
29/// | `compress_images` | `bool` | `true` | Whether images are compressed before encoding or not. Effects manually extracted images too |
30/// | `image_quality` | `u8` | `80` | Compression level (0-100);<br/> higher values retain more detail but increase file size |
31/// | `image_handling_mode` | `ImageHandlingMode` | `InMarkdown` | Determines how images are handled during content export |
32/// | `image_output_path` | `Option<PathBuf>` | `None` | Output directory path for `ImageHandlingMode::Save` (mandatory for the saving mode) |
33/// | `include_slide_comment` | `bool` | `true` | Weather the slide number comment is included or not (`<!-- Slide [n] -->`) |
34///
35/// # Example
36///
37/// ```
38/// use std::path::PathBuf;
39/// use pptx_to_md::{ImageHandlingMode, ParserConfig};
40///
41/// let config = ParserConfig::builder()
42/// .extract_images(true)
43/// .compress_images(true)
44/// .quality(75)
45/// .image_handling_mode(ImageHandlingMode::Save)
46/// .image_output_path(PathBuf::from("/path/to/output/dir/"))
47/// .build();
48/// ```
49#[derive(Debug, Clone)]
50pub struct ParserConfig {
51 pub extract_images: bool,
52 pub compress_images: bool,
53 pub quality: u8,
54 pub image_handling_mode: ImageHandlingMode,
55 pub image_output_path: Option<PathBuf>,
56 pub include_slide_comment: bool,
57}
58
59impl Default for ParserConfig {
60 fn default() -> Self {
61 Self {
62 extract_images: true,
63 compress_images: true,
64 quality: 80,
65 image_handling_mode: ImageHandlingMode::InMarkdown,
66 image_output_path: None,
67 include_slide_comment: true,
68 }
69 }
70}
71
72impl ParserConfig {
73 pub fn builder() -> ParserConfigBuilder {
74 ParserConfigBuilder::default()
75 }
76}
77
78/// Builder for [`ParserConfig`].
79///
80/// Allows setting individual configuration fields while falling back to defaults for any unspecified values
81#[derive(Debug, Default)]
82pub struct ParserConfigBuilder {
83 extract_images: Option<bool>,
84 compress_images: Option<bool>,
85 image_quality: Option<u8>,
86 image_handling_mode: Option<ImageHandlingMode>,
87 image_output_path: Option<PathBuf>,
88 include_slide_comment: Option<bool>,
89}
90
91impl ParserConfigBuilder {
92 /// Sets weather images should be extracted from the slides.
93 pub fn extract_images(mut self, value: bool) -> Self {
94 self.extract_images = Some(value);
95 self
96 }
97
98 /// Sets weather images should be compressed before encoded to base64 or not
99 pub fn compress_images(mut self, value: bool) -> Self {
100 self.compress_images = Some(value);
101 self
102 }
103
104 /// Specifies the desired image quality where `100` is the original quality and `50` means half the quality
105 /// The lower the quality, the smaller the file size of the output image will be
106 pub fn quality(mut self, value: u8) -> Self {
107 self.image_quality = Some(value);
108 self
109 }
110
111 /// Specifies the mode for processing the image after its extracted
112 pub fn image_handling_mode(mut self, value: ImageHandlingMode) -> Self {
113 self.image_handling_mode = Some(value);
114 self
115 }
116
117 /// Specifies the output directory for the [`ImageHandlingMode::Save`]
118 pub fn image_output_path<P>(mut self, path: P) -> Self
119 where
120 P: Into<PathBuf>,
121 {
122 self.image_output_path = Some(path.into());
123 self
124 }
125
126 /// Sets weather comments with current slide number are included or not
127 pub fn include_slide_comment(mut self, value: bool) -> Self {
128 self.include_slide_comment = Some(value);
129 self
130 }
131
132 /// Builds the final [`ParserConfig`] instance, applying default values for any fields that were not set.
133 pub fn build(self) -> ParserConfig {
134 ParserConfig {
135 extract_images: self.extract_images.unwrap_or(true),
136 compress_images: self.compress_images.unwrap_or(true),
137 quality: self.image_quality.unwrap_or(80),
138 image_handling_mode: self.image_handling_mode.unwrap_or(ImageHandlingMode::InMarkdown),
139 image_output_path: self.image_output_path,
140 include_slide_comment: self.include_slide_comment.unwrap_or(true),
141 }
142 }
143}