unchained_web 0.2.2

Simple router and template renderer.
Documentation
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::collections::HashMap;

use crate::error::{Error, WebResult};

use super::{
    context::{ContextMap, ContextTree as Ctx, Primitive::*},
    render::{load_template, render_html, RenderOptions},
    text_parse::{between_connected_patterns, Match},
};

const INSIDE_COMPONENT_OP_ID: &str = "inside_component_operation_identifier";

pub fn template_operation(content: &str) -> Option<Match> {
    between_connected_patterns(content, "{*", "*}")
}

#[derive(Debug)]
pub struct TemplateOperationCall {
    pub name: String,
    pub parameters: Vec<String>,
    pub children: Option<String>,
}

fn childless_templ_op_call(op_content: &str) -> Option<TemplateOperationCall> {
    let splitted = op_content.trim().split(' ').map(|s| s.to_string());
    let name = splitted.clone().take(1).collect::<String>();
    if name.is_empty() {
        return None;
    }
    Some(TemplateOperationCall {
        name,
        parameters: splitted.skip(1).collect::<Vec<String>>(),
        children: None,
    })
}

pub fn operation_params_and_children(operation: &str) -> Option<TemplateOperationCall> {
    let find = between_connected_patterns(operation, "{", "}");

    if let Some(find) = find {
        let mut operation = operation.to_string();
        operation.replace_range(find.from..=find.to, "");

        let op_call = childless_templ_op_call(&operation);
        return match op_call {
            None => None,
            Some(mut operation) => {
                operation.children = Some(find.content);
                return Some(operation);
            }
        };
    }
    childless_templ_op_call(operation)
}

pub type TemplateOperation =
    fn(TemplateOperationCall, &ContextMap, &RenderOptions) -> WebResult<String>;

/// Get the operation function from a template operation name
/// If not found as a default name, it uses a custom operation from template options.
/// Current reserved operation names:
/// `"get"`, `"for"`, `"if`, `"component`, `"slot`, `"comment`, `"dbg`
///
/// Example template operation
/// ```html
/// {* if boolean {
///  <div>Renders if true</div>
/// }
/// *}
/// ```
pub fn get_template_operation(
    op_name: &str,
    custom_operations: HashMap<&str, TemplateOperation>,
) -> Option<TemplateOperation> {
    match op_name {
        "get" => Some(attribute_operation),
        "for" => Some(for_loop_operation),
        "if" => Some(if_operation),
        "component" => Some(component_operation),
        "slot" => Some(slot),
        "comment" => Some(comment_operation),
        "dbg" => Some(dbg_operation),
        s => custom_operations.get(s).copied(),
    }
}

pub fn unwrap_n_params<const N: usize>(params: &[String]) -> WebResult<[&str; N]> {
    let mut arr = [""; N];
    if params.len() != N {
        return Err(Error::InvalidParams(format!(
            "Expected {N} parameters, got {}",
            params.len()
        )));
    }
    for (i, param) in params.iter().enumerate() {
        arr[i] = param.as_str();
    }
    Ok(arr)
}

fn attribute_from_context(attribute: &str, context: &ContextMap) -> WebResult<Ctx> {
    let splitted = attribute.split('.');
    let mut resulting_attribute = None;
    let mut new_context = context.clone();

    let mut last = false;
    let splitted = splitted.collect::<Vec<&str>>();
    for i in 0..splitted.len() {
        if i == splitted.len() - 1 {
            last = true;
        }
        let attribute = splitted[i];
        resulting_attribute = match new_context.get(attribute) {
            Some(Ctx::Branch(b)) => {
                if last {
                    Some(Ctx::Branch(b.clone()))
                } else {
                    new_context = *b.clone();
                    continue;
                }
            }
            Some(s) => Some(s.clone()),
            _ => {
                return Err(Error::InvalidParams(format!(
                    "Invalid attribute: {} not found in context",
                    attribute
                )))
            }
        };
    }
    if resulting_attribute.is_none() {
        return Err(Error::InvalidParams(format!(
            "Attribute {} not found in context",
            attribute
        )));
    }
    Ok(resulting_attribute.unwrap())
}

