attributes.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. use crate::core::OperationTransform;
  2. use crate::errors::OTError;
  3. use serde::{Deserialize, Serialize};
  4. use std::collections::HashMap;
  5. pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
  6. #[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
  7. pub struct NodeAttributes(AttributeMap);
  8. impl Default for NodeAttributes {
  9. fn default() -> Self {
  10. Self::new()
  11. }
  12. }
  13. impl std::ops::Deref for NodeAttributes {
  14. type Target = AttributeMap;
  15. fn deref(&self) -> &Self::Target {
  16. &self.0
  17. }
  18. }
  19. impl std::ops::DerefMut for NodeAttributes {
  20. fn deref_mut(&mut self) -> &mut Self::Target {
  21. &mut self.0
  22. }
  23. }
  24. impl NodeAttributes {
  25. pub fn new() -> NodeAttributes {
  26. NodeAttributes(HashMap::new())
  27. }
  28. pub fn from_value(attribute_map: AttributeMap) -> Self {
  29. Self(attribute_map)
  30. }
  31. pub fn to_inner(&self) -> AttributeMap {
  32. self.0.clone()
  33. }
  34. pub fn insert<K: ToString, V: Into<AttributeValue>>(&mut self, key: K, value: V) {
  35. self.0.insert(key.to_string(), value.into());
  36. }
  37. pub fn delete(&mut self, key: &AttributeKey) {
  38. self.insert(key.clone(), AttributeValue(None));
  39. }
  40. }
  41. impl OperationTransform for NodeAttributes {
  42. fn compose(&self, other: &Self) -> Result<Self, OTError>
  43. where
  44. Self: Sized,
  45. {
  46. let mut attributes = self.clone();
  47. attributes.0.extend(other.clone().0);
  48. Ok(attributes)
  49. }
  50. fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
  51. where
  52. Self: Sized,
  53. {
  54. let a = self.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
  55. if !other.contains_key(k) {
  56. new_attributes.insert(k.clone(), v.clone());
  57. }
  58. new_attributes
  59. });
  60. let b = other.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
  61. if !self.contains_key(k) {
  62. new_attributes.insert(k.clone(), v.clone());
  63. }
  64. new_attributes
  65. });
  66. Ok((a, b))
  67. }
  68. fn invert(&self, other: &Self) -> Self {
  69. let base_inverted = other.iter().fold(NodeAttributes::new(), |mut attributes, (k, v)| {
  70. if other.get(k) != self.get(k) && self.contains_key(k) {
  71. attributes.insert(k.clone(), v.clone());
  72. }
  73. attributes
  74. });
  75. self.iter().fold(base_inverted, |mut attributes, (k, _)| {
  76. if other.get(k) != self.get(k) && !other.contains_key(k) {
  77. attributes.delete(k);
  78. }
  79. attributes
  80. })
  81. }
  82. }
  83. pub type AttributeKey = String;
  84. #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
  85. pub struct AttributeValue(pub Option<String>);
  86. impl std::convert::From<&usize> for AttributeValue {
  87. fn from(val: &usize) -> Self {
  88. AttributeValue::from(*val)
  89. }
  90. }
  91. impl std::convert::From<usize> for AttributeValue {
  92. fn from(val: usize) -> Self {
  93. if val > 0_usize {
  94. AttributeValue(Some(format!("{}", val)))
  95. } else {
  96. AttributeValue(None)
  97. }
  98. }
  99. }
  100. impl std::convert::From<&str> for AttributeValue {
  101. fn from(val: &str) -> Self {
  102. val.to_owned().into()
  103. }
  104. }
  105. impl std::convert::From<String> for AttributeValue {
  106. fn from(val: String) -> Self {
  107. if val.is_empty() {
  108. AttributeValue(None)
  109. } else {
  110. AttributeValue(Some(val))
  111. }
  112. }
  113. }
  114. impl std::convert::From<&bool> for AttributeValue {
  115. fn from(val: &bool) -> Self {
  116. AttributeValue::from(*val)
  117. }
  118. }
  119. impl std::convert::From<bool> for AttributeValue {
  120. fn from(val: bool) -> Self {
  121. let val = match val {
  122. true => Some("true".to_owned()),
  123. false => None,
  124. };
  125. AttributeValue(val)
  126. }
  127. }