Skip to main content

floria_plugin_sdk/data/expression/call/
call.rs

1use super::super::super::super::{dispatch_bindings::*, host, *};
2
3use std::{cmp::*, fmt, hash::*};
4
5impl CallResource {
6    /// Into call.
7    pub fn into_call(self) -> Call {
8        self.into_inner()
9    }
10
11    /// Get call.
12    pub fn call(&self) -> &Call {
13        self.get()
14    }
15
16    /// Get call.
17    pub fn call_mut(&mut self) -> &mut Call {
18        self.get_mut()
19    }
20}
21
22//
23// Call
24//
25
26/// Call.
27#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
28pub struct Call {
29    /// Plugin name.
30    pub plugin: String,
31
32    /// Function name.
33    pub function: String,
34
35    /// Arguments.
36    pub arguments: Vec<Expression>,
37
38    /// Kind.
39    pub kind: CallKind,
40}
41
42impl Call {
43    /// Dispatch.
44    pub fn dispatch(&self, call_site: &CallSite) -> DispatchResult {
45        let (name, dispatcher) = registered_dispatcher_plugin()?;
46        if self.plugin == name {
47            dispatcher(self.function.clone(), self.arguments.clone(), call_site.clone())
48        } else {
49            host::evaluate_expression(self.clone().into(), call_site.clone())
50        }
51    }
52}
53
54impl GuestCallResource for Call {
55    fn new(plugin: String, function: String, arguments: Vec<Expression>, kind: CallKind) -> Self {
56        Self { plugin, function, arguments, kind }
57    }
58
59    fn inner(&self) -> (String, String, Vec<Expression>, CallKind) {
60        (self.plugin.clone(), self.function.clone(), self.arguments.clone(), self.kind)
61    }
62}
63
64impl fmt::Display for Call {
65    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66        match self.kind {
67            CallKind::Eager => write!(formatter, "*")?,
68            CallKind::Lazy => write!(formatter, "&")?,
69            _ => {}
70        }
71
72        write!(formatter, "{}:{}(", self.plugin, self.function)?;
73
74        let mut iterator = self.arguments.iter().peekable();
75        while let Some(argument) = iterator.next() {
76            fmt::Display::fmt(argument, formatter)?;
77            if iterator.peek().is_some() {
78                write!(formatter, ",")?;
79            }
80        }
81
82        write!(formatter, ")")
83    }
84}