1use std::{
5 fmt,
6 fmt::{Display, Formatter},
7 num::ParseIntError,
8 str::FromStr,
9 time::Duration,
10};
11
12use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
13
14#[repr(transparent)]
15#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
16pub struct CommitVersion(pub u64);
17
18impl FromStr for CommitVersion {
19 type Err = ParseIntError;
20
21 fn from_str(s: &str) -> Result<Self, Self::Err> {
22 Ok(CommitVersion(u64::from_str(s)?))
23 }
24}
25
26impl Display for CommitVersion {
27 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28 Display::fmt(&self.0, f)
29 }
30}
31
32impl PartialEq<i32> for CommitVersion {
33 fn eq(&self, other: &i32) -> bool {
34 self.0 == *other as u64
35 }
36}
37
38impl PartialEq<CommitVersion> for i32 {
39 fn eq(&self, other: &CommitVersion) -> bool {
40 *self as u64 == other.0
41 }
42}
43
44impl PartialEq<u64> for CommitVersion {
45 fn eq(&self, other: &u64) -> bool {
46 self.0.eq(other)
47 }
48}
49
50impl From<CommitVersion> for u64 {
51 fn from(value: CommitVersion) -> Self {
52 value.0
53 }
54}
55
56impl From<i32> for CommitVersion {
57 fn from(value: i32) -> Self {
58 Self(value as u64)
59 }
60}
61
62impl From<u64> for CommitVersion {
63 fn from(value: u64) -> Self {
64 Self(value)
65 }
66}
67
68impl Serialize for CommitVersion {
69 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
70 where
71 S: Serializer,
72 {
73 serializer.serialize_u64(self.0)
74 }
75}
76
77impl<'de> Deserialize<'de> for CommitVersion {
78 fn deserialize<D>(deserializer: D) -> Result<CommitVersion, D::Error>
79 where
80 D: Deserializer<'de>,
81 {
82 struct U64Visitor;
83
84 impl Visitor<'_> for U64Visitor {
85 type Value = CommitVersion;
86
87 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
88 formatter.write_str("an unsigned 64-bit number")
89 }
90
91 fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E> {
92 Ok(CommitVersion(value))
93 }
94 }
95
96 deserializer.deserialize_u64(U64Visitor)
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Default)]
101pub enum JoinType {
102 Inner,
103 #[default]
104 Left,
105}
106
107#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
108pub enum IndexType {
109 #[default]
110 Index,
111 Unique,
112 Primary,
113}
114
115#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
116pub enum WindowSize {
117 Duration(Duration),
118 Count(u64),
119}
120
121impl WindowSize {
122 pub fn is_count(&self) -> bool {
123 matches!(self, WindowSize::Count(_))
124 }
125
126 pub fn as_duration(&self) -> Option<Duration> {
127 match self {
128 WindowSize::Duration(d) => Some(*d),
129 _ => None,
130 }
131 }
132
133 pub fn as_count(&self) -> Option<u64> {
134 match self {
135 WindowSize::Count(c) => Some(*c),
136 _ => None,
137 }
138 }
139}
140
141#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
142pub enum WindowKind {
143 Tumbling {
144 size: WindowSize,
145 },
146
147 Sliding {
148 size: WindowSize,
149 slide: WindowSize,
150 },
151
152 Rolling {
153 size: WindowSize,
154 },
155
156 Session {
157 gap: Duration,
158 },
159}
160
161impl WindowKind {
162 pub fn size(&self) -> Option<&WindowSize> {
163 match self {
164 WindowKind::Tumbling {
165 size,
166 } => Some(size),
167 WindowKind::Sliding {
168 size,
169 ..
170 } => Some(size),
171 WindowKind::Rolling {
172 size,
173 } => Some(size),
174 WindowKind::Session {
175 ..
176 } => None,
177 }
178 }
179}