attributes.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. use crate::core::OperationTransform;
  2. use crate::errors::OTError;
  3. use serde::{Deserialize, Serialize};
  4. use serde_repr::*;
  5. use std::collections::HashMap;
  6. pub type AttributeMap = HashMap<AttributeKey, AttributeValue>;
  7. #[derive(Default, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
  8. pub struct NodeAttributes(AttributeMap);
  9. impl std::ops::Deref for NodeAttributes {
  10. type Target = AttributeMap;
  11. fn deref(&self) -> &Self::Target {
  12. &self.0
  13. }
  14. }
  15. impl std::ops::DerefMut for NodeAttributes {
  16. fn deref_mut(&mut self) -> &mut Self::Target {
  17. &mut self.0
  18. }
  19. }
  20. impl NodeAttributes {
  21. pub fn new() -> NodeAttributes {
  22. NodeAttributes(HashMap::new())
  23. }
  24. pub fn from_value(attribute_map: AttributeMap) -> Self {
  25. Self(attribute_map)
  26. }
  27. pub fn insert<K: ToString, V: Into<AttributeValue>>(&mut self, key: K, value: V) {
  28. self.0.insert(key.to_string(), value.into());
  29. }
  30. pub fn is_empty(&self) -> bool {
  31. self.0.is_empty()
  32. }
  33. pub fn delete<K: ToString>(&mut self, key: K) {
  34. self.insert(key.to_string(), AttributeValue::empty());
  35. }
  36. }
  37. impl OperationTransform for NodeAttributes {
  38. fn compose(&self, other: &Self) -> Result<Self, OTError>
  39. where
  40. Self: Sized,
  41. {
  42. let mut attributes = self.clone();
  43. attributes.0.extend(other.clone().0);
  44. Ok(attributes)
  45. }
  46. fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
  47. where
  48. Self: Sized,
  49. {
  50. let a = self.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
  51. if !other.contains_key(k) {
  52. new_attributes.insert(k.clone(), v.clone());
  53. }
  54. new_attributes
  55. });
  56. let b = other.iter().fold(NodeAttributes::new(), |mut new_attributes, (k, v)| {
  57. if !self.contains_key(k) {
  58. new_attributes.insert(k.clone(), v.clone());
  59. }
  60. new_attributes
  61. });
  62. Ok((a, b))
  63. }
  64. fn invert(&self, other: &Self) -> Self {
  65. let base_inverted = other.iter().fold(NodeAttributes::new(), |mut attributes, (k, v)| {
  66. if other.get(k) != self.get(k) && self.contains_key(k) {
  67. attributes.insert(k.clone(), v.clone());
  68. }
  69. attributes
  70. });
  71. self.iter().fold(base_inverted, |mut attributes, (k, _)| {
  72. if other.get(k) != self.get(k) && !other.contains_key(k) {
  73. attributes.delete(k);
  74. }
  75. attributes
  76. })
  77. }
  78. }
  79. pub type AttributeKey = String;
  80. #[derive(Eq, PartialEq, Hash, Debug, Clone, Serialize_repr, Deserialize_repr)]
  81. #[repr(u8)]
  82. pub enum ValueType {
  83. IntType = 0,
  84. FloatType = 1,
  85. StrType = 2,
  86. BoolType = 3,
  87. }
  88. #[derive(Debug, Clone, PartialEq, Eq, Hash)]
  89. pub struct AttributeValue {
  90. pub ty: ValueType,
  91. pub value: Option<String>,
  92. }
  93. impl AttributeValue {
  94. pub fn empty() -> Self {
  95. Self {
  96. ty: ValueType::StrType,
  97. value: None,
  98. }
  99. }
  100. pub fn from_int(val: usize) -> Self {
  101. Self {
  102. ty: ValueType::IntType,
  103. value: Some(val.to_string()),
  104. }
  105. }
  106. pub fn from_float(val: f64) -> Self {
  107. Self {
  108. ty: ValueType::FloatType,
  109. value: Some(val.to_string()),
  110. }
  111. }
  112. pub fn from_bool(val: bool) -> Self {
  113. Self {
  114. ty: ValueType::BoolType,
  115. value: Some(val.to_string()),
  116. }
  117. }
  118. pub fn from_str(s: &str) -> Self {
  119. let value = if s.is_empty() { None } else { Some(s.to_string()) };
  120. Self {
  121. ty: ValueType::StrType,
  122. value,
  123. }
  124. }
  125. pub fn int_value(&self) -> Option<i64> {
  126. let value = self.value.as_ref()?;
  127. Some(value.parse::<i64>().unwrap_or(0))
  128. }
  129. pub fn bool_value(&self) -> Option<bool> {
  130. let value = self.value.as_ref()?;
  131. Some(value.parse::<bool>().unwrap_or(false))
  132. }
  133. pub fn str_value(&self) -> Option<String> {
  134. self.value.clone()
  135. }
  136. pub fn float_value(&self) -> Option<f64> {
  137. let value = self.value.as_ref()?;
  138. Some(value.parse::<f64>().unwrap_or(0.0))
  139. }
  140. }
  141. impl std::convert::From<bool> for AttributeValue {
  142. fn from(value: bool) -> Self {
  143. AttributeValue::from_bool(value)
  144. }
  145. }
  146. pub struct NodeAttributeBuilder {
  147. attributes: NodeAttributes,
  148. }
  149. impl NodeAttributeBuilder {
  150. pub fn new() -> Self {
  151. Self {
  152. attributes: NodeAttributes::default(),
  153. }
  154. }
  155. pub fn insert<K: ToString, V: Into<AttributeValue>>(mut self, key: K, value: V) -> Self {
  156. self.attributes.insert(key, value);
  157. self
  158. }
  159. pub fn delete<K: ToString>(mut self, key: K) -> Self {
  160. self.attributes.delete(key);
  161. self
  162. }
  163. pub fn build(self) -> NodeAttributes {
  164. self.attributes
  165. }
  166. }