operation_serde.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. use crate::core::{Attributes, Delta, Operation};
  2. use serde::{
  3. de,
  4. de::{MapAccess, SeqAccess, Visitor},
  5. ser::{SerializeMap, SerializeSeq},
  6. Deserialize,
  7. Deserializer,
  8. Serialize,
  9. Serializer,
  10. };
  11. use std::fmt;
  12. impl Serialize for Operation {
  13. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  14. where
  15. S: Serializer,
  16. {
  17. match self {
  18. Operation::Retain(retain) => retain.serialize(serializer),
  19. Operation::Delete(i) => {
  20. let mut map = serializer.serialize_map(Some(1))?;
  21. map.serialize_entry("delete", i)?;
  22. map.end()
  23. },
  24. Operation::Insert(insert) => insert.serialize(serializer),
  25. }
  26. }
  27. }
  28. impl<'de> Deserialize<'de> for Operation {
  29. fn deserialize<D>(deserializer: D) -> Result<Operation, D::Error>
  30. where
  31. D: Deserializer<'de>,
  32. {
  33. struct OperationVisitor;
  34. impl<'de> Visitor<'de> for OperationVisitor {
  35. type Value = Operation;
  36. fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  37. formatter.write_str("an integer between -2^64 and 2^63 or a string")
  38. }
  39. fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
  40. where
  41. V: MapAccess<'de>,
  42. {
  43. let mut operation = None;
  44. let mut attributes = None;
  45. while let Some(key) = map.next_key()? {
  46. match key {
  47. "delete" => {
  48. if operation.is_some() {
  49. return Err(de::Error::duplicate_field("operation"));
  50. }
  51. operation = Some(Operation::Delete(map.next_value()?));
  52. },
  53. "retain" => {
  54. if operation.is_some() {
  55. return Err(de::Error::duplicate_field("operation"));
  56. }
  57. let i: u64 = map.next_value()?;
  58. operation = Some(Operation::Retain(i.into()));
  59. },
  60. "insert" => {
  61. if operation.is_some() {
  62. return Err(de::Error::duplicate_field("operation"));
  63. }
  64. let i: String = map.next_value()?;
  65. operation = Some(Operation::Insert(i.into()));
  66. },
  67. "attributes" => {
  68. if attributes.is_some() {
  69. return Err(de::Error::duplicate_field("attributes"));
  70. }
  71. let map: Attributes = map.next_value()?;
  72. attributes = Some(map);
  73. },
  74. _ => panic!(),
  75. }
  76. }
  77. match operation {
  78. None => Err(de::Error::missing_field("operation")),
  79. Some(mut operation) => {
  80. operation.set_attributes(attributes.unwrap_or(Attributes::Empty));
  81. Ok(operation)
  82. },
  83. }
  84. }
  85. }
  86. deserializer.deserialize_any(OperationVisitor)
  87. }
  88. }
  89. impl Serialize for Delta {
  90. fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  91. where
  92. S: Serializer,
  93. {
  94. let mut seq = serializer.serialize_seq(Some(self.ops.len()))?;
  95. for op in self.ops.iter() {
  96. seq.serialize_element(op)?;
  97. }
  98. seq.end()
  99. }
  100. }
  101. impl<'de> Deserialize<'de> for Delta {
  102. fn deserialize<D>(deserializer: D) -> Result<Delta, D::Error>
  103. where
  104. D: Deserializer<'de>,
  105. {
  106. struct OperationSeqVisitor;
  107. impl<'de> Visitor<'de> for OperationSeqVisitor {
  108. type Value = Delta;
  109. fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  110. formatter.write_str("a sequence")
  111. }
  112. fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
  113. where
  114. A: SeqAccess<'de>,
  115. {
  116. let mut o = Delta::default();
  117. while let Some(op) = seq.next_element()? {
  118. o.add(op);
  119. }
  120. Ok(o)
  121. }
  122. }
  123. deserializer.deserialize_seq(OperationSeqVisitor)
  124. }
  125. }