Skip to main content

sk_core/
macros.rs

1pub use std::collections::BTreeMap;
2
3// Generate labels for a k8s object, using klabel!("label1" = "value1", "label2" = "value2") syntax
4#[macro_export]
5macro_rules! klabel {
6    ($($key:expr=>$val:expr),+$(,)?) => {
7        Some(BTreeMap::from([$(($key.to_string(), $val.to_string())),+]))
8    };
9}
10
11#[macro_export]
12macro_rules! klabel_insert {
13    ($obj:ident, $($key:tt=>$val:tt),+$(,)?) => {
14        $($obj.labels_mut().insert($key.to_string(), $val.to_string()));+
15    };
16}
17
18// Implement PartialEq and PartialOrd for comparisons between an object and a reference to that
19// object
20macro_rules! partial_ord_eq_ref {
21    ($type:ty) => {
22        impl<'a> PartialEq<&'a $type> for $type {
23            fn eq(&self, other: &&'a $type) -> bool {
24                self == *other
25            }
26        }
27
28        impl<'a> PartialEq<$type> for &'a $type {
29            fn eq(&self, other: &$type) -> bool {
30                *self == other
31            }
32        }
33
34        impl<'a> PartialOrd<&'a $type> for $type {
35            fn partial_cmp(&self, other: &&'a $type) -> Option<std::cmp::Ordering> {
36                self.partial_cmp(*other)
37            }
38        }
39
40        impl<'a> PartialOrd<$type> for &'a $type {
41            fn partial_cmp(&self, other: &$type) -> Option<std::cmp::Ordering> {
42                (*self).partial_cmp(other)
43            }
44        }
45    };
46}
47
48pub(crate) use partial_ord_eq_ref;
49pub use {
50    klabel,
51    klabel_insert,
52};