rwf 0.2.1

Framework for building web applications in the Rust programming language
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Language expression. When evaluated, produces a single value.
use super::{
    super::lexer::{Token, TokenWithContext, Tokenize, Value},
    super::Context,
    super::Error,
    Op, Term,
};

use std::iter::{Iterator, Peekable};

/// An expression, like `5 == 6` or `logged_in == false`,
/// which when evaluated produces a single value, e.g. `true`.
#[derive(Debug, Clone)]
pub enum Expression {
    // Standard `5 + 6`-style expression.
    // It's recursive, so you can have something like `(5 + 6) / (1 - 5)`.
    Binary {
        left: Box<Expression>,
        op: Op,
        right: Box<Expression>,
    },

    Unary {
        op: Op,
        operand: Box<Expression>,
    },

    // Base case for recursive expression parsing, which evaluates to the value
    // of the term, e.g. `5` evalutes to `5` or `variable_name` evalutes to whatever
    // the variable is set to in the context.
    Term {
        term: Term,
    },

    // A list of expressions, e.g.
    // `[1, 2, variable, "hello world"]`
    //
    // The list is dynamically evaluated at runtime, so it can contain variables
    // and constants, as long as the variable is in scope.
    List {
        terms: Vec<Expression>,
    },

    // Call a function on a value/expression.
    Function {
        term: Box<Expression>,
        name: Box<Expression>,
        args: Vec<Expression>,
    },

    Interpreter,
}

impl Expression {
    /// Create new constant expression (term).
    pub fn constant(value: Value) -> Self {
        Self::Term {
            term: Term::constant(value),
        }
    }

    /// Create new variable expression (term).
    pub fn variable(variable: String) -> Self {
        Self::Term {
            term: Term::variable(variable),
        }
    }

    /// Evaluate the expression to a value given the context.
    pub fn evaluate(&self, context: &Context) -> Result<Value, Error> {
        match self {
            Expression::Term { term } => match term.evaluate(context) {
                Ok(value) => Ok(value),
                Err(Error::UndefinedVariable(name)) => {
                    let value = Value::Interpreter;
                    match value.call(term.name(), &[], context) {
                        Ok(value) => Ok(value),
                        Err(Error::UnknownMethod(_, _)) => {
                            return Err(Error::UndefinedVariable(name))
                        }
                        Err(err) => return Err(err),
                    }
                }
                Err(err) => return Err(err),
            },

            Expression::Binary { left, op, right } => {
                let left = left.evaluate(context)?;
                let right = right.evaluate(context)?;
                op.evaluate_binary(&left, &right)
            }

            Expression::Unary { op, operand } => {
                let operand = operand.evaluate(context)?;
                op.evaluate_unary(&operand)
            }

            Expression::List { terms } => {
                let mut list = vec![];
                for term in terms {
                    list.push(term.evaluate(context)?);
                }
                Ok(Value::List(list))
            }

            Expression::Function { term, name, args } => {
                let value = term.evaluate(context)?;
                let name = match name.evaluate(context)? {
                    Value::String(name) => name,
                    name => {
                        return Err(Error::Runtime(format!(
                            "function name should be a string, got {} instead",
                            name
                        )))
                    }
                };

                // Allow to pass undefined variables to a function.
                // Typically that's not great, but the purpose of this function
                // is to catch such cases and replace with a default value (presumably defined).
                let allow_undefined = name == "default" && value == Value::Interpreter;

                let args = args
                    .iter()
                    .map(|arg| match arg.evaluate(context) {
                        Ok(value) => Ok(value),
                        Err(Error::UndefinedVariable(v)) => {
                            if allow_undefined {
                                Ok(Value::Null)
                            } else {
                                Err(Error::UndefinedVariable(v))
                            }
                        }

                        Err(e) => Err(e),
                    })
                    .collect::<Result<Vec<Value>, Error>>()?;

                Ok(value.call(&name, &args, context)?)
            }

            Expression::Interpreter => Ok(Value::Interpreter),
        }
    }

