Skip to content

Commit b208e18

Browse files
committed
Use serde_with
1 parent 5b157b1 commit b208e18

File tree

5 files changed

+160
-105
lines changed

5 files changed

+160
-105
lines changed

Cargo.lock

Lines changed: 144 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const_format = "0.2.34"
2323
futures-lite = { version = "2.6.0", default-features = false, features = ["alloc"] }
2424
rustc-hash = { version = "2.1", default-features = false }
2525
thiserror = { version = "2", default-features = false }
26+
serde_with = { version = "3.14.0", default-features = false, features = ["alloc", "macros"] }
2627

2728
[dependencies.uuid]
2829
version = "1.4.1"

crates/core/src/sync/interface.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ use alloc::rc::Rc;
77
use alloc::sync::Arc;
88
use alloc::{string::String, vec::Vec};
99
use serde::{Deserialize, Serialize};
10+
use serde_with::{serde_as, DisplayFromStr};
1011
use sqlite::{ResultCode, Value};
1112
use sqlite_nostd::{self as sqlite, ColumnType};
1213
use sqlite_nostd::{Connection, Context};
1314

1415
use crate::error::PowerSyncError;
1516
use crate::schema::Schema;
1617
use crate::state::DatabaseState;
17-
use crate::util::serialize_i64_to_string;
1818

1919
use super::streaming_sync::SyncClient;
2020
use super::sync_status::DownloadSyncStatus;
@@ -115,13 +115,14 @@ pub struct StreamSubscriptionRequest {
115115
pub subscriptions: Vec<RequestedStreamSubscription>,
116116
}
117117

118+
#[serde_as]
118119
#[derive(Serialize)]
119120
pub struct RequestedStreamSubscription {
120121
/// The name of the sync stream to subscribe to.
121122
pub stream: String,
122123
/// Parameters to make available in the stream's definition.
123124
pub parameters: Box<serde_json::value::RawValue>,
124-
#[serde(serialize_with = "serialize_i64_to_string")]
125+
#[serde_as(as = "DisplayFromStr")]
125126
pub client_id: i64,
126127
}
127128

crates/core/src/sync/line.rs

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use alloc::borrow::Cow;
22
use alloc::vec::Vec;
33
use serde::de::{IgnoredAny, VariantAccess, Visitor};
44
use serde::Deserialize;
5-
6-
use crate::util::{deserialize_optional_string_to_i64, deserialize_string_to_i64};
5+
use serde_with::{serde_as, DisplayFromStr};
76

87
use super::bucket_priority::BucketPriority;
98
use super::Checksum;
@@ -73,27 +72,28 @@ impl<'de> Deserialize<'de> for SyncLine<'de> {
7372
}
7473
}
7574

75+
#[serde_as]
7676
#[derive(Deserialize, Debug)]
7777
pub struct Checkpoint<'a> {
78-
#[serde(deserialize_with = "deserialize_string_to_i64")]
78+
#[serde_as(as = "DisplayFromStr")]
7979
pub last_op_id: i64,
8080
#[serde(default)]
81-
#[serde(deserialize_with = "deserialize_optional_string_to_i64")]
81+
#[serde_as(as = "Option<DisplayFromStr>")]
8282
pub write_checkpoint: Option<i64>,
8383
#[serde(borrow)]
8484
pub buckets: Vec<BucketChecksum<'a>>,
8585
}
8686

87+
#[serde_as]
8788
#[derive(Deserialize, Debug)]
8889
pub struct CheckpointDiff<'a> {
89-
#[serde(deserialize_with = "deserialize_string_to_i64")]
90+
#[serde_as(as = "DisplayFromStr")]
9091
pub last_op_id: i64,
9192
#[serde(borrow)]
9293
pub updated_buckets: Vec<BucketChecksum<'a>>,
9394
#[serde(borrow)]
9495
pub removed_buckets: Vec<SyncLineStr<'a>>,
95-
#[serde(default)]
96-
#[serde(deserialize_with = "deserialize_optional_string_to_i64")]
96+
#[serde_as(as = "Option<DisplayFromStr>")]
9797
pub write_checkpoint: Option<i64>,
9898
}
9999

@@ -110,6 +110,7 @@ pub struct CheckpointPartiallyComplete {
110110
pub priority: BucketPriority,
111111
}
112112

113+
#[serde_as]
113114
#[derive(Deserialize, Debug)]
114115
pub struct BucketChecksum<'a> {
115116
#[serde(borrow)]
@@ -119,60 +120,14 @@ pub struct BucketChecksum<'a> {
119120
pub priority: Option<BucketPriority>,
120121
#[serde(default)]
121122
pub count: Option<i64>,
122-
#[serde(
123-
default,
124-
deserialize_with = "BucketChecksum::deserialize_subscriptions"
125-
)]
123+
#[serde_as(as = "Option<Vec<DisplayFromStr>>")]
124+
#[serde(default)]
126125
pub subscriptions: Option<Vec<i64>>,
127126
// #[serde(default)]
128127
// #[serde(deserialize_with = "deserialize_optional_string_to_i64")]
129128
// pub last_op_id: Option<i64>,
130129
}
131130

132-
impl BucketChecksum<'_> {
133-
fn deserialize_subscriptions<'de, D: serde::Deserializer<'de>>(
134-
deserializer: D,
135-
) -> Result<Option<Vec<i64>>, D::Error> {
136-
struct MyVisitor;
137-
138-
impl<'de> Visitor<'de> for MyVisitor {
139-
type Value = Option<Vec<i64>>;
140-
141-
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
142-
write!(formatter, "optional list of subscriptions")
143-
}
144-
145-
fn visit_none<E>(self) -> Result<Self::Value, E>
146-
where
147-
E: serde::de::Error,
148-
{
149-
Ok(None)
150-
}
151-
152-
fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
153-
where
154-
D: serde::Deserializer<'de>,
155-
{
156-
deserializer.deserialize_seq(self)
157-
}
158-
159-
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
160-
where
161-
A: serde::de::SeqAccess<'de>,
162-
{
163-
let mut result: Vec<i64> = Vec::new();
164-
while let Some(element) = seq.next_element::<&'de str>()? {
165-
result.push(element.parse::<i64>().map_err(serde::de::Error::custom)?);
166-
}
167-
168-
Ok(Some(result))
169-
}
170-
}
171-
172-
deserializer.deserialize_option(MyVisitor)
173-
}
174-
}
175-
176131
#[derive(Deserialize, Debug)]
177132
pub struct DataLine<'a> {
178133
#[serde(borrow)]
@@ -186,10 +141,11 @@ pub struct DataLine<'a> {
186141
// pub next_after: Option<SyncLineStr<'a>>,
187142
}
188143

144+
#[serde_as]
189145
#[derive(Deserialize, Debug)]
190146
pub struct OplogEntry<'a> {
191147
pub checksum: Checksum,
192-
#[serde(deserialize_with = "deserialize_string_to_i64")]
148+
#[serde_as(as = "DisplayFromStr")]
193149
pub op_id: i64,
194150
pub op: OpType,
195151
#[serde(default, borrow)]

0 commit comments

Comments
 (0)