|
| 1 | +use serde::{de::Visitor, Deserialize}; |
| 2 | +use sqlite_nostd::ResultCode; |
| 3 | + |
| 4 | +use crate::error::SQLiteError; |
| 5 | + |
| 6 | +#[repr(transparent)] |
| 7 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 8 | +pub struct BucketPriority { |
| 9 | + pub number: i32, |
| 10 | +} |
| 11 | + |
| 12 | +impl BucketPriority { |
| 13 | + pub fn may_publish_with_outstanding_uploads(self) -> bool { |
| 14 | + self == BucketPriority::HIGHEST |
| 15 | + } |
| 16 | + |
| 17 | + pub const HIGHEST: BucketPriority = BucketPriority { number: 0 }; |
| 18 | + |
| 19 | + /// A low priority used to represent fully-completed sync operations across all priorities. |
| 20 | + pub const SENTINEL: BucketPriority = BucketPriority { number: i32::MAX }; |
| 21 | +} |
| 22 | + |
| 23 | +impl TryFrom<i32> for BucketPriority { |
| 24 | + type Error = SQLiteError; |
| 25 | + |
| 26 | + fn try_from(value: i32) -> Result<Self, Self::Error> { |
| 27 | + if value < BucketPriority::HIGHEST.number || value == Self::SENTINEL.number { |
| 28 | + return Err(SQLiteError( |
| 29 | + ResultCode::MISUSE, |
| 30 | + Some("Invalid bucket priority".into()), |
| 31 | + )); |
| 32 | + } |
| 33 | + |
| 34 | + return Ok(BucketPriority { number: value }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl Into<i32> for BucketPriority { |
| 39 | + fn into(self) -> i32 { |
| 40 | + self.number |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl PartialOrd<BucketPriority> for BucketPriority { |
| 45 | + fn partial_cmp(&self, other: &BucketPriority) -> Option<core::cmp::Ordering> { |
| 46 | + Some(self.number.partial_cmp(&other.number)?.reverse()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl<'de> Deserialize<'de> for BucketPriority { |
| 51 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 52 | + where |
| 53 | + D: serde::Deserializer<'de>, |
| 54 | + { |
| 55 | + struct PriorityVisitor; |
| 56 | + impl<'de> Visitor<'de> for PriorityVisitor { |
| 57 | + type Value = BucketPriority; |
| 58 | + |
| 59 | + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 60 | + formatter.write_str("a priority as an integer between 0 and 3 (inclusive)") |
| 61 | + } |
| 62 | + |
| 63 | + fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> |
| 64 | + where |
| 65 | + E: serde::de::Error, |
| 66 | + { |
| 67 | + BucketPriority::try_from(v).map_err(|e| E::custom(e.1.unwrap_or_default())) |
| 68 | + } |
| 69 | + |
| 70 | + fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> |
| 71 | + where |
| 72 | + E: serde::de::Error, |
| 73 | + { |
| 74 | + let i: i32 = v.try_into().map_err(|_| E::custom("int too large"))?; |
| 75 | + Self::visit_i32(self, i) |
| 76 | + } |
| 77 | + |
| 78 | + fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> |
| 79 | + where |
| 80 | + E: serde::de::Error, |
| 81 | + { |
| 82 | + let i: i32 = v.try_into().map_err(|_| E::custom("int too large"))?; |
| 83 | + Self::visit_i32(self, i) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + deserializer.deserialize_i32(PriorityVisitor) |
| 88 | + } |
| 89 | +} |
0 commit comments