Skip to content

Commit f95553d

Browse files
authored
[Feat] add map function to map current config into builder (#7)
1 parent f059927 commit f95553d

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

core/src/config.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ pub struct Window {
166166
pub shadow: f32,
167167
}
168168

169+
impl WindowBuilder {
170+
pub fn from_window(window: Window) -> WindowBuilder {
171+
WindowBuilder {
172+
margin: Some(window.margin),
173+
title: Some(window.title),
174+
border: Some(window.border),
175+
mac_window_bar: Some(window.mac_window_bar),
176+
shadow: Some(window.shadow),
177+
}
178+
}
179+
}
180+
169181
#[derive(Clone, Serialize, Deserialize, Debug)]
170182
#[serde(untagged)]
171183
pub enum HighlightLine {
@@ -228,6 +240,21 @@ pub struct Code {
228240
pub file_path: Option<String>,
229241
}
230242

243+
impl CodeBuilder {
244+
pub fn from_code(code: Code) -> CodeBuilder {
245+
CodeBuilder {
246+
content: Some(code.content),
247+
font_family: Some(code.font_family),
248+
theme: Some(code.theme),
249+
breadcrumbs: Some(code.breadcrumbs),
250+
line_number: Some(code.line_number),
251+
highlight_lines: Some(code.highlight_lines),
252+
language: Some(code.language),
253+
file_path: Some(code.file_path),
254+
}
255+
}
256+
}
257+
231258
/// Draw a watermark below the code, you can use this to add a logo or any other text
232259
/// The watermark is designed as a place for users to provide personalize label
233260
#[derive(Serialize, Deserialize, Clone, Builder, Debug)]
@@ -243,8 +270,23 @@ pub struct Watermark {
243270
pub color: String,
244271
}
245272

273+
impl WatermarkBuilder {
274+
pub fn from_watermark(watermark: Option<Watermark>) -> WatermarkBuilder {
275+
watermark
276+
.and_then(|watermark| {
277+
Some(WatermarkBuilder {
278+
content: Some(watermark.content),
279+
font_family: Some(watermark.font_family),
280+
color: Some(watermark.color),
281+
})
282+
})
283+
.unwrap_or(WatermarkBuilder::default())
284+
}
285+
}
286+
246287
#[derive(Clone, Builder, Serialize, Deserialize, Debug)]
247288
#[builder(name = "CodeSnap", build_fn(validate = "Self::validate"))]
289+
#[builder(derive(serde::Deserialize))]
248290
#[serde(rename_all = "camelCase")]
249291
pub struct SnapshotConfig {
250292
#[builder(setter(into, strip_option), default = WindowBuilder::default().build().unwrap())]
@@ -302,8 +344,41 @@ impl CodeSnap {
302344
Ok(())
303345
}
304346

305-
pub fn from_config(config: &str) -> Result<SnapshotConfig, serde_json::Error> {
306-
serde_json::from_str::<SnapshotConfig>(config)
347+
pub fn from_config(config: &str) -> Result<CodeSnap, serde_json::Error> {
348+
serde_json::from_str::<CodeSnap>(config)
349+
}
350+
351+
pub fn map_code<F>(&mut self, f: F) -> Result<&mut Self, CodeBuilderError>
352+
where
353+
F: Fn(Code) -> Result<Code, CodeBuilderError>,
354+
{
355+
if let Some(ref code) = self.code {
356+
self.code = Some(f(code.clone())?);
357+
}
358+
359+
Ok(self)
360+
}
361+
362+
pub fn map_window<F>(&mut self, f: F) -> Result<&mut Self, WindowBuilderError>
363+
where
364+
F: Fn(Window) -> Result<Window, WindowBuilderError>,
365+
{
366+
if let Some(ref window) = self.window {
367+
self.window = Some(f(window.clone())?);
368+
}
369+
370+
Ok(self)
371+
}
372+
373+
pub fn map_watermark<F>(&mut self, f: F) -> Result<&mut Self, WatermarkBuilderError>
374+
where
375+
F: Fn(Option<Watermark>) -> Result<Watermark, WatermarkBuilderError>,
376+
{
377+
if let Some(ref watermark) = self.watermark {
378+
self.watermark = Some(Some(f(watermark.clone())?));
379+
}
380+
381+
Ok(self)
307382
}
308383
}
309384

0 commit comments

Comments
 (0)