use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use super::{FunctionCall, FunctionType};
pub mod fn_macro;
pub trait FunctionCallable {
fn schema_type() -> FunctionType
where
Self: Sized;
fn fn_schema() -> FunctionCall
where
Self: Sized,
{
panic!(
"FunctionCallable::fn_schema() is only meaningful on \
#[derive(FunctionCall)] types — not on primitives or containers."
)
}
}
impl FunctionCallable for String {
fn schema_type() -> FunctionType {
FunctionType::String
}
}
impl FunctionCallable for &'static str {
fn schema_type() -> FunctionType {
FunctionType::String
}
}
impl FunctionCallable for Cow<'static, str> {
fn schema_type() -> FunctionType {
FunctionType::String
}
}
impl FunctionCallable for bool {
fn schema_type() -> FunctionType {
FunctionType::Boolean
}
}
macro_rules! impl_numeric {
($($t:ty),+) => {
$(
impl FunctionCallable for $t {
fn schema_type() -> FunctionType { FunctionType::Number }
}
)+
};
}
impl_numeric!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64);
impl<T: FunctionCallable> FunctionCallable for Vec<T> {
fn schema_type() -> FunctionType {
FunctionType::Array(Box::new(T::schema_type()))
}
}
impl<T: FunctionCallable, const N: usize> FunctionCallable for [T; N] {
fn schema_type() -> FunctionType {
FunctionType::Array(Box::new(T::schema_type()))
}
}
impl<T: FunctionCallable> FunctionCallable for Option<T> {
fn schema_type() -> FunctionType {
FunctionType::Option(Box::new(T::schema_type()))
}
}
impl<V: FunctionCallable> FunctionCallable for HashMap<String, V> {
fn schema_type() -> FunctionType {
FunctionType::Map(Box::new(V::schema_type()))
}
}
impl<V: FunctionCallable> FunctionCallable for BTreeMap<String, V> {
fn schema_type() -> FunctionType {
FunctionType::Map(Box::new(V::schema_type()))
}
}