1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use anyhow::anyhow;
use serde::ser::Serialize;
use serde_json::value::{to_value, Map, Value};
use std::collections::BTreeMap;
use anyhow::Result;
/// The struct that holds the context of a template rendering.
///
/// Light wrapper around a `BTreeMap` for easier insertions of Serializable
/// values
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Context {
data: BTreeMap<String, Value>,
}
impl Context {
/// Initializes an empty context
pub fn new() -> Self {
Context {
data: BTreeMap::new(),
}
}
/// Converts the `val` parameter to `Value` and insert it into the context.
///
/// Panics if the serialization fails.
///
/// ```rust
/// # use genkit::Context;
/// let mut context = Context::new();
/// context.insert("number_users", &42);
/// ```
pub fn insert<T: Serialize + ?Sized, S: Into<String>>(&mut self, key: S, val: &T) {
self.data.insert(key.into(), to_value(val).unwrap());
}
/// Converts the `val` parameter to `Value` and insert it into the context.
///
/// Returns an error if the serialization fails.
///
/// ```rust
/// # use genkit::Context;
/// # struct CannotBeSerialized;
/// # impl serde::Serialize for CannotBeSerialized {
/// # fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
/// # Err(serde::ser::Error::custom("Error"))
/// # }
/// # }
/// # let user = CannotBeSerialized;
/// let mut context = Context::new();
/// // user is an instance of a struct implementing `Serialize`
/// if let Err(_) = context.try_insert("number_users", &user) {
/// // Serialization failed
/// }
/// ```
pub fn try_insert<T: Serialize + ?Sized, S: Into<String>>(
&mut self,
key: S,
val: &T,
) -> Result<()> {
self.data.insert(key.into(), to_value(val)?);
Ok(())
}
/// Appends the data of the `source` parameter to `self`, overwriting existing keys.
/// The source context will be dropped.
///
/// ```rust
/// # use genkit::Context;
/// let mut target = Context::new();
/// target.insert("a", &1);
/// target.insert("b", &2);
/// let mut source = Context::new();
/// source.insert("b", &3);
/// source.insert("d", &4);
/// target.extend(source);
/// ```
pub fn extend(&mut self, mut source: Context) {
self.data.append(&mut source.data);
}
/// Converts the context to a `serde_json::Value` consuming the context.
pub fn into_json(self) -> Value {
let mut m = Map::new();
for (key, value) in &self.data {
m.insert(key.to_owned(), value.clone());
}
Value::Object(m)
}
/// Takes a serde-json `Value` and convert it into a `Context` with no overhead/cloning.
pub fn from_value(obj: Value) -> Result<Self> {
match obj {
Value::Object(m) => {
let mut data = BTreeMap::new();
for (key, value) in m {
data.insert(key, value);
}
Ok(Context { data })
}
_ => Err(anyhow!(
"Creating a Context from a Value/Serialize requires it being a JSON object",
)),
}
}
/// Takes something that impl Serialize and create a context with it.
/// Meant to be used if you have a hashmap or a struct and don't want to insert values
/// one by one in the context.
pub fn from_serialize(value: impl Serialize) -> Result<Self> {
let obj = to_value(value)?;
Context::from_value(obj)
}
/// Returns the value at a given key index.
pub fn get(&self, index: &str) -> Option<&Value> {
self.data.get(index)
}
/// Remove a key from the context, returning the value at the key if the key was previously inserted into the context.
pub fn remove(&mut self, index: &str) -> Option<Value> {
self.data.remove(index)
}
/// Checks if a value exists at a specific index.
pub fn contains_key(&self, index: &str) -> bool {
self.data.contains_key(index)
}
}