    fn term(iter: &mut Peekable<impl Iterator<Item = TokenWithContext>>) -> Result<Self, Error> {
        let next = iter.next().ok_or(Error::Eof("term next"))?;
        let term = match next.token() {
            Token::Not => {
                let term = Self::term(iter)?;
                Expression::Unary {
                    op: Op::Not,
                    operand: Box::new(term),
                }
            }

            Token::Minus => {
                let term = Self::term(iter)?;
                Expression::Unary {
                    op: Op::Sub,
                    operand: Box::new(term),
                }
            }

            Token::Plus => {
                let term = Self::term(iter)?;
                Expression::Unary {
                    op: Op::Add,
                    operand: Box::new(term),
                }
            }

            Token::RoundBracketStart => {
                let mut count = 1;
                let mut expr = vec![];

                // Count the brackets. The term is finished when the number of opening brackets
                // match the number of closing brackets.
                while count > 0 {
                    let next = iter.peek().ok_or(Error::Eof("round bracket start"))?;

                    match next.token() {
                        Token::RoundBracketStart => {
                            count += 1;
                            expr.push(iter.next().ok_or(Error::Eof("round bracket start 1"))?);
                        }
                        Token::RoundBracketEnd => {
                            count -= 1;

                            // If it's not the closing bracket, push it in for recursive parsing later.
                            if count > 0 {
                                expr.push(iter.next().ok_or(Error::Eof("round bracket end"))?);
                            } else {
                                // Drop the closing bracket, the expression is over.
                                let _ = iter.next().ok_or(Error::Eof("round bracket end 1"))?;
                            }
                        }
                        Token::BlockEnd => return Err(Error::ExpressionSyntax(next.clone())),

                        _ => {
                            expr.push(iter.next().ok_or(Error::Eof("round bracket push"))?);
                        }
                    }
                }

                Self::accessor(Self::parse(&mut expr.into_iter().peekable())?, iter)?
            }

            token => {
                let expr = match token {
                    Token::Variable(name) => {
                        if let Some(next) = iter.peek() {
                            match next.token() {
                                Token::RoundBracketStart => {
                                    Self::function(&name, Expression::Interpreter, iter)?
                                }

                                _ => Self::variable(name),
                            }
                        } else {
                            Self::variable(name)
                        }
                    }
                    Token::Value(value) => Self::constant(value),
                    Token::SquareBracketStart => {
                        let mut terms = vec![];

                        loop {
                            let term = Self::term(iter)?;
                            terms.push(term);
                            let next = iter.next().ok_or(Error::Eof("term token"))?;
                            match next.token() {
                                Token::SquareBracketEnd => break,
                                Token::Comma => continue,
                                _ => return Err(Error::ExpressionSyntax(next)),
                            }
                        }

                        Expression::List { terms }
                    }

                    _ => return Err(Error::ExpressionSyntax(next)),
                };

                Self::accessor(expr, iter)?
            }
        };

        Ok(term)
    }

    // TODO: Support parsing function arguments between parenthesis, e.g.:
    // `my_function((another_func(1, 2)), "hello")`
    fn function(
        name: &str,
        expr: Self,
        iter: &mut Peekable<impl Iterator<Item = TokenWithContext>>,
    ) -> Result<Self, Error> {
        let arg = iter.peek().map(|t| t.token());
        let args = match arg {
            Some(Token::RoundBracketStart) => {
                let mut buffer = vec![];
                let mut args = vec![];
                let _ = iter.next().ok_or(Error::Eof("function args start"));

                loop {
                    let next = match iter.next() {
                        Some(next) => next,
                        None => break,
                    };

                    match next.token() {
                        Token::RoundBracketEnd => {
                            if !buffer.is_empty() {
                                args.push(Self::parse(
                                    &mut std::mem::take(&mut buffer).into_iter().peekable(),
                                )?);
                            }
                            break;
                        }
                        Token::Comma => {
                            args.push(Self::parse(
                                &mut std::mem::take(&mut buffer).into_iter().peekable(),
                            )?);
                        }

                        // TODO: handle function calls inside function calls
                        _ => {
                            buffer.push(next);
                        }
                    }
                }

                args
            }

            _ => {
                vec![]
            }
        };

        Ok(Expression::Function {
            term: Box::new(expr),
            name: Box::new(Expression::constant(Value::String(name.to_string()))),
            args,
        })
    }

