1use anyhow::Result;
2use core::fmt::Debug;
3use schemars::JsonSchema;
4use serde::{de::DeserializeOwned, Serialize};
5use serde_json::Value;
6use std::future::Future;
7
8use crate::http::Request;
9
10pub trait Cog: Sized + Send {
12 type Request: DeserializeOwned + JsonSchema + Send;
13 type Response: CogResponse + Debug + JsonSchema;
14
15 fn setup() -> impl Future<Output = Result<Self>> + Send;
21
22 fn predict(&self, input: Self::Request) -> Result<Self::Response>;
28}
29
30pub trait CogResponse: Send {
32 fn into_response(self, request: Request) -> impl Future<Output = Result<Value>> + Send;
34}
35
36impl<T: Serialize + Send + 'static> CogResponse for T {
37 async fn into_response(self, _: Request) -> Result<Value> {
38 Ok(tokio::task::spawn_blocking(move || serde_json::to_value(self)).await??)
40 }
41}