Skip to content

Commit 0518768

Browse files
committed
fix: better feature cfg
1 parent fc3ddd1 commit 0518768

File tree

3 files changed

+44
-35
lines changed

3 files changed

+44
-35
lines changed

libsql/src/database.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ cfg_replication_or_sync! {
6464
}
6565
}
6666

67+
}
68+
69+
cfg_sync! {
6770
#[derive(Default)]
6871
pub enum SyncProtocol {
6972
#[default]

libsql/src/database/builder.rs

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ impl Builder<()> {
6767
http_request_callback: None,
6868
namespace: None,
6969
skip_safety_assert: false,
70+
#[cfg(feature = "sync")]
7071
sync_protocol: Default::default(),
7172
},
7273
}
@@ -223,6 +224,7 @@ cfg_replication! {
223224
http_request_callback: Option<crate::util::HttpRequestCallback>,
224225
namespace: Option<String>,
225226
skip_safety_assert: bool,
227+
#[cfg(feature = "sync")]
226228
sync_protocol: super::SyncProtocol,
227229
}
228230

@@ -279,6 +281,7 @@ cfg_replication! {
279281
/// Set the duration at which the replicator will automatically call `sync` in the
280282
/// background. The sync will continue for the duration that the resulted `Database`
281283
/// type is alive for, once it is dropped the background task will get dropped and stop.
284+
#[cfg(feature = "sync")]
282285
pub fn sync_protocol(mut self, protocol: super::SyncProtocol) -> Builder<RemoteReplica> {
283286
self.inner.sync_protocol = protocol;
284287
self
@@ -337,6 +340,7 @@ cfg_replication! {
337340
http_request_callback,
338341
namespace,
339342
skip_safety_assert,
343+
#[cfg(feature = "sync")]
340344
sync_protocol,
341345
} = self.inner;
342346

@@ -353,44 +357,46 @@ cfg_replication! {
353357
crate::util::ConnectorService::new(svc)
354358
};
355359

356-
use super::SyncProtocol;
357-
358-
match sync_protocol {
359-
p @ (SyncProtocol::Auto | SyncProtocol::V2) => {
360-
let client = hyper::client::Client::builder()
361-
.build::<_, hyper::Body>(connector.clone());
362-
363-
let req = http::Request::get(format!("{url}/sync/0/0/0"))
364-
.header("Authorization", format!("Bearer {}", auth_token))
365-
.body(hyper::Body::empty())
366-
.unwrap();
367-
368-
let res = client
369-
.request(req)
370-
.await
371-
.map_err(|err| crate::Error::Sync(err.into()))?;
372-
373-
if matches!(p, SyncProtocol::V2) {
374-
if !res.status().is_success() {
375-
let status = res.status();
376-
let body_bytes = hyper::body::to_bytes(res.into_body())
377-
.await
378-
.map_err(|err| crate::Error::Sync(err.into()))?;
379-
let error_message = String::from_utf8_lossy(&body_bytes);
380-
return Err(crate::Error::Sync(format!("HTTP error {}: {}", status, error_message).into()));
360+
#[cfg(feature = "sync")]
361+
{
362+
use super::SyncProtocol;
363+
match sync_protocol {
364+
p @ (SyncProtocol::Auto | SyncProtocol::V2) => {
365+
let client = hyper::client::Client::builder()
366+
.build::<_, hyper::Body>(connector.clone());
367+
368+
let req = http::Request::get(format!("{url}/sync/0/0/0"))
369+
.header("Authorization", format!("Bearer {}", auth_token))
370+
.body(hyper::Body::empty())
371+
.unwrap();
372+
373+
let res = client
374+
.request(req)
375+
.await
376+
.map_err(|err| crate::Error::Sync(err.into()))?;
377+
378+
if matches!(p, SyncProtocol::V2) {
379+
if !res.status().is_success() {
380+
let status = res.status();
381+
let body_bytes = hyper::body::to_bytes(res.into_body())
382+
.await
383+
.map_err(|err| crate::Error::Sync(err.into()))?;
384+
let error_message = String::from_utf8_lossy(&body_bytes);
385+
return Err(crate::Error::Sync(format!("HTTP error {}: {}", status, error_message).into()));
386+
}
381387
}
382-
}
383388

384-
if res.status().is_success() {
385-
return Builder::new_synced_database(path, url, auth_token)
386-
.remote_writes(true)
387-
.read_your_writes(read_your_writes)
388-
.build()
389-
.await;
390-
}
389+
if res.status().is_success() {
390+
return Builder::new_synced_database(path, url, auth_token)
391+
.remote_writes(true)
392+
.read_your_writes(read_your_writes)
393+
.build()
394+
.await;
395+
}
391396

397+
}
398+
SyncProtocol::V1 => {}
392399
}
393-
SyncProtocol::V1 => {}
394400
}
395401

396402
let path = path.to_str().ok_or(crate::Error::InvalidUTF8Path)?.to_owned();

libsql/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ pub mod params;
131131

132132
cfg_sync! {
133133
mod sync;
134+
pub use database::SyncProtocol;
134135
}
135136

136137
cfg_replication! {
137138
pub mod replication;
138-
pub use database::SyncProtocol;
139139
}
140140

141141
cfg_core! {

0 commit comments

Comments
 (0)