mdbook_journal/journal/topic/
variables.rs1pub use crate::prelude::*;
2
3pub type DefaultValue = Arc<str>;
4pub type KeyName = Arc<str>;
5
6#[derive(Debug)]
7pub struct Variable {
8 key: KeyName,
9 required: bool,
10 default: Option<DefaultValue>,
11}
12
13#[derive(Debug, Default)]
14pub struct VariableMap {
15 data: BTreeMap<KeyName, Variable>,
16}
17
18impl Variable {
19 pub fn new<S>(key: S) -> Self
20 where
21 S: Into<KeyName>,
22 {
23 Self {
24 key: key.into(),
25 required: false,
26 default: None,
27 }
28 }
29
30 pub fn required(self) -> Self {
31 Self {
32 required: true,
33 ..self
34 }
35 }
36
37 pub fn default<S>(self, value: S) -> Self
38 where
39 S: Into<DefaultValue>,
40 {
41 Self {
42 default: Some(value.into()),
43 ..self
44 }
45 }
46
47 pub fn is_required(&self) -> bool {
48 self.required
49 }
50
51 pub fn key(&self) -> &str {
52 self.key.as_ref()
53 }
54
55 pub fn default_value(&self) -> Option<MetaValue> {
56 self.default
57 .as_ref()
58 .map(ToString::to_string)
59 .map(MetaValue::String)
60 }
61}
62
63impl VariableMap {
64 pub fn insert(&mut self, var: Variable) {
65 self.data.insert(var.key.clone(), var);
66 }
67
68 pub fn get<K>(&self, key: &K) -> Option<&Variable>
69 where
70 K: AsRef<str>,
71 {
72 self.data.get(key.as_ref())
73 }
74
75 pub fn iter(&self) -> impl Iterator<Item = &Variable> {
76 self.data.values()
77 }
78
79 pub fn is_empty(&self) -> bool {
80 self.data.is_empty()
81 }
82}