    fn accessor(
        mut expr: Self,
        iter: &mut Peekable<impl Iterator<Item = TokenWithContext>>,
    ) -> Result<Self, Error> {
        loop {
            let accessor = iter.peek().map(|t| t.token());

            expr = match accessor {
                Some(Token::Dot) => {
                    let _ = iter.next().ok_or(Error::Eof("accessor dot"))?;
                    let name = iter.next().ok_or(Error::Eof("accessor name"))?;
                    match name.token() {
                        Token::Variable(name) => Self::function(&name, expr, iter)?,
                        Token::Value(Value::Integer(n)) => Expression::Function {
                            term: Box::new(expr),
                            name: Box::new(Expression::constant(Value::String(n.to_string()))),
                            args: vec![],
                        },
                        _ => return Err(Error::ExpressionSyntax(name.clone())),
                    }
                }

                Some(Token::SquareBracketStart) => {
                    let _ = iter.next().ok_or(Error::Eof("accessor bracket"))?;
                    let name = Self::parse(iter)?;
                    let next = iter.next().ok_or(Error::Eof("expected closing bracket"))?;
                    match next.token() {
                        Token::SquareBracketEnd => (),
                        token => return Err(Error::WrongToken(next, token)),
                    }
                    Expression::Function {
                        term: Box::new(expr),
                        name: Box::new(name),
                        args: vec![],
                    }
                }

                Some(_) | None => return Ok(expr),
            };
        }
    }

    /// Recursively parse the expression.
    ///
    /// Consumes language tokens automatically.
    pub fn parse(
        iter: &mut Peekable<impl Iterator<Item = TokenWithContext>>,
    ) -> Result<Self, Error> {
        // Get the left term, if one exists.
        // TODO: support unary operations.
        let left = Self::term(iter)?;

        // Check if we have another operator.
        let next = match iter.peek() {
            Some(next) => next,
            None => return Ok(left),
        };

        match Op::from_token(next.token()) {
            Some(op) => {
                // We have another operator. Consume the token.
                let _ = iter.next().ok_or(Error::Eof("parse token"))?;

                // Get the right term. This is a binary op.
                let right = Self::term(iter)?;

                // Check if there's another operator.
                let next = iter.peek();

                match next.map(|t| t.token()) {
                    // Expression is over.
                    Some(Token::BlockEnd) | None => Ok(Expression::Binary {
                        left: Box::new(left),
                        op,
                        right: Box::new(right),
                    }),

                    // We have an operator.
                    Some(token) => match Op::from_token(token) {
                        Some(second_op) => {
                            // Consume the token.
                            let _ = iter.next().ok_or(Error::Eof("parse second op"))?;

                            // Get the right term.
                            let right2 = Expression::parse(iter)?;

                            // Check operator precendence.
                            if second_op < op {
                                let expr = Expression::Binary {
                                    left: Box::new(right),
                                    right: Box::new(right2),
                                    op: second_op,
                                };

                                Ok(Expression::Binary {
                                    left: Box::new(left),
                                    right: Box::new(expr),
                                    op,
                                })
                            } else {
                                let left = Expression::Binary {
                                    left: Box::new(left),
                                    right: Box::new(right),
                                    op,
                                };

                                Ok(Expression::Binary {
                                    left: Box::new(left),
                                    right: Box::new(right2),
                                    op: second_op,
                                })
                            }
                        }

                        // Not an op, so syntax error.
                        None => Err(Error::ExpressionSyntax(next.unwrap().clone())),
                    },
                }
            }

            None => return Ok(left),
        }
    }
}

