specta 2.0.0-rc.21

Easily export your Rust types to other languages
Documentation
use std::future::Future;

use crate::{datatype::FunctionResultVariant, Type, TypeCollection};

/// Implemented by types that can be returned from a function annotated with
/// [`specta`](crate::specta).
pub trait FunctionResult<TMarker> {
    /// Gets the type of the result as a [`DataType`].
    fn to_datatype(type_map: &mut TypeCollection) -> FunctionResultVariant;
}

#[doc(hidden)]
pub enum FunctionValueMarker {}
impl<T: Type> FunctionResult<FunctionValueMarker> for T {
    fn to_datatype(type_map: &mut TypeCollection) -> FunctionResultVariant {
        FunctionResultVariant::Value(T::reference(type_map, &[]).inner)
    }
}

#[doc(hidden)]
pub enum FunctionResultMarker {}
impl<T: Type, E: Type> FunctionResult<FunctionResultMarker> for Result<T, E> {
    fn to_datatype(type_map: &mut TypeCollection) -> FunctionResultVariant {
        FunctionResultVariant::Result(
            T::reference(type_map, &[]).inner,
            E::reference(type_map, &[]).inner,
        )
    }
}

#[doc(hidden)]
pub enum FunctionFutureMarker {}
impl<F> FunctionResult<FunctionFutureMarker> for F
where
    F: Future,
    F::Output: Type,
{
    fn to_datatype(type_map: &mut TypeCollection) -> FunctionResultVariant {
        FunctionResultVariant::Value(F::Output::reference(type_map, &[]).inner)
    }
}

#[doc(hidden)]
pub enum FunctionResultFutureMarker {}
impl<F, T, E> FunctionResult<FunctionResultFutureMarker> for F
where
    F: Future<Output = Result<T, E>>,
    T: Type,
    E: Type,
{
    fn to_datatype(type_map: &mut TypeCollection) -> FunctionResultVariant {
        FunctionResultVariant::Result(
            T::reference(type_map, &[]).inner,
            E::reference(type_map, &[]).inner,
        )
    }
}