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