Skip to main content

osp_cli/cli/rows/
row.rs

1use crate::core::row::Row;
2
3pub(crate) struct RowBuilder(Row);
4
5impl RowBuilder {
6    pub(crate) fn new() -> Self {
7        Self(Row::new())
8    }
9
10    pub(crate) fn insert<K, V>(&mut self, key: K, value: V) -> &mut Self
11    where
12        K: Into<String>,
13        V: Into<serde_json::Value>,
14    {
15        self.0.insert(key.into(), value.into());
16        self
17    }
18
19    pub(crate) fn build(self) -> Row {
20        self.0
21    }
22}
23
24#[macro_export]
25/// Builds a [`Row`](crate::core::row::Row) from literal key/value pairs.
26///
27/// This is the terse path for fixed row literals in command/render code.
28///
29/// # Examples
30///
31/// ```
32/// use osp_cli::row;
33/// use serde_json::json;
34///
35/// let row = row! {
36///     "id" => 7,
37///     "name" => "alice",
38/// };
39///
40/// assert_eq!(row.get("id"), Some(&json!(7)));
41/// assert_eq!(row.get("name"), Some(&json!("alice")));
42/// ```
43macro_rules! row {
44    ($($key:expr => $value:expr),* $(,)?) => {{
45        let mut row = $crate::core::row::Row::new();
46        $(row.insert(($key).into(), ($value).into());)*
47        row
48    }};
49}