1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! Helper macros for the `odoo_api` crate
//!
//! See the [`odoo_api`](https://crates.io/crates/odoo-api) crate.

use proc_macro::TokenStream;

mod common;
mod error;
mod odoo_api;
mod odoo_web;
mod serialize_tuple;

use common::{parse_result, ItemStructNamed};
use error::{Error, Result};
use syn::{parse_macro_input, AttributeArgs};

/// Implement traits for an "API" method struct
///
/// The input should be a struct with named field. Zero-sized structs and generics
/// are supported.
///
/// Arguments:
///  - Service: The Odoo "service" for this method
///  - Method: The method name
///  - Auth: Whether authentication is required, optional, or ignored
///
/// For example, consider the following:
/// ```ignore
/// // service: "object"
/// // method: "execute"
/// // auth: "yes"
/// #[derive(Debug, Serialize)]
/// #[odoo_api("object", "execute", "yes")]
/// struct Execute {
///     database: String,
///     uid: OdooId,
///     password: String,
///
///     model: String,
///     method: String,
///     args: Vec<Value>
/// }
/// ```
///
/// Then the following impls will be generated:
/// ```ignore
/// // The `Execute` is able to be used as JSON-RPC `params`
/// impl JsonRpcParams for Execute {
///     // Set the container; in this case, the container will use `describe` below
///     // to implement a custom Serialize
///     type Container<T> = OdooApiContainer<Self>;
///     type Response = ExecuteResponse;
///
///     fn build(self) -> JsonRpcRequest<Self> { self._build() }
/// }
///
/// // Which JSON-RPC service and method does `Execute` belong to?
/// impl OdooApiMethod for Execute {
///     fn describe(&self) -> (&'static str, &'static str) {
///         ("object", "execute")
///     }
/// }
///
/// // Implement a method for this struct, so users can write `client.execute(...)`
/// // Note that we specified "yes" for auth, so this impl is bound to `Authed`
/// // clients only
/// impl<I: RequestImpl> OdooClient<Authed, I> {
///     pub fn execute(&self, model: &str, method: &str, args: Vec<Value>) -> OdooRequest<Execute, I> {
///         let execute = Execute {
///             // Auth info is pulled from the Client
///             database: self.auth.database.clone(),
///             uid: self.auth.uid,
///             password: self.auth.password.clone(),
///
///             // Strings are passed as &str then converted to Strings
///             model: model.into(),
///             method: method.into(),
///             args
///         };
///
///         // Finally, build the request
///         self.build_request(
///             execute,
///             &self.url_jsonrpc
///         )
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn odoo_api(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as AttributeArgs);
    let input = parse_macro_input!(input as ItemStructNamed);

    parse_result(odoo_api::odoo_api(args, input))
}

#[proc_macro_attribute]
pub fn odoo_web(args: TokenStream, input: TokenStream) -> TokenStream {
    let args = parse_macro_input!(args as AttributeArgs);
    let input = parse_macro_input!(input as ItemStructNamed);

    parse_result(odoo_web::odoo_web(args, input))
}

#[proc_macro_derive(SerializeTuple)]
pub fn serialize_tuple(input: TokenStream) -> TokenStream {
    parse_result(serialize_tuple::serialize_tuple(input))
}