fn attribute_operation(
    call: TemplateOperationCall,
    context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    let attribute = unwrap_n_params::<1>(&call.parameters)?[0];
    match attribute_from_context(attribute, context) {
        Ok(a) => match a {
            Ctx::Leaf(s) => Ok(s.to_string()),
            Ctx::Slot(s) => Ok(s.to_string()),
            Ctx::Branch(_) => Err(Error::InvalidParams(format!(
                "Attribute {} is a nested object. Retrieve a primitive instead.",
                attribute
            ))),
            Ctx::Array(_) => Err(Error::InvalidParams(format!(
                "Attribute {} is an array. Retrieve a primitive instead.",
                attribute
            ))),
        },
        Err(e) => Err(e),
    }
}

fn if_operation(
    call: TemplateOperationCall,
    context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    let first_param = unwrap_n_params::<1>(&call.parameters)?[0];
    let display_content = match first_param {
        "true" => true,
        "false" => false,
        _ => match attribute_from_context(first_param, context)? {
            Ctx::Leaf(Bool(bool)) => bool,
            Ctx::Leaf(Str(str)) => !str.is_empty(),
            Ctx::Leaf(Num(num)) => num != 0,
            Ctx::Array(arr) => arr.is_empty(),
            res => {
                return Err(Error::InvalidParams(format!(
                    "Expected value resolve to boolean, but found {:?}",
                    res
                )))
            }
        },
    };
    if display_content {
        return Ok(call.children.unwrap_or_default());
    }
    Ok(String::new())
}

/// Iterates over a range from the parameters
/// Expects three parameters: element, "in", range
/// Range needs to be a context array
/// Element is the name of the variable
/// that changes in each iteration to the
/// next value in the range.
/// Example:
/// ```html
/// {* for element in range {
///    <div>{element}</div>
///    }
/// *}
/// ```
fn for_loop_operation(
    call: TemplateOperationCall,
    context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    let param_slice = unwrap_n_params::<3>(&call.parameters)?;
    let (element, range_key) = match param_slice {
        [element, "in", range] => (element, range),
        _ => {
            return Err(Error::InvalidParams(format!(
                "Expected 'for element in range', but got '{:?}'",
                param_slice
            )))
        }
    };
    let range = match attribute_from_context(range_key, context)? {
        Ctx::Array(arr) => arr,
        res => {
            return Err(Error::InvalidParams(format!(
                "Range has to be a context array. Got {:?}",
                res
            )))
        }
    };
    let children = match call.children {
        Some(children) => children,
        None => {
            return Err(Error::InvalidParams(
                "A for loop needs to have children to iterate.".to_string(),
            ))
        }
    };
    let mut new_context = context.clone();
    let mut iterated_content = String::new();
    for item in range.iter() {
        new_context.insert(element.to_string(), (*item).clone());
        iterated_content.push_str(
            render_html(
                children.clone(),
                Some(new_context.to_owned()),
                &RenderOptions::empty(),
            )
            .unwrap()
            .as_str(),
        );
    }
    Ok(iterated_content)
}

