odoo_api/jsonrpc/request/
orm.rs

1use serde::ser::SerializeStruct;
2use serde::{Serialize, Serializer};
3use std::fmt::Debug;
4
5use super::{JsonRpcId, JsonRpcMethod, JsonRpcParams, JsonRpcRequest, JsonRpcVersion};
6
7/// The container type for an Odoo "ORM" request
8///
9/// These functions are essentially just wrappers around
10/// [`Execute`](crate::service::object::Execute)
11/// and [`ExecuteKw`](crate::service::object::ExecuteKw), providing a more
12/// user-friendly interface (and better type checking!)
13///
14/// For more info, see [`super::JsonRpcParams`]
15#[derive(Debug)]
16pub struct OdooOrmContainer<T>
17where
18    T: OdooOrmMethod + JsonRpcParams<Container<T> = Self>,
19{
20    pub(crate) inner: T,
21}
22
23// Custom "man-in-the-middle" serialize impl
24impl<T> Serialize for OdooOrmContainer<T>
25where
26    T: OdooOrmMethod + JsonRpcParams<Container<T> = Self>,
27{
28    fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
29    where
30        S: Serializer,
31    {
32        let mut state = serializer.serialize_struct("args", 3)?;
33        state.serialize_field("service", "object")?;
34        state.serialize_field("method", "execute_kw")?;
35        state.serialize_field("args", &self.inner)?;
36        state.end()
37    }
38}
39
40/// An Odoo "Orm" request type
41pub trait OdooOrmMethod
42where
43    Self: Sized + Debug + Serialize + JsonRpcParams<Container<Self> = OdooOrmContainer<Self>>,
44    Self::Container<Self>: Debug + Serialize,
45{
46    /// Describe the "ORM" method endpoint (e.g., "/web/session/authenticate")
47    fn endpoint(&self) -> &'static str;
48
49    /// Return the model method name (e.g., "read_group" or "create")
50    fn method(&self) -> &'static str;
51
52    /// Build `self` into a full [`JsonRpcRequest`]
53    fn _build(self, id: JsonRpcId) -> JsonRpcRequest<Self> {
54        JsonRpcRequest {
55            jsonrpc: JsonRpcVersion::V2,
56            method: JsonRpcMethod::Call,
57            id,
58            params: OdooOrmContainer { inner: self },
59        }
60    }
61}