use super::super::super::super::{dispatch_bindings::*, utils::*, *};
use std::{cmp::*, fmt, hash::*};
impl CallResource {
pub fn call(&self) -> &Call {
self.get()
}
pub fn to_call_mut(&mut self) -> &mut Call {
self.get_mut()
}
}
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Call {
pub plugin: String,
pub function: String,
pub arguments: Vec<Expression>,
pub kind: CallKind,
}
impl Call {
pub fn dispatch(&self, call_site: &CallSite) -> Result<Option<Expression>, String> {
let (name, dispatcher) = registered_dispatcher_plugin()?;
if self.plugin == name {
dispatcher(self.function.clone(), self.arguments.clone(), call_site.clone())
} else {
Err(format!("other plugin: |error|{}|", escape_depiction_markup(&self.plugin)))
}
}
}
impl GuestCallResource for Call {
fn new(plugin: String, function: String, arguments: Vec<Expression>, kind: CallKind) -> Self {
Self { plugin, function, arguments, kind }
}
fn get(&self) -> (String, String, Vec<Expression>, CallKind) {
(self.plugin.clone(), self.function.clone(), self.arguments.clone(), self.kind)
}
}
impl fmt::Display for Call {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.kind {
CallKind::Eager => write!(formatter, "*")?,
CallKind::Lazy => write!(formatter, "&")?,
_ => {}
}
write!(formatter, "{}:{}(", self.plugin, self.function)?;
let mut iterator = self.arguments.iter().peekable();
while let Some(argument) = iterator.next() {
fmt::Display::fmt(argument, formatter)?;
if iterator.peek().is_some() {
write!(formatter, ",")?;
}
}
write!(formatter, ")")
}
}