libmpv_client/macros.rs
1#[cfg(debug_assertions)]
2macro_rules! build_debug_loc {
3 ($var:expr) => {
4 Some(crate::error::DebugLoc {
5 file: file!(),
6 line: line!(),
7 function: {
8 fn f() {}
9 fn type_name_of<T>(_: T) -> &'static str {
10 std::any::type_name::<T>()
11 }
12 type_name_of(f)
13 },
14 variable: Some(stringify!($var)),
15 })
16 };
17}
18
19#[cfg(not(debug_assertions))]
20macro_rules! build_debug_loc {
21 ($var:expr) => {
22 None
23 };
24}
25
26macro_rules! check_null {
27 ($ptr:expr) => {
28 if $ptr.is_null() {
29 return Err($crate::Error::Rust($crate::error::RustError::Pointer(build_debug_loc!($ptr))));
30 }
31 };
32}
33
34#[macro_export]
35/// Construct a [`NodeArray`](crate::NodeArray) from a list of items that implement [`Into<Node>`].
36///
37/// See the [list of traits on `Node`](crate::Node#trait-implementations).
38///
39/// # Example
40/// ```
41///# use libmpv_client::{node_array, Node, NodeArray};
42/// let array_node = node_array!("one", 2, 3.14);
43///
44/// let test_array_node = Node::Array(vec![
45/// Node::String("one".to_string()),
46/// Node::Int64(2),
47/// Node::Double(3.14)
48/// ]);
49///
50/// assert_eq!(array_node, test_array_node);
51/// ```
52macro_rules! node_array {
53 (
54 $($elem:expr),*$(,)?
55 ) => {
56 $crate::Node::Array(vec![$($crate::Node::from($elem),)*])
57 };
58}
59
60#[macro_export]
61/// Construct a [`NodeMap`](crate::NodeMap) from a list of `(Into<String>, Into<Node>)` tuples.
62///
63/// See the [list of traits on `Node`](crate::Node#trait-implementations).
64///
65/// # Example
66/// ```
67///# use std::collections::HashMap;
68///# use libmpv_client::{node_map, Node, NodeMap};
69/// let array_node = node_map! {
70/// ("1", "one"),
71/// ("two", 2),
72/// ("pi", 3.14),
73/// };
74///
75/// let test_array_node = Node::Map(HashMap::from([
76/// ("1".to_string(), Node::String("one".to_string())),
77/// ("two".to_string(), Node::Int64(2)),
78/// ("pi".to_string(), Node::Double(3.14))
79/// ]));
80///
81/// assert_eq!(array_node, test_array_node);
82/// ```
83macro_rules! node_map {
84 (
85 $(($key:expr, $val:expr)),*$(,)?
86 ) => {
87 $crate::Node::Map(std::collections::HashMap::from([$(($key.to_string(), $crate::Node::from($val)),)*]))
88 };
89}