mdbook_journal/journal/
entry.rs

1mod builder;
2mod meta;
3
4use crate::prelude::*;
5
6pub use builder::*;
7pub use meta::*;
8
9/// Journal Entry
10///
11/// Represents a single markdown file created from a `Topic`
12///
13#[derive(Debug, Clone, Serialize)]
14pub struct Entry {
15    /// Topic identifier to which this entry belongs
16    #[serde(serialize_with = "serialize_topic_name")]
17    topic: TopicName,
18    /// Time this entry was created
19    #[serde(serialize_with = "serialize_created_at")]
20    created_at: UtcDateTime,
21    /// Location where this Entry is persisted to disk
22    pub(crate) file_loc: Option<PathBuf>,
23    pub(crate) virtual_path: Option<PathBuf>,
24    /// Additional data found in the entries front-matter
25    meta: EntryMeta,
26    /// The contents of the markdown file except
27    /// for the front-matter
28    content: String,
29}
30
31impl Entry {
32    pub(crate) fn builder<S>(topic: S) -> EntryBuilder
33    where
34        S: Into<TopicName>,
35    {
36        EntryBuilder::new(topic)
37    }
38
39    pub fn created_at(&self) -> &UtcDateTime {
40        &self.created_at
41    }
42
43    pub fn meta(&self) -> &EntryMeta {
44        &self.meta
45    }
46
47    pub fn meta_value<K>(&self, key: &K) -> Option<&MetaValue>
48    where
49        K: AsRef<str>,
50    {
51        self.meta.get(key)
52    }
53
54    pub fn topic_name(&self) -> &str {
55        self.topic.as_ref()
56    }
57
58    pub fn content(&self) -> &str {
59        &self.content
60    }
61
62    pub fn file_location(&self) -> Option<&PathBuf> {
63        self.file_loc.as_ref()
64    }
65}
66
67fn serialize_topic_name<S>(name: &TopicName, serializer: S) -> std::result::Result<S::Ok, S::Error>
68where
69    S: serde::Serializer,
70{
71    serializer.serialize_str(name.as_ref())
72}
73
74fn serialize_created_at<S>(
75    date: &UtcDateTime,
76    serializer: S,
77) -> std::result::Result<S::Ok, S::Error>
78where
79    S: serde::Serializer,
80{
81    let data = &date.to_rfc3339();
82    serializer.serialize_str(data)
83}