1use sim_cookbook::{RecipeCard, RecipeStore, next, ordered_cards, view};
2use sim_kernel::{Cx, Result};
3use sim_lib_cookbook::{run_recipe, seeded_recipe_store};
4
5use crate::cookbook_web_json::{
6 render_error_json, render_index_json, render_recipe_json, render_run_json, render_search_json,
7};
8
9const JSON: &str = "application/json; charset=utf-8";
10const HTML: &str = "text/html; charset=utf-8";
11
12#[derive(Clone, Debug)]
14pub struct CookbookWebState {
15 store: RecipeStore,
16}
17
18impl CookbookWebState {
19 pub fn seeded() -> Result<Self> {
21 Ok(Self {
22 store: seeded_recipe_store()?,
23 })
24 }
25
26 pub fn from_store(store: RecipeStore) -> Self {
28 Self { store }
29 }
30
31 pub fn empty() -> Self {
33 Self::from_store(RecipeStore::new())
34 }
35
36 pub fn store(&self) -> &RecipeStore {
38 &self.store
39 }
40
41 pub fn handle_request(
47 &self,
48 method: &str,
49 target: &str,
50 cx: Option<&mut Cx>,
51 ) -> CookbookWebResponse {
52 let (path, query) = split_target(target);
53 match (method, path) {
54 ("GET", "/cookbook") => {
55 let selected = query.and_then(|q| query_value(q, "recipe"));
56 CookbookWebResponse::html(render_page(&self.store, selected.as_deref()))
57 }
58 ("GET", "/api/cookbook") => CookbookWebResponse::json(render_index_json(&self.store)),
59 ("GET", "/api/cookbook/search") => {
60 let q = query
61 .and_then(|query| query_value(query, "q"))
62 .unwrap_or_default();
63 CookbookWebResponse::json(render_search_json(&self.store, &q))
64 }
65 ("POST", path)
66 if path.starts_with("/api/cookbook/recipe/") && path.ends_with("/run") =>
67 {
68 let start = "/api/cookbook/recipe/".len();
69 let end = path.len() - "/run".len();
70 let raw_id = &path[start..end];
71 let Some(cx) = cx else {
72 return CookbookWebResponse::server_error(
73 "cookbook run endpoint requires a runtime context",
74 );
75 };
76 let card = match decode_component(raw_id, false)
77 .and_then(|id| resolve_recipe(&self.store, &id))
78 {
79 Ok(card) => card.clone(),
80 Err(err) => return response_for_resolve_error(err),
81 };
82 match run_recipe(cx, &card) {
83 Ok(run) => CookbookWebResponse::json(render_run_json(&run)),
84 Err(err) => CookbookWebResponse::server_error(err.to_string()),
85 }
86 }
87 (_, path) if path.starts_with("/api/cookbook/recipe/") && path.ends_with("/run") => {
88 CookbookWebResponse::method_not_allowed()
89 }
90 ("GET", path) if path.starts_with("/api/cookbook/recipe/") => {
91 let raw_id = &path["/api/cookbook/recipe/".len()..];
92 match decode_component(raw_id, false)
93 .and_then(|id| resolve_recipe(&self.store, &id))
94 {
95 Ok(card) => CookbookWebResponse::json(render_recipe_json(&self.store, card)),
96 Err(err) => response_for_resolve_error(err),
97 }
98 }
99 (_, "/api/cookbook") | (_, "/api/cookbook/search") | (_, "/cookbook") => {
100 CookbookWebResponse::method_not_allowed()
101 }
102 (_, path) if path.starts_with("/api/cookbook/recipe/") => {
103 CookbookWebResponse::method_not_allowed()
104 }
105 _ => CookbookWebResponse::not_found("unknown cookbook route"),
106 }
107 }
108}
109
110#[derive(Clone, Debug, PartialEq, Eq)]
112pub struct CookbookWebResponse {
113 pub status: u16,
115 pub content_type: &'static str,
117 pub body: String,
119}
120
121impl CookbookWebResponse {
122 fn html(body: String) -> Self {
123 Self {
124 status: 200,
125 content_type: HTML,
126 body,
127 }
128 }
129
130 fn json(body: String) -> Self {
131 Self {
132 status: 200,
133 content_type: JSON,
134 body,
135 }
136 }
137
138 fn not_found(message: impl AsRef<str>) -> Self {
139 Self::error(404, message)
140 }
141
142 fn method_not_allowed() -> Self {
143 Self::error(405, "method not allowed")
144 }
145
146 fn server_error(message: impl AsRef<str>) -> Self {
147 Self::error(500, message)
148 }
149
150 fn error(status: u16, message: impl AsRef<str>) -> Self {
151 Self {
152 status,
153 content_type: JSON,
154 body: render_error_json(message.as_ref()),
155 }
156 }
157}
158
159#[derive(Debug, PartialEq, Eq)]
160enum ResolveError {
161 Unknown(String),
162 Ambiguous(String),
163 BadRequest(String),
164}
165
166fn response_for_resolve_error(err: ResolveError) -> CookbookWebResponse {
167 match err {
168 ResolveError::Unknown(message) => CookbookWebResponse::not_found(message),
169 ResolveError::Ambiguous(message) | ResolveError::BadRequest(message) => {
170 CookbookWebResponse::error(400, message)
171 }
172 }
173}
174
175fn resolve_recipe<'a>(
176 store: &'a RecipeStore,
177 id: &str,
178) -> std::result::Result<&'a RecipeCard, ResolveError> {
179 if id.trim().is_empty() {
180 return Err(ResolveError::BadRequest(
181 "cookbook recipe id must not be empty".to_owned(),
182 ));
183 }
184 if let Some(card) = store.card(id) {
185 return Ok(card);
186 }
187 let suffix = format!("/{id}");
188 let candidates: Vec<&RecipeCard> = ordered_cards(store)
189 .into_iter()
190 .filter(|card| card.id.ends_with(&suffix))
191 .collect();
192 match candidates.as_slice() {
193 [card] => Ok(*card),
194 [] => Err(ResolveError::Unknown(format!("unknown recipe {id}"))),
195 many => Err(ResolveError::Ambiguous(format!(
196 "ambiguous recipe {id}: {}",
197 many.iter()
198 .map(|card| card.id.as_str())
199 .collect::<Vec<_>>()
200 .join(", ")
201 ))),
202 }
203}
204
205fn render_page(store: &RecipeStore, selected: Option<&str>) -> String {
206 let selected_card = selected
207 .and_then(|id| resolve_recipe(store, id).ok())
208 .or_else(|| ordered_cards(store).first().copied());
209 let selected_id = selected_card.map(|card| card.id.as_str());
210 let mut out = String::from(
211 r#"<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>SIM Cookbook</title><link rel="stylesheet" href="/styles/theme.css" /></head><body><nav class="top-nav"><a href="/">Shell</a><a href="/cookbook" aria-current="page">Cookbook</a></nav><main class="cookbook-shell">"#,
212 );
213 out.push_str(r#"<aside class="cookbook-rail"><input type="search" aria-label="Search recipes" placeholder="Search" /><nav class="cookbook-tree">"#);
214 if store.is_empty() {
215 out.push_str(r#"<p class="empty-state">No recipes loaded.</p>"#);
216 } else {
217 render_tree_html(&mut out, store, selected_id);
218 }
219 out.push_str(r#"</nav></aside><section class="cookbook-main">"#);
220 match selected_card {
221 Some(card) => render_recipe_html(&mut out, store, card),
222 None => out.push_str(r#"<h1>Cookbook</h1><p class="empty-state">No recipes loaded.</p>"#),
223 }
224 out.push_str("</section></main></body></html>");
225 out
226}
227
228fn render_tree_html(out: &mut String, store: &RecipeStore, selected: Option<&str>) {
229 for book in view(store).books {
230 out.push_str("<section><h2>");
231 push_html(out, &book.title);
232 out.push_str("</h2>");
233 for chapter in book.chapters {
234 out.push_str("<div><h3>");
235 push_html(out, &chapter.title);
236 out.push_str("</h3><ul>");
237 for recipe in chapter.recipes {
238 let active = selected == Some(recipe.id.as_str());
239 out.push_str("<li><a href=\"/cookbook?recipe=");
240 push_attr(out, &recipe.id);
241 out.push('"');
242 if active {
243 out.push_str(" aria-current=\"page\" class=\"selected\"");
244 }
245 out.push('>');
246 push_html(out, &recipe.title);
247 out.push_str("</a></li>");
248 }
249 out.push_str("</ul></div>");
250 }
251 out.push_str("</section>");
252 }
253}
254
255fn render_recipe_html(out: &mut String, store: &RecipeStore, card: &RecipeCard) {
256 out.push_str("<h1>");
257 push_html(out, &card.title);
258 out.push_str("</h1><div class=\"purpose\">");
259 push_purpose_html(out, &card.purpose);
260 out.push_str("</div><div class=\"recipe-actions\"><button type=\"button\">Copy</button><button type=\"button\">Run</button></div><pre><code>");
261 push_html(out, &String::from_utf8_lossy(&card.setup));
262 out.push_str("</code></pre><section class=\"results-panel\" aria-live=\"polite\">Run this recipe to see pass/fail data.</section><footer>");
263 if let Some(next) = next(store, &card.id) {
264 out.push_str("<a href=\"/cookbook?recipe=");
265 push_attr(out, &next.id);
266 out.push_str("\">Next recipe -></a>");
267 } else {
268 out.push_str("<span>No next recipe.</span>");
269 }
270 out.push_str("</footer>");
271}
272
273fn split_target(target: &str) -> (&str, Option<&str>) {
274 match target.split_once('?') {
275 Some((path, query)) => (path, Some(query)),
276 None => (target, None),
277 }
278}
279
280fn query_value(query: &str, key: &str) -> Option<String> {
281 query.split('&').find_map(|pair| {
282 let (name, value) = pair.split_once('=').unwrap_or((pair, ""));
283 (name == key).then(|| decode_component(value, true).unwrap_or_default())
284 })
285}
286
287fn decode_component(raw: &str, plus_is_space: bool) -> std::result::Result<String, ResolveError> {
288 let bytes = raw.as_bytes();
289 let mut out = Vec::with_capacity(bytes.len());
290 let mut i = 0;
291 while i < bytes.len() {
292 match bytes[i] {
293 b'%' if i + 2 < bytes.len() => {
294 let hi = hex(bytes[i + 1]);
295 let lo = hex(bytes[i + 2]);
296 match (hi, lo) {
297 (Some(hi), Some(lo)) => out.push((hi << 4) | lo),
298 _ => {
299 return Err(ResolveError::BadRequest(
300 "invalid percent escape in cookbook route".to_owned(),
301 ));
302 }
303 }
304 i += 3;
305 }
306 b'%' => {
307 return Err(ResolveError::BadRequest(
308 "truncated percent escape in cookbook route".to_owned(),
309 ));
310 }
311 b'+' if plus_is_space => {
312 out.push(b' ');
313 i += 1;
314 }
315 byte => {
316 out.push(byte);
317 i += 1;
318 }
319 }
320 }
321 String::from_utf8(out).map_err(|err| ResolveError::BadRequest(err.to_string()))
322}
323
324fn hex(byte: u8) -> Option<u8> {
325 match byte {
326 b'0'..=b'9' => Some(byte - b'0'),
327 b'a'..=b'f' => Some(byte - b'a' + 10),
328 b'A'..=b'F' => Some(byte - b'A' + 10),
329 _ => None,
330 }
331}
332
333fn push_purpose_html(out: &mut String, purpose: &str) {
334 let mut wrote = false;
335 let mut open_p = false;
336 for raw in purpose.lines() {
337 let line = raw.trim();
338 if line.is_empty() {
339 if open_p {
340 out.push_str("</p>");
341 open_p = false;
342 }
343 continue;
344 }
345 if let Some(title) = line.strip_prefix("# ") {
346 if open_p {
347 out.push_str("</p>");
348 open_p = false;
349 }
350 out.push_str("<h2>");
351 push_html(out, title);
352 out.push_str("</h2>");
353 } else {
354 if !open_p {
355 out.push_str("<p>");
356 open_p = true;
357 } else {
358 out.push(' ');
359 }
360 push_html(out, line);
361 }
362 wrote = true;
363 }
364 if open_p {
365 out.push_str("</p>");
366 }
367 if !wrote {
368 out.push_str("<p>No purpose provided.</p>");
369 }
370}
371
372fn push_html(out: &mut String, text: &str) {
373 for ch in text.chars() {
374 match ch {
375 '&' => out.push_str("&"),
376 '<' => out.push_str("<"),
377 '>' => out.push_str(">"),
378 '"' => out.push_str("""),
379 '\'' => out.push_str("'"),
380 ch => out.push(ch),
381 }
382 }
383}
384
385fn push_attr(out: &mut String, text: &str) {
386 push_html(out, text);
387}