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
use crate::bumpalo::Bump;
use crate::context::Context;
use crate::value::Value;
use async_trait::async_trait;

#[async_trait(?Send)]
pub trait Builtin {
    async fn run<'c>(&mut self, name: &'_ str, ctx: &'_ mut Context<'c>) -> Option<Value<'c>>;
    fn load<'b>(&mut self, name: &str, b: &'b Bump) -> Value<'b>;
    fn print(&mut self, v: Value);
    fn new_line(&mut self);
    async fn wait(&mut self);
}

#[async_trait(?Send)]
impl<'a, B: Builtin> Builtin for &'a mut B {
    #[inline]
    async fn run<'c>(&mut self, name: &'_ str, ctx: &'_ mut Context<'c>) -> Option<Value<'c>> {
        (**self).run(name, ctx).await
    }
    #[inline]
    fn load<'b>(&mut self, name: &str, b: &'b Bump) -> Value<'b> {
        (**self).load(name, b)
    }
    #[inline]
    fn print(&mut self, v: Value) {
        (**self).print(v);
    }
    #[inline]
    fn new_line(&mut self) {
        (**self).new_line();
    }
    #[inline]
    async fn wait(&mut self) {
        (**self).wait().await;
    }
}

#[cfg(test)]
pub struct RecordBuiltin(String);

#[cfg(test)]
impl RecordBuiltin {
    #[inline]
    pub fn new() -> Self {
        Self(String::with_capacity(8196))
    }

    #[inline]
    pub fn text(&self) -> &str {
        &self.0
    }
}

#[cfg(test)]
#[async_trait(?Send)]
impl Builtin for RecordBuiltin {
    #[inline]
    async fn run<'c>(&mut self, name: &'_ str, _ctx: &'_ mut Context<'c>) -> Option<Value<'c>> {
        self.0.push_str(name);
        None
    }
    #[inline]
    fn load<'b>(&mut self, name: &str, _b: &'b Bump) -> Value<'b> {
        use std::fmt::Write;
        write!(self.0, "${}", name).unwrap();
        Value::Int(0)
    }
    #[inline]
    fn print(&mut self, v: Value) {
        use std::fmt::Write;
        write!(self.0, "{}", v).unwrap();
    }
    #[inline]
    fn new_line(&mut self) {
        self.0.push('@');
    }
    #[inline]
    async fn wait(&mut self) {
        self.0.push('#');
    }
}