Skip to content

Commit

Permalink
Introduction of attributes!
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Apr 7, 2024
1 parent cd6a6ca commit 2654c8e
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 268 deletions.
87 changes: 53 additions & 34 deletions src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ use std::{
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;

#[macro_export]
macro_rules! attributes {
($($k:expr => $v:expr),* $(,)?) => {
{
AttributesMap::from([$(($k, $v.into()),)*])
}
};
}

pub use attributes;

/// Attributes of an operation
///
/// These include any formating attribute or companion data associated with
Expand Down Expand Up @@ -79,26 +90,26 @@ impl AttributesMap {
/// # Example
///
/// ```
/// use quill_delta_rs::attributes::AttributesMap;
/// use quill_delta_rs::attributes::{attributes, AttributesMap};
/// use serde_json::Value;
///
/// let mut a = AttributesMap::from([
/// ("keyA", Value::from("a")),
/// ("keyANull", Value::Null)
/// ]);
/// let mut b = AttributesMap::from([
/// ("keyA", Value::from("ab")),
/// ("keyB", Value::from("b")),
/// ("keyBNull", Value::Null)
/// ]);
/// let mut a = attributes!(
/// "keyA" => "a",
/// "keyANull" => Value::Null
/// );
/// let mut b = attributes!(
/// "keyA" => "ab",
/// "keyB" => "b",
/// "keyBNull" => Value::Null
/// );
/// let composed = AttributesMap::diff(a, b);
/// assert_eq!(
/// Some(AttributesMap::from([
/// ("keyA", Value::from("ab")),
/// ("keyB", Value::from("b")),
/// ("keyBNull", Value::Null),
/// ("keyANull", Value::Null),
/// ])),
/// Some(attributes!(
/// "keyA" => "ab",
/// "keyB" => "b",
/// "keyBNull" => Value::Null,
/// "keyANull" => Value::Null,
/// )),
/// composed
/// );
/// ```
Expand Down Expand Up @@ -133,13 +144,13 @@ impl AttributesMap {
/// # Example
///
/// ```
/// use quill_delta_rs::attributes::AttributesMap;
/// use quill_delta_rs::attributes::{attributes, AttributesMap};
/// use serde_json::Value;
///
/// let attributes = AttributesMap::from([("bold", Value::Bool(true))]);
/// let base = AttributesMap::from([("italic", Value::Bool(true))]);
/// let attributes = attributes!("bold" => true);
/// let base = attributes!("italic" => true);
/// assert_eq!(
/// AttributesMap::from([("bold", Value::Null)]),
/// attributes!("bold" => Value::Null),
/// AttributesMap::invert(attributes, base)
/// );
/// ```
Expand All @@ -164,21 +175,21 @@ impl AttributesMap {
/// #Example
///
/// ```
/// use quill_delta_rs::attributes::AttributesMap;
/// use quill_delta_rs::attributes::{attributes, AttributesMap};
/// use serde_json::Value;
///
/// let left = AttributesMap::from([
/// ("bold", Value::Bool(true)),
/// ("color", Value::from("red")),
/// ("font", Value::Null),
/// ]);
/// let right = AttributesMap::from([
/// ("color", Value::from("blue")),
/// ("font", Value::from("serif")),
/// ("italic", Value::Bool(true)),
/// ]);
/// let left = attributes!(
/// "bold" => true,
/// "color" => "red",
/// "font"=> Value::Null,
/// );
/// let right = attributes!(
/// "color" => "blue",
/// "font" => "serif",
/// "italic" => true,
/// );
/// assert_eq!(
/// Some(AttributesMap::from([("italic", Value::Bool(true))])),
/// Some(attributes!("italic" => true)),
/// AttributesMap::transform(left, right, true)
/// )
/// ```
Expand Down Expand Up @@ -343,10 +354,10 @@ impl<'a, const N: usize> From<[(&'a str, Value); N]> for AttributesMap {
/// # Examples
///
/// ```
/// use quill_delta_rs::attributes::AttributesMap;
/// use quill_delta_rs::attributes::{attributes,AttributesMap};
/// use serde_json::Value;
///
/// let map1 = AttributesMap::from([("key1", Value::from(2)), ("key2", Value::from(4))]);
/// let map1 = attributes!("key1" => 2, "key2" => 4);
/// let map2: AttributesMap = [("key1", Value::from(2)), ("key2", Value::from(4))].into();
/// assert_eq!(map1, map2);
/// ```
Expand Down Expand Up @@ -656,4 +667,12 @@ mod tests {
AttributesMap::transform(left, right, false)
)
}

#[test]
fn attributes_macro() {
assert_eq!(
AttributesMap::from([("bold", true.into()), ("italic", Value::Null)]),
attributes!("bold" => true, "italic" => None::<&str>)
);
}
}
Loading

0 comments on commit 2654c8e

Please sign in to comment.