ppt_rs/integration/
builders.rs1use crate::generator;
4use crate::exc::Result;
5use crate::config::Config;
6use crate::constants;
7use std::fs;
8
9pub struct PresentationBuilder {
11 pub title: String,
12 pub slides: usize,
13 pub config: Config,
14}
15
16impl PresentationBuilder {
17 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 pub fn with_config(mut self, config: Config) -> Self {
28 self.config = config;
29 self
30 }
31
32 pub fn with_slides(mut self, count: usize) -> Self {
34 self.slides = count;
35 self
36 }
37
38 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 pub fn save_to_file(&self, path: &str) -> Result<()> {
46 let pptx_data = self.build()?;
47 fs::write(path, pptx_data)
48 .map_err(|e| crate::exc::PptxError::Io(e))
49 }
50
51 pub fn save(&self, filename: &str) -> Result<()> {
53 let path = self.config.output_path(filename);
54 self.save_to_file(&path)
55 }
56}
57
58pub struct PresentationMetadata {
60 pub title: String,
61 pub slides: usize,
62 pub created: String,
63 pub modified: String,
64}
65
66impl PresentationMetadata {
67 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
79pub struct SlideBuilder {
81 pub title: String,
82 pub content: String,
83}
84
85impl SlideBuilder {
86 pub fn new(title: &str) -> Self {
88 SlideBuilder {
89 title: title.to_string(),
90 content: String::new(),
91 }
92 }
93
94 pub fn with_content(mut self, content: &str) -> Self {
96 self.content = content.to_string();
97 self
98 }
99
100 pub fn build(&self) -> (String, String) {
102 (self.title.clone(), self.content.clone())
103 }
104}