1
use std::fmt::Debug;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
use crate::schema::ApiName;
6

            
7
/// An API request type.
8
pub trait Api: Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug + 'static {
9
    /// The type that represents an API response. This type will be sent to clients from the server.
10
    type Response: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug;
11
    /// The error type that this  can return.
12
    type Error: ApiError;
13

            
14
    /// Returns the unique name of this api.
15
    fn name() -> ApiName;
16
}
17
/// An Error type that can be used in within an [`Api`] definition.
18
///
19
/// The reason `std::convert::Infallible` can't be used is because `Api`
20
/// errors must be able to be serialized across a network connection. While a
21
/// value will never be present when this is Infallible, the associated type
22
/// still must be declared as Serializable.
23
#[derive(thiserror::Error, Debug, Clone, Serialize, Deserialize)]
24
#[error("an unreachable error")]
25
pub enum Infallible {}
26

            
27
/// An error that can be used within a [`Api`] definition.
28
pub trait ApiError:
29
    std::fmt::Display + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug
30
{
31
}
32

            
33
impl<T> ApiError for T where
34
    T: std::fmt::Display + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + Debug
35
{
36
}
37

            
38
/// The result of executing a custom API call.
39
pub type ApiResult<Api> = Result<<Api as self::Api>::Response, <Api as self::Api>::Error>;