/// Loads an html file to include in a template.
/// Use slots to add html to specific parts of the template.
/// If there are no slots, the component children, if any,
/// will use the default slot.
///
/// Context can be given to the component. If it is,
/// other context data is removed.
///
/// ```html
/// <!-- page.html -->
/// {* component file.html data=object.attribute {
///     {* slot default {
///        <div>Default content</div>
///     } *}
///     {* slot something_else {
///        <div>Default content</div>
///     } *}
/// } *}
/// <!-- my_component.html -->
/// <div>
///     {* slot default *}
/// </div>
/// ```
///
fn component_operation(
    call: TemplateOperationCall,
    context: &ContextMap,
    options: &RenderOptions,
) -> WebResult<String> {
    let parameters = call.parameters;
    let file_path = parameters.first();
    let file_path = match file_path {
        Some(file_path) => file_path,
        None => return Err(Error::InvalidParams("File path not specified".to_string())),
    };
    if !file_path.ends_with(".html")
        && !file_path.ends_with(".svg")
        && !file_path.ends_with(".js")
        && !file_path.ends_with(".css")
    {
        return Err(Error::InvalidParams("Invalid file path".to_string()));
    }
    let mut new_context = context.clone();
    if parameters.len() > 1 {
        new_context.clear();
        parameters
            .iter()
            .skip(1) // Skip file path
            .map(|p| p.split('=').collect::<Vec<_>>())
            .for_each(|p| {
                if p.len() != 2 {
                    return;
                }
                let item = attribute_from_context(p[1], context);
                new_context.insert(p[0].to_string(), item.unwrap());
            });
    }

    if let Some(children) = call.children {
        new_context.insert(INSIDE_COMPONENT_OP_ID.to_string(), Ctx::Leaf(Bool(true)));
        let mut content_w_slots = children.clone();
        let mut slot_operations = 0;
        while let Some(op) = template_operation(&content_w_slots) {
            if let Some(operation_call) = operation_params_and_children(&op.content) {
                if &operation_call.name == "slot" {
                    let slot_name = unwrap_n_params::<1>(&operation_call.parameters)?[0];
                    new_context.insert(
                        slot_name.to_string(),
                        Ctx::Slot(Str(operation_call.children.unwrap_or(String::new()))),
                    );
                    slot_operations += 1;
                }
            }
            content_w_slots.replace_range(op.from..op.to, "");
        }
        if slot_operations == 0 {
            new_context.insert("default".to_string(), Ctx::Slot(Str(children.to_string())));
        }
    }

    let rendered = load_template(file_path, Some(new_context), options)?;

    Ok(rendered)
}

/// Retrives html to include in context
/// This slot operation is handled when rendering
/// a component. A component operation includes the
/// slot html in the context. If a component excludes
/// a slot. It renderes nothing.
/// ```html
/// <!-- page.html -->
/// {* component file.html {
///     {* slot default {
///        <div>Default content</div>
///     } *}
///     {* slot something_else {
///        <div>Default content</div>
///     } *}
/// } *}
/// <!-- my_component.html -->
/// <div>
///     {* slot default *}
/// </div>
/// ```
///
fn slot(
    call: TemplateOperationCall,
    context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    let slot_name = unwrap_n_params::<1>(&call.parameters)?[0];
    if let Some(Ctx::Leaf(Bool(inside_component))) = context.get(INSIDE_COMPONENT_OP_ID) {
        if !inside_component {
            return Err(Error::InvalidParams(
                "Slot function is not loaded from component".to_string(),
            ));
        }
    }
    let content_to_include = match context.get(slot_name) {
        Some(Ctx::Slot(a)) => a.to_string(),
        _ => String::new(),
    };
    Ok(content_to_include)
}

/// Add a comment to the html to not render
/// ```html
/// {* comment Any information to not render *}
/// ```
fn comment_operation(
    _call: TemplateOperationCall,
    _context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    Ok(String::new())
}

/// Returns the entire context
/// Useful for debugging to find what information can be retrieved
/// ```html
/// {* dbg *} <!-- Prints out the entire context -->
/// {* dbg nested.attribute *} <!-- Retrieve attribute in context -->
/// ```
fn dbg_operation(
    call: TemplateOperationCall,
    context: &ContextMap,
    _options: &RenderOptions,
) -> WebResult<String> {
    let mut dbg = String::from("{ ");

    let optional_attr = unwrap_n_params::<1>(&call.parameters).ok();
    if let Some(attribute) = optional_attr {
        let gotten_attr = attribute_from_context(attribute[0], context)?;
        dbg.push_str(&gotten_attr.to_string());
    } else {
        for (k, v) in context.iter() {
            let entry_str = format!("{k}: {val_str}, ", val_str = &v.to_string());
            dbg.push_str(&entry_str);
        }
    }

    dbg.push_str(" }");
    Ok(dbg)
}