use facet::Facet;
use super::builder::{BuiltinBuilder, ModuleBuilder, ReturnHint, TypeBuilder, XurType};
use super::{Context, Registry};
use crate::marshal::TypeDesc;
use crate::{RunResult, Value};
use xur_resolve::NativeFnId;
#[derive(Clone, Default)]
pub struct RawSig {
pub params: Vec<RawParam>,
pub ret: Option<TypeDesc>,
pub lsp: Option<XurType>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RawParam {
pub name: &'static str,
pub ty: TypeDesc,
pub optional: bool,
pub rest: bool,
}
impl RawSig {
pub fn new() -> Self {
Self::default()
}
pub fn param(self, name: &'static str, ty: impl Into<TypeDesc>) -> Self {
self.push(name, ty.into(), false, false)
}
pub fn optional(self, name: &'static str, ty: impl Into<TypeDesc>) -> Self {
self.push(name, ty.into(), true, false)
}
pub fn rest(self, name: &'static str, ty: impl Into<TypeDesc>) -> Self {
self.push(name, ty.into(), true, true)
}
pub fn returns(mut self, ty: impl Into<TypeDesc>) -> Self {
self.ret = Some(ty.into());
self
}
pub fn lsp(mut self, ty: XurType) -> Self {
self.lsp = Some(ty);
self
}
fn push(mut self, name: &'static str, ty: TypeDesc, optional: bool, rest: bool) -> Self {
self.params.push(RawParam {
name,
ty,
optional,
rest,
});
self
}
fn names(&self) -> Vec<String> {
self.params.iter().map(|p| p.name.to_string()).collect()
}
}
impl From<&'static str> for TypeDesc {
fn from(name: &'static str) -> Self {
TypeDesc::Name(name)
}
}
impl TypeDesc {
pub fn of<T: Facet<'static>>() -> Self {
TypeDesc::Shape(T::SHAPE)
}
}
impl Registry {
pub(super) fn set_raw_sig(&self, id: NativeFnId, sig: RawSig) {
let hint = match (sig.lsp, &sig.ret) {
(Some(xur), _) => Some(ReturnHint::Xur(xur)),
(None, Some(TypeDesc::Shape(shape))) => Some(ReturnHint::Shape(shape)),
(None, None) => Some(ReturnHint::Xur(XurType::UNIT)),
_ => None,
};
self.with_fn_mut(id, |f| {
f.return_hint = hint;
f.raw_sig = Some(sig);
});
}
}
impl ModuleBuilder<'_> {
pub fn raw_function_sig(
&mut self,
name: &str,
sig: RawSig,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let id = self.add_raw(name, sig.names(), Box::new(f));
self.registry().set_raw_sig(id, sig);
self
}
}
impl<T: Facet<'static> + 'static> TypeBuilder<'_, T> {
pub fn raw_method_sig(
&mut self,
name: &str,
sig: RawSig,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let names = std::iter::once("self".to_string())
.chain(sig.names())
.collect();
let id = self.add_raw(name, names, Box::new(f));
self.registry().set_raw_sig(id, sig);
self
}
pub fn raw_function_sig(
&mut self,
name: &str,
sig: RawSig,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let id = self.add_raw(name, sig.names(), Box::new(f));
self.registry().set_raw_sig(id, sig);
self
}
}
impl BuiltinBuilder<'_> {
pub fn raw_method_sig(
&mut self,
name: &str,
sig: RawSig,
f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
) -> &mut Self {
let id = self.add_raw(name, sig.names(), Box::new(f));
self.registry().set_raw_sig(id, sig);
self
}
}