mdbook_journal/journal/entry/
builder.rs1use crate::prelude::*;
2
3#[derive(Debug)]
4pub struct EntryBuilder {
5 entry: Entry,
6}
7
8impl AsRef<Entry> for EntryBuilder {
9 fn as_ref(&self) -> &Entry {
10 &self.entry
11 }
12}
13
14impl EntryBuilder {
15 pub(super) fn new<S>(topic: S) -> EntryBuilder
16 where
17 S: Into<TopicName>,
18 {
19 Self {
20 entry: Entry {
21 topic: topic.into(),
22 content: String::new(),
23 meta: EntryMeta::default(),
24 created_at: Utc::now(),
25 file_loc: None,
26 virtual_path: None,
27 },
28 }
29 }
30
31 pub fn created_at<D>(mut self, created_at: D) -> Self
32 where
33 D: Into<UtcDateTime>,
34 {
35 self.entry.created_at = created_at.into();
36 self
37 }
38
39 pub fn virtual_path<P>(mut self, path: P) -> Self
40 where
41 P: Into<PathBuf>,
42 {
43 self.entry.virtual_path = Some(path.into());
44 self
45 }
46
47 pub fn content<S>(mut self, content: S) -> Self
48 where
49 S: Into<String>,
50 {
51 self.entry.content = content.into();
52 self
53 }
54
55 pub fn file_name<N>(mut self, file_name: N) -> Self
56 where
57 N: Into<PathBuf>,
58 {
59 let filename: PathBuf = file_name.into();
60 self.entry.file_loc = filename.file_name().map(Into::into);
61 self
62 }
63
64 pub fn add_meta(mut self, meta: EntryMeta) -> Self {
65 self.entry.meta = meta;
66 self
67 }
68
69 pub fn add_meta_value<K, V>(mut self, key: K, value: V) -> Self
70 where
71 K: Into<String>,
72 V: Into<MetaValue>,
73 {
74 self.entry.meta.insert(key, value);
75 self
76 }
77
78 pub fn build(self) -> Entry {
79 self.entry
80 }
81}