/// Evalute an expression given a context.
pub trait Evaluate {
    /// Evaluate expression given the context.
    fn evaluate(&self, context: &Context) -> Result<Value, Error>;

    /// Evaluate expression with an empty context.
    fn evaluate_default(&self) -> Result<Value, Error> {
        self.evaluate(&Context::default())
    }
}

impl Evaluate for &str {
    fn evaluate(&self, context: &Context) -> Result<Value, Error> {
        let tokens = self.tokenize()?[1..].to_vec(); // Skip code block start.
        let expr = Expression::parse(&mut tokens.into_iter().peekable())?;
        expr.evaluate(context)
    }
}

impl Evaluate for String {
    fn evaluate(&self, context: &Context) -> Result<Value, Error> {
        self.as_str().evaluate(context)
    }
}

#[cfg(test)]
mod test {
    use super::super::super::Context;
    use super::*;
    use std::collections::HashMap;

    #[test]
    fn test_if_const() -> Result<(), Error> {
        assert_eq!(r#"<% 1 == 2 %>"#.evaluate_default()?, Value::Boolean(false));
        assert_eq!(r#"<% 1 == 1 %>"#.evaluate_default()?, Value::Boolean(true));

        Ok(())
    }

    #[test]
    fn test_list() -> Result<(), Error> {
        let mut context = Context::default();
        context.set("variable", "world")?;
        context.set("list", vec![1, 2, 3])?;

        let t1 = r#"<% [1, 2, "hello", 3.13, variable] %>"#.evaluate(&context)?;
        assert_eq!(
            t1,
            Value::List(vec![
                Value::Integer(1),
                Value::Integer(2),
                Value::String("hello".into()),
                Value::Float(3.13),
                Value::String("world".into()),
            ])
        );

        let t2 = "<% [1, 2, 3] * 2 %>".evaluate_default()?;
        assert_eq!(
            t2,
            Value::List(vec![
                Value::Integer(1),
                Value::Integer(2),
                Value::Integer(3),
                Value::Integer(1),
                Value::Integer(2),
                Value::Integer(3),
            ])
        );

        assert_eq!("<% [1, 2, 3].0 %>".evaluate_default()?, Value::Integer(1));

        assert_eq!(r#"<% list.0 %>"#.evaluate(&context)?, Value::Integer(1));

        let nested = r#"<% [[1, 2, 3], "hello", ["world", "is", "pretty"]]"#.evaluate_default()?;
        assert_eq!(
            nested,
            Value::List(vec![
                Value::List(vec![
                    Value::Integer(1),
                    Value::Integer(2),
                    Value::Integer(3),
                ]),
                Value::String("hello".into()),
                Value::List(vec![
                    Value::String("world".into()),
                    Value::String("is".into()),
                    Value::String("pretty".into()),
                ])
            ])
        );

        Ok(())
    }

    #[test]
    fn test_hash() -> Result<(), Error> {
        let mut context = Context::default();
        context.set(
            "hash",
            Value::Hash(HashMap::from([("key".to_string(), Value::Integer(5))])),
        )?;
        context.set("name", Value::String("key".into()))?;

        let t1 = "<% (hash.key * 2.5) - 2.5 %>".evaluate(&context)?;
        assert_eq!(t1, Value::Float(10.0));

        let t1 = "<% hash[name] %>".evaluate(&context)?;
        assert_eq!(t1, Value::Integer(5));

        Ok(())
    }

    #[test]
    fn test_call() -> Result<(), Error> {
        assert_eq!(
            "<% 54.5.to_string %>".evaluate_default()?,
            Value::String("54.5".into())
        );
        assert_eq!(
            r#"<% "one".upcase %>"#.evaluate_default()?,
            Value::String("ONE".into())
        );
        assert_eq!(
            r#"<% ("one" + "two" + "three" ).upcase %>"#.evaluate_default()?,
            Value::String("ONETWOTHREE".into())
        );
        assert_eq!(
            r#"<% " one".upcase.trim %>"#.evaluate_default()?,
            Value::String("ONE".into())
        );

        let mut context = Context::default();
        context.set("variable", "hello")?;

        assert_eq!(
            "<% (((variable.upcase * 2) * 1).downcase).upcase %>".evaluate(&context)?,
            Value::String("HELLOHELLO".into())
        );

        Ok(())
    }

    #[test]
    fn test_math() -> Result<(), Error> {
        assert_eq!("<% 2 * 0.5 %>".evaluate_default()?, Value::Float(1.0));
        assert_eq!(
            "<% 2 * 2 + 3 * 5 %>".evaluate_default()?,
            Value::Integer(19)
        );
        assert_eq!("<% 1.5 * 3 + 25 %>".evaluate_default()?, Value::Float(29.5));
        assert_eq!(
            "<% (1 + 5) * 0.25 %>".evaluate_default()?,
            Value::Float(1.5)
        );

        Ok(())
    }

    #[test]
    fn test_unary() -> Result<(), Error> {
        assert_eq!(
            "<% !false == true && true %>".evaluate_default()?,
            Value::Boolean(true)
        );

        let mut context = Context::default();
        context.set("variable", 5)?;
        assert_eq!(
            "<% -variable * 1.5 %>".evaluate(&context)?,
            Value::Float(-7.5)
        );
        Ok(())
    }

    #[test]
    fn test_parenthesis() -> Result<(), Error> {
        let t1 = "<% ((1 + 2) + (-1 - -1)) * 5 + (25 - 5) %>";
        assert_eq!(t1.evaluate_default()?, Value::Integer(35));

        Ok(())
    }

    #[test]
    fn test_syntactic_sugar() -> Result<(), Error> {
        let t1 = r#"<% "copy" * 3 + 1 * "copy" %>"#.evaluate_default()?;
        assert_eq!(t1, Value::String("copycopycopycopy".into()));

        let t2 = r#"<% "where is the love" - "where is the " %>"#.evaluate_default()?;
        assert_eq!(t2, Value::String("love".into()));

        Ok(())
    }

    #[test]
    fn test_global_function() -> Result<(), Error> {
        let t1 = r#"<% encrypt_number(1) %>"#;
        let result = t1.evaluate_default()?;

        let mut context = Context::new();
        context.set("n", result)?;

        let result = "<% decrypt_number(n) %>".evaluate(&context)?;

        assert_eq!(result.to_string(), String::from("1"));

        Ok(())
    }

    #[test]
    #[ignore]
    fn test_list_flatten() -> Result<(), Error> {
        let mut context = Context::default();
        context["test"] = Value::List(vec![
            Value::List(vec![Value::Integer(1), Value::Integer(2)]),
            Value::List(vec![Value::Integer(3), Value::Integer(4)]),
        ]);

        let t1 = "<% test.flatten %>".evaluate(&context)?;
        assert_eq!(
            t1,
            Value::List(vec![
                Value::Integer(1),
                Value::Integer(2),
                Value::Integer(3),
                Value::Integer(4)
            ])
        );

        Ok(())
    }

    #[test]
    fn test_default() -> Result<(), Error> {
        let t1 = r#"<% default(some_var, "val") %>"#;
        let v = t1.evaluate_default()?;
        assert_eq!(v, Value::String("val".into()));

        let t1 = r#"<% default("set", "val") %>"#;
        let v = t1.evaluate_default()?;
        assert_eq!(v, Value::String("set".into()));

        let t1 = r#"<% default(var, "val") %>"#;
        let mut context = Context::default();
        context.set("var", "set")?;
        let v = t1.evaluate(&context)?;
        assert_eq!(v, Value::String("set".into()));

        Ok(())
    }

    #[test]
    fn test_replace() -> Result<(), Error> {
        let t1 = r#"<% "Some string".sub("string", 1234) %>"#.evaluate_default()?;
        assert_eq!(t1, Value::String("Some 1234".into()));

        let t1 = "<% 1234.replace(3, 5) %>".evaluate_default()?;
        assert_eq!(t1, Value::Integer(1254));

        Ok(())
    }
}