odoo_api/macros.rs
1//! Internal module to store macro_rules!() macros
2
3// Import Value and Map so the doc comments render properly
4#[allow(unused_imports)]
5use serde_json::{Map, Value};
6
7/// Helper macro to build a [`Vec<Value>`]
8///
9/// This is useful when using any of the API methods that require a `Vec<Value>`,
10/// as [`serde_json`] doesn't have a way to build these.
11///
12/// ## Example:
13/// ```no_run
14/// # #[cfg(not(feature = "types-only"))]
15/// # fn test() -> odoo_api::client::error::Result<()> {
16/// # use serde_json::{json, Value};
17/// # use odoo_api::{jvec, jmap};
18/// # use odoo_api::{OdooClient};
19/// # let client = OdooClient::new_reqwest_blocking("https://demo.odoo.com")?;
20/// # let mut client = client.authenticate_manual("", "", 1, "", None);
21/// // Manually
22/// let mut args = Vec::<Value>::new();
23/// args.push(json!([1, 2, 3]));
24/// args.push(json!(["id", "login"]));
25///
26/// let request = client.execute(
27/// "res.users",
28/// "read",
29/// args,
30/// ).send()?;
31///
32/// // With jvec![]:
33/// let request = client.execute(
34/// "res.users",
35/// "read",
36/// jvec![
37/// [1, 2, 3],
38/// ["id", "login"]
39/// ]
40/// ).send()?;
41/// # Ok(())
42/// # }
43/// ```
44#[macro_export]
45macro_rules! jvec {
46 [$($v:tt),*] => {
47 {
48 let mut vec = ::std::vec::Vec::<serde_json::Value>::new();
49 $(
50 vec.push(::serde_json::json!($v));
51 )*
52 vec
53 }
54 };
55 () => { compiler_error!("")};
56}
57
58/// Helper macro to build a [`Map<String, Value>`]
59///
60/// This is useful when using any of the API methods that require a `Map<String, Value>`,
61/// as [`serde_json`] doesn't have a way to build these.
62///
63/// ## Example:
64/// ```no_run
65/// # #[cfg(not(feature = "types-only"))]
66/// # fn test() -> odoo_api::client::error::Result<()> {
67/// # use serde_json::{json, Value, Map};
68/// # use odoo_api::{jvec, jmap};
69/// # use odoo_api::{OdooClient};
70/// # let client = OdooClient::new_reqwest_blocking("https://demo.odoo.com")?;
71/// # let mut client = client.authenticate_manual("", "", 1, "", None);
72/// // Manually
73/// let mut kwargs = Map::<String, Value>::new();
74/// kwargs.insert("domain".into(), json!([["name", "ilike", "admin"]]));
75/// kwargs.insert("fields".into(), json!(["id", "login"]));
76///
77/// let request = client.execute_kw(
78/// "res.users",
79/// "search_read",
80/// jvec![],
81/// kwargs,
82/// ).send()?;
83///
84/// // With jmap!{}:
85/// let request = client.execute_kw(
86/// "res.users",
87/// "search_read",
88/// jvec![],
89/// jmap!{
90/// "domain": [["name", "ilike", "admin"]],
91/// "fields": ["id", "login"]
92/// }
93/// ).send()?;
94/// # Ok(())
95/// # }
96/// ```
97#[macro_export]
98macro_rules! jmap {
99 {$($k:tt: $v:tt),*} => {
100 {
101 let mut map = ::serde_json::Map::<String, ::serde_json::Value>::new();
102 $(
103 map.insert($k.into(), ::serde_json::json!($v));
104 )*
105 map
106 }
107 };
108 () => { compiler_error!("")};
109}
110
111/// Helper macro to build a [`Vec<String>`]
112///
113/// Quite a few ORM methods take [`Vec<String>`] as an argument. Using the built-in
114/// `vec![]` macro requires that each element is converted into a `String`, which
115/// is very cumbersome.
116///
117/// Using this macro, we can write:
118/// ```
119/// # #[cfg(not(feature = "types-only"))]
120/// # fn test() {
121/// # use odoo_api::svec;
122/// let fields = svec!["string", "literals", "without", "to_string()"];
123/// # }
124/// ```
125#[macro_export]
126macro_rules! svec {
127 [$($v:tt),*] => {
128 {
129 let mut vec = ::std::vec::Vec::<String>::new();
130 $(
131 vec.push($v.to_string());
132 )*
133 vec
134 }
135 };
136 () => { compiler_error!("")};
137}