use crate::jsonrpc::JsonRpcId;
use super::{JsonRpcMethod, JsonRpcParams, JsonRpcRequest, JsonRpcVersion};
use serde::ser::{SerializeStruct, Serializer};
use serde::Serialize;
use std::fmt::Debug;
#[derive(Debug)]
pub struct OdooApiContainer<T>
where
T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
{
pub(crate) inner: T,
}
impl<T> Serialize for OdooApiContainer<T>
where
T: OdooApiMethod + JsonRpcParams<Container<T> = Self>,
{
fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("args", 3)?;
let (service, method) = self.inner.describe();
state.serialize_field("service", service)?;
state.serialize_field("method", method)?;
state.serialize_field("args", &self.inner)?;
state.end()
}
}
pub trait OdooApiMethod
where
Self: Sized + Debug + Serialize + JsonRpcParams<Container<Self> = OdooApiContainer<Self>>,
Self::Container<Self>: Debug + Serialize,
{
fn describe(&self) -> (&'static str, &'static str);
fn endpoint(&self) -> &'static str;
fn _build(self, id: JsonRpcId) -> JsonRpcRequest<Self> {
JsonRpcRequest {
jsonrpc: JsonRpcVersion::V2,
method: JsonRpcMethod::Call,
id,
params: OdooApiContainer { inner: self },
}
}
}