Skip to main content

ppt_rs/integration/
builders.rs

1//! Builder types for presentations and slides
2
3use crate::generator;
4use crate::exc::Result;
5use crate::config::Config;
6use crate::constants;
7use std::fs;
8
9/// Complete PPTX presentation builder
10pub struct PresentationBuilder {
11    pub title: String,
12    pub slides: usize,
13    pub config: Config,
14}
15
16impl PresentationBuilder {
17    /// Create a new presentation builder
18    pub fn new(title: &str) -> Self {
19        PresentationBuilder {
20            title: title.to_string(),
21            slides: constants::presentation::DEFAULT_SLIDES,
22            config: Config::default(),
23        }
24    }
25
26    /// Create with custom config
27    pub fn with_config(mut self, config: Config) -> Self {
28        self.config = config;
29        self
30    }
31
32    /// Set number of slides
33    pub fn with_slides(mut self, count: usize) -> Self {
34        self.slides = count;
35        self
36    }
37
38    /// Build and generate PPTX file
39    pub fn build(&self) -> Result<Vec<u8>> {
40        generator::create_pptx(&self.title, self.slides)
41            .map_err(|e| crate::exc::PptxError::Generic(e.to_string()))
42    }
43
44    /// Save to file
45    pub fn save_to_file(&self, path: &str) -> Result<()> {
46        let pptx_data = self.build()?;
47        fs::write(path, pptx_data)
48            .map_err(crate::exc::PptxError::Io)
49    }
50
51    /// Save to configured output directory
52    pub fn save(&self, filename: &str) -> Result<()> {
53        let path = self.config.output_path(filename);
54        self.save_to_file(&path)
55    }
56}
57
58/// Presentation metadata
59pub struct PresentationMetadata {
60    pub title: String,
61    pub slides: usize,
62    pub created: String,
63    pub modified: String,
64}
65
66impl PresentationMetadata {
67    /// Create new metadata
68    pub fn new(title: &str, slides: usize) -> Self {
69        let now = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
70        PresentationMetadata {
71            title: title.to_string(),
72            slides,
73            created: now.clone(),
74            modified: now,
75        }
76    }
77}
78
79/// Slide builder
80pub struct SlideBuilder {
81    pub title: String,
82    pub content: String,
83}
84
85impl SlideBuilder {
86    /// Create new slide
87    pub fn new(title: &str) -> Self {
88        SlideBuilder {
89            title: title.to_string(),
90            content: String::new(),
91        }
92    }
93
94    /// Add content
95    pub fn with_content(mut self, content: &str) -> Self {
96        self.content = content.to_string();
97        self
98    }
99
100    /// Get slide data
101    pub fn build(&self) -> (String, String) {
102        (self.title.clone(), self.content.clone())
103    }
104}