Skip to content

Commit

Permalink
make FunctionArg public
Browse files Browse the repository at this point in the history
  • Loading branch information
oscartbeaumont committed Apr 2, 2024
1 parent bbbd1c3 commit 2ae7766
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 68 deletions.
57 changes: 15 additions & 42 deletions src/functions/arg.rs
@@ -1,46 +1,19 @@
mod private {
use crate::{DataType, Type, TypeMap};

/// Implemented by types that can be used as an argument in a function annotated with
/// [`specta`](crate::specta).
pub trait FunctionArg<TMarker> {
/// Gets the type of an argument as a [`DataType`].
///
/// Some argument types should be ignored (eg Tauri command State),
/// so the value is optional.
fn to_datatype(type_map: &mut TypeMap) -> Option<DataType>;
}
use crate::{DataType, Type, TypeMap};

/// Implemented by types that can be used as an argument in a function annotated with
/// [`specta`](crate::specta).
pub trait FunctionArg<TMarker> {
/// Gets the type of an argument as a [`DataType`].
///
/// Some argument types should be ignored (eg Tauri command State),
/// so the value is optional.
fn to_datatype(type_map: &mut TypeMap) -> Option<DataType>;
}

pub enum FunctionArgMarker {}
pub enum FunctionArgMarker {}

impl<T: Type> FunctionArg<FunctionArgMarker> for T {
fn to_datatype(type_map: &mut TypeMap) -> Option<DataType> {
Some(T::reference(type_map, &[]).inner)
}
impl<T: Type> FunctionArg<FunctionArgMarker> for T {
fn to_datatype(type_map: &mut TypeMap) -> Option<DataType> {
Some(T::reference(type_map, &[]).inner)
}

#[cfg(feature = "tauri")]
const _: () = {
pub enum FunctionArgTauriMarker {}

impl<R: tauri::Runtime> FunctionArg<FunctionArgTauriMarker> for tauri::Window<R> {
fn to_datatype(_: &mut TypeMap) -> Option<DataType> {
None
}
}

impl<'r, T: Send + Sync + 'static> FunctionArg<FunctionArgTauriMarker> for tauri::State<'r, T> {
fn to_datatype(_: &mut TypeMap) -> Option<DataType> {
None
}
}

impl<R: tauri::Runtime> FunctionArg<FunctionArgTauriMarker> for tauri::AppHandle<R> {
fn to_datatype(_: &mut TypeMap) -> Option<DataType> {
None
}
}
};
}

pub(crate) use private::FunctionArg;
3 changes: 2 additions & 1 deletion src/functions/mod.rs
Expand Up @@ -3,7 +3,8 @@ mod result;

use std::borrow::Cow;

pub(crate) use arg::*;
pub use arg::FunctionArg;

pub(crate) use result::*;

use crate::*;
Expand Down
46 changes: 21 additions & 25 deletions src/functions/result.rs
@@ -1,32 +1,28 @@
mod private {
use std::future::Future;
use std::future::Future;

use crate::{DataType, Type, TypeMap};
use crate::{DataType, Type, TypeMap};

/// 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 TypeMap) -> DataType;
}
/// 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 TypeMap) -> DataType;
}

pub enum FunctionResultMarker {}
impl<T: Type> FunctionResult<FunctionResultMarker> for T {
fn to_datatype(type_map: &mut TypeMap) -> DataType {
T::reference(type_map, &[]).inner
}
pub enum FunctionResultMarker {}
impl<T: Type> FunctionResult<FunctionResultMarker> for T {
fn to_datatype(type_map: &mut TypeMap) -> DataType {
T::reference(type_map, &[]).inner
}
}

pub enum FunctionResultFutureMarker {}
impl<F> FunctionResult<FunctionResultFutureMarker> for F
where
F: Future,
F::Output: Type,
{
fn to_datatype(type_map: &mut TypeMap) -> DataType {
F::Output::reference(type_map, &[]).inner
}
pub enum FunctionResultFutureMarker {}
impl<F> FunctionResult<FunctionResultFutureMarker> for F
where
F: Future,
F::Output: Type,
{
fn to_datatype(type_map: &mut TypeMap) -> DataType {
F::Output::reference(type_map, &[]).inner
}
}

pub(crate) use private::FunctionResult;

0 comments on commit 2ae7766

Please sign in to comment.