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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
mod pages;
#[cfg(feature = "warp")]
mod warp_ui;
use crate::pages::{PageError, Pages};
use feattle_core::persist::Persist;
use feattle_core::{Feattles, HistoryError, UpdateError};
use serde_json::Value;
use std::error::Error;
use std::marker::PhantomData;
use std::sync::Arc;
#[cfg(feature = "warp")]
pub use warp_ui::run_warp_server;
pub struct AdminPanel<F, P> {
feattles: Arc<F>,
pages: Pages,
_phantom: PhantomData<P>,
}
#[derive(Debug, Clone)]
pub struct RenderedPage {
pub content_type: String,
pub content: Vec<u8>,
}
#[derive(Debug, thiserror::Error)]
pub enum RenderError<PersistError: Error + Send + Sync + 'static> {
#[error("the requested page does not exist")]
NotFound,
#[error("the template failed to render")]
Template(#[from] handlebars::RenderError),
#[error("failed to serialize or deserialize JSON")]
Serialization(#[from] serde_json::Error),
#[error("failed to recover history information")]
History(#[from] HistoryError<PersistError>),
#[error("failed to update value")]
Update(#[from] UpdateError<PersistError>),
}
impl<PersistError: Error + Send + Sync + 'static> From<PageError> for RenderError<PersistError> {
fn from(error: PageError) -> Self {
match error {
PageError::NotFound => RenderError::NotFound,
PageError::Template(error) => RenderError::Template(error),
PageError::Serialization(error) => RenderError::Serialization(error),
}
}
}
impl<F: Feattles<P> + Sync, P: Persist + Sync + 'static> AdminPanel<F, P> {
pub fn new(feattles: Arc<F>, label: String) -> Self {
AdminPanel {
feattles,
pages: Pages::new(label),
_phantom: PhantomData,
}
}
pub fn list_feattles(&self) -> Result<RenderedPage, RenderError<P::Error>> {
Ok(self.pages.render_feattles(
&self.feattles.definitions(),
self.feattles.last_reload(),
self.feattles.current_values().as_deref(),
)?)
}
pub async fn show_feattle(&self, key: &str) -> Result<RenderedPage, RenderError<P::Error>> {
let definition = self
.feattles
.definition(&key)
.ok_or(RenderError::NotFound)?;
let history = self.feattles.history(&key).await?;
Ok(self
.pages
.render_feattle(&definition, &history, self.feattles.last_reload())?)
}
pub async fn edit_feattle(
&self,
key: &str,
value_json: &str,
) -> Result<(), RenderError<P::Error>> {
log::info!(
"Received edit request for key {} with value {}",
key,
value_json
);
let value: Value = serde_json::from_str(&value_json)?;
self.feattles
.update(&key, value, "admin".to_owned())
.await?;
Ok(())
}
pub fn render_public_file(&self, path: &str) -> Result<RenderedPage, RenderError<P::Error>> {
Ok(self.pages.render_public_file(path)?)
}
}
#[cfg(test)]
mod tests {
use super::*;
use feattle_core::{feattles, Feattles};
feattles! {
struct MyToggles { a: bool, b: i32 }
}
#[tokio::test]
async fn test() {
use feattle_core::persist::NoPersistence;
let my_toggles = Arc::new(MyToggles::new(NoPersistence));
my_toggles.reload().await.unwrap();
let admin_panel = Arc::new(AdminPanel::new(
my_toggles,
"Project Panda - DEV".to_owned(),
));
admin_panel.list_feattles().unwrap();
admin_panel.show_feattle("a").await.unwrap();
admin_panel.show_feattle("non-existent").await.unwrap_err();
admin_panel.render_public_file("script.js").unwrap();
admin_panel.render_public_file("non-existent").unwrap_err();
admin_panel.edit_feattle("a", "true").await.unwrap();
admin_panel.edit_feattle("a", "17").await.unwrap_err();
}
}