revision.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use bytes::Bytes;
  2. use serde::{Deserialize, Serialize};
  3. use std::{convert::TryFrom, fmt::Formatter, ops::RangeInclusive};
  4. #[derive(PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
  5. pub struct Revision {
  6. pub base_rev_id: i64,
  7. pub rev_id: i64,
  8. pub bytes: Vec<u8>,
  9. pub md5: String,
  10. pub object_id: String,
  11. }
  12. impl std::convert::From<Vec<u8>> for Revision {
  13. fn from(data: Vec<u8>) -> Self {
  14. let bytes = Bytes::from(data);
  15. Revision::try_from(bytes).unwrap()
  16. }
  17. }
  18. impl std::convert::TryFrom<Bytes> for Revision {
  19. type Error = serde_json::Error;
  20. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  21. serde_json::from_slice(&bytes)
  22. }
  23. }
  24. impl Revision {
  25. pub fn new<T: Into<String>>(
  26. object_id: &str,
  27. base_rev_id: i64,
  28. rev_id: i64,
  29. bytes: Bytes,
  30. md5: T,
  31. ) -> Revision {
  32. let object_id = object_id.to_owned();
  33. let bytes = bytes.to_vec();
  34. let base_rev_id = base_rev_id;
  35. let rev_id = rev_id;
  36. if base_rev_id != 0 {
  37. debug_assert!(base_rev_id <= rev_id);
  38. }
  39. Self {
  40. base_rev_id,
  41. rev_id,
  42. bytes,
  43. md5: md5.into(),
  44. object_id,
  45. }
  46. }
  47. pub fn is_empty(&self) -> bool {
  48. self.base_rev_id == self.rev_id
  49. }
  50. pub fn pair_rev_id(&self) -> (i64, i64) {
  51. (self.base_rev_id, self.rev_id)
  52. }
  53. pub fn is_initial(&self) -> bool {
  54. self.rev_id == 0
  55. }
  56. pub fn initial_revision(object_id: &str, bytes: Bytes) -> Self {
  57. let md5 = format!("{:x}", md5::compute(&bytes));
  58. Self::new(object_id, 0, 0, bytes, md5)
  59. }
  60. pub fn to_bytes(&self) -> Bytes {
  61. let bytes = serde_json::to_vec(self).unwrap();
  62. Bytes::from(bytes)
  63. }
  64. }
  65. impl std::fmt::Debug for Revision {
  66. fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
  67. f.write_fmt(format_args!("object_id {}, ", self.object_id))?;
  68. f.write_fmt(format_args!("base_rev_id {}, ", self.base_rev_id))?;
  69. f.write_fmt(format_args!("rev_id {}, ", self.rev_id))?;
  70. Ok(())
  71. }
  72. }
  73. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  74. pub struct RevisionRange {
  75. pub start: i64,
  76. pub end: i64,
  77. }
  78. impl std::fmt::Display for RevisionRange {
  79. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
  80. f.write_fmt(format_args!("[{},{}]", self.start, self.end))
  81. }
  82. }
  83. impl RevisionRange {
  84. pub fn len(&self) -> u64 {
  85. debug_assert!(self.end >= self.start);
  86. if self.end >= self.start {
  87. (self.end - self.start + 1) as u64
  88. } else {
  89. 0
  90. }
  91. }
  92. pub fn is_empty(&self) -> bool {
  93. self.end == self.start
  94. }
  95. pub fn iter(&self) -> RangeInclusive<i64> {
  96. // debug_assert!(self.start != self.end);
  97. RangeInclusive::new(self.start, self.end)
  98. }
  99. pub fn to_rev_ids(&self) -> Vec<i64> {
  100. self.iter().collect::<Vec<_>>()
  101. }
  102. }