dittolive_ditto/ditto/mod.rs
1//! # Entry point for the DittoSDK
2//!
3//! `Ditto` is a cross-platform peer-to-peer database that allows apps to sync with and even without
4//! internet connectivity.
5//!
6//! To manage your local data and connections to other peers in the mesh, `Ditto` gives you access
7//! to:
8//! * [`Store`], the entry point to the database.
9//! * [`TransportConfig`] to change the transport layers in use.
10//! * [`Presence`] to monitor peers in the mesh.
11//! * [`DiskUsage`] to monitor local Ditto disk usage.
12
13use_prelude!();
14
15pub mod builder;
16pub mod init;
17
18use std::{env, sync::Weak};
19
20/// The log levels that the Ditto SDK supports.
21pub use ffi_sdk::CLogLevel as LogLevel;
22use ffi_sdk::{BoxedDitto, FsComponent};
23use uuid::Uuid;
24
25#[allow(deprecated)]
26use self::builder::DittoBuilder;
27#[allow(deprecated)]
28use crate::presence::observer_legacy::PeersObserver;
29use crate::{
30 disk_usage::DiskUsage,
31 ditto::init::ConfigOrIdentity,
32 error::{DittoError, ErrorKind, LicenseTokenError},
33 identity::DittoAuthenticator,
34 presence::Presence,
35 small_peer_info::SmallPeerInfo,
36 transport::TransportConfig,
37 utils::{extension_traits::FfiResultIntoRustResult, prelude::*},
38};
39
40#[extension(pub(crate) trait TryUpgrade)]
41impl std::sync::Weak<BoxedDitto> {
42 fn try_upgrade(&self) -> Result<Arc<BoxedDitto>, ErrorKind> {
43 self.upgrade().ok_or(ErrorKind::ReleasedDittoInstance)
44 }
45}
46
47/// The entrypoint for accessing all Ditto functionality.
48///
49/// Use the `Ditto` object to access all other Ditto APIs, such as:
50///
51/// - [`ditto.store()`] to access the [`Store`] API and read and write data on this peer
52/// - [`ditto.sync()`] to access the [`Sync`] API and sync data with other peers
53/// - [`ditto.presence()`] to access the [`Presence`] API and inspect connected peers
54/// - [`ditto.small_peer_info()`] to access the [`SmallPeerInfo`] API and manage peer metadata
55/// - [`ditto.disk_usage()`] to access the [`DiskUsage`] API and inspect disk usage
56///
57/// [`ditto.store()`]: crate::Ditto::store
58/// [`ditto.sync()`]: crate::Ditto::sync
59/// [`ditto.presence()`]: crate::Ditto::presence
60/// [`ditto.small_peer_info()`]: crate::Ditto::small_peer_info
61/// [`ditto.disk_usage()`]: crate::Ditto::disk_usage
62pub struct Ditto {
63 pub(crate) fields: Arc<DittoFields>,
64 /// Always `true` except in `Auth::logout()`.
65 is_shut_down_able: bool,
66}
67
68impl std::ops::Deref for Ditto {
69 type Target = DittoFields;
70
71 #[inline]
72 fn deref(&'_ self) -> &'_ DittoFields {
73 &self.fields
74 }
75}
76
77impl Ditto {
78 pub(crate) fn upgrade(weak: &Weak<DittoFields>) -> Result<Ditto> {
79 let fields = weak.upgrade().ok_or(ErrorKind::ReleasedDittoInstance)?;
80 Ok(Ditto::new_temp(fields))
81 }
82}
83
84/// Inner fields for Ditto
85#[doc(hidden)]
86// TODO(pub_check)
87pub struct DittoFields {
88 // FIXME: (Ham & Daniel) - ideally we'd use this in the same way as we do
89 // with the `fields` on `Ditto` (only ever extracting weak references)
90 pub(crate) ditto: Arc<ffi_sdk::BoxedDitto>,
91 has_auth: bool,
92 state: ConfigOrIdentity,
93 pub(crate) store: Store,
94 pub(crate) sync: crate::sync::Sync,
95 #[allow(deprecated)]
96 site_id: SiteId,
97 presence: Arc<Presence>,
98 disk_usage: DiskUsage,
99 small_peer_info: SmallPeerInfo,
100 // This field must go last since after it is dropped the backing fs will dangle.
101 ditto_root: Arc<dyn DittoRoot>, // Arc since Identity will need a copy
102}
103
104// We use this pattern to ensure `self` is not used after `ManuallyDrop::take()`-ing its fields.
105impl Drop for Ditto {
106 fn drop(&mut self) {
107 if self.is_shut_down_able {
108 // stop all transports
109 self.stop_sync();
110 // Here, ditto is implicitly dropped using ditto_free if there is no strong reference to
111 // it anymore. We need to make sure that `ditto_shutdown` is called before
112 // `ditto_free` gets called though, as this will perform all the necessary
113 // pre-drop actions such as stopping TCP servers, etc.
114 ffi_sdk::ditto_shutdown(&self.ditto);
115 }
116 }
117}
118
119// Public interface for modifying Transport configuration.
120impl Ditto {
121 /// Clean shutdown of the Ditto instance
122 pub fn close(self) {
123 // take ownership of Ditto in order to drop it
124 }
125
126 /// Starts the network transports. Ditto will connect to other devices.
127 ///
128 /// By default, Ditto will enable all peer-to-peer transport types.
129 /// The network configuration can be customized using [`set_transport_config`].
130 ///
131 /// Performance of initial sync when bootstrapping a new peer can be improved
132 /// by calling [`disable_sync_with_v3`] before calling `start_sync`. Only
133 /// do this when all peers in the mesh are known to be running Ditto v4 or higher.
134 ///
135 /// [`set_transport_config`]: Self::set_transport_config
136 /// [`disable_sync_with_v3`]: Self::disable_sync_with_v3
137 pub fn start_sync(&self) -> Result<(), DittoError> {
138 let result = ffi_sdk::dittoffi_ditto_try_start_sync(&self.ditto);
139 if let Some(error) = result.error {
140 if ffi_sdk::dittoffi_error_code(&*error)
141 == ffi_sdk::FfiErrorCode::ActivationNotActivated
142 {
143 return Err(ErrorKind::NotActivated.into());
144 }
145 return Err(DittoError::from(error));
146 }
147 Ok(())
148 }
149
150 /// Stop syncing on all transports.
151 ///
152 /// You may continue to use the Ditto store locally but no data
153 /// will sync to or from other devices.
154 pub fn stop_sync(&self) {
155 ffi_sdk::dittoffi_ditto_stop_sync(&self.ditto);
156 }
157
158 /// Returns `true` if sync is currently active, otherwise returns `false`.
159 ///
160 /// Use [`Ditto::start_sync`] to activate and [`Ditto::stop_sync`] to deactivate sync.
161 pub fn is_sync_active(&self) -> bool {
162 ffi_sdk::dittoffi_ditto_is_sync_active(&self.ditto)
163 }
164
165 /// Set a new [`TransportConfig`] and begin syncing over these
166 /// transports. Any change to start or stop a specific transport should proceed via providing a
167 /// modified configuration to this method.
168 pub fn set_transport_config(&self, config: TransportConfig) {
169 let cbor = serde_cbor::to_vec(&config).expect("bug: failed to serialize TransportConfig");
170 ffi_sdk::dittoffi_ditto_try_set_transport_config(&self.ditto, (&*cbor).into(), false)
171 .into_rust_result()
172 .expect("bug: core failed to set transport config");
173 }
174
175 /// Convenience method to update the current transport config of the receiver.
176 ///
177 /// Invokes the block with a copy of the current transport config which
178 /// you can alter to your liking. The updated transport config is then set
179 /// on the receiver.
180 ///
181 /// You may use this method to alter the configuration at any time.
182 /// Sync will not begin until [`ditto.start_sync()`] is invoked.
183 ///
184 /// # Example
185 ///
186 /// Edit the config by simply mutating the `&mut TransportConfig` passed
187 /// to your callback:
188 ///
189 /// ```
190 /// use dittolive_ditto::prelude::*;
191 /// # fn example(ditto: &Ditto) -> anyhow::Result<()> {
192 ///
193 /// // Enable the TCP listener on port 4000
194 /// ditto.update_transport_config(|config| {
195 /// config.listen.tcp.enabled = true;
196 /// config.listen.tcp.interface_ip = "0.0.0.0".to_string();
197 /// config.listen.tcp.port = 4000;
198 /// });
199 /// # Ok(())
200 /// # }
201 /// ```
202 ///
203 /// [`ditto.start_sync()`]: crate::prelude::Ditto::start_sync
204 pub fn update_transport_config(&self, update: impl FnOnce(&mut TransportConfig)) {
205 let mut transport_config = self.transport_config();
206 update(&mut transport_config);
207 self.set_transport_config(transport_config);
208 }
209
210 /// Returns a snapshot of the currently configured transports.
211 #[doc(hidden)]
212 #[deprecated(note = "Use `.transport_config()` instead")]
213 pub fn current_transport_config(&self) -> Result<TransportConfig, DittoError> {
214 let transport_config_cbor = ffi_sdk::dittoffi_ditto_transport_config(&self.ditto);
215 let transport_config =
216 serde_cbor::from_slice::<TransportConfig>(transport_config_cbor.as_slice())?;
217 Ok(transport_config)
218 }
219
220 /// Returns a snapshot of the currently configured transports.
221 ///
222 /// # Example
223 ///
224 /// ```
225 /// # use dittolive_ditto::prelude::*;
226 /// # fn example(ditto: &Ditto) {
227 /// let transport_config = ditto.transport_config();
228 /// println!("Current transport config: {transport_config:#?}");
229 /// # }
230 /// ```
231 pub fn transport_config(&self) -> TransportConfig {
232 let transport_config_cbor = ffi_sdk::dittoffi_ditto_transport_config(&self.ditto);
233 serde_cbor::from_slice::<TransportConfig>(transport_config_cbor.as_slice())
234 .expect("bug: failed to deserialize TransportConfig from core")
235 }
236}
237
238impl Ditto {
239 /// Return the version of the SDK.
240 pub fn with_sdk_version<R>(ret: impl FnOnce(&'_ str) -> R) -> R {
241 ret(ffi_sdk::ditto_get_sdk_version().to_str())
242 }
243}
244
245#[doc(hidden)]
246impl Ditto {
247 #[deprecated(note = "Use `DittoLogger::set_logging_enabled()` instead")]
248 /// Enable or disable logging.
249 pub fn set_logging_enabled(enabled: bool) {
250 ffi_sdk::ditto_logger_enabled(enabled)
251 }
252
253 #[deprecated(note = "Use `DittoLogger::get_logging_enabled()` instead")]
254 /// Return true if logging is enabled.
255 pub fn get_logging_enabled() -> bool {
256 ffi_sdk::ditto_logger_enabled_get()
257 }
258
259 #[deprecated(note = "Use `DittoLogger::get_emoji_log_level_headings_enabled()` instead")]
260 /// Represent whether or not emojis should be used as the log level indicator in the logs.
261 pub fn get_emoji_log_level_headings_enabled() -> bool {
262 ffi_sdk::ditto_logger_emoji_headings_enabled_get()
263 }
264
265 #[deprecated(note = "Use `DittoLogger::set_emoji_log_level_headings_enabled()` instead")]
266 /// Set whether or not emojis should be used as the log level indicator in the logs.
267 pub fn set_emoji_log_level_headings_enabled(enabled: bool) {
268 ffi_sdk::ditto_logger_emoji_headings_enabled(enabled);
269 }
270
271 #[deprecated(note = "Use `DittoLogger::get_minimum_log_level()` instead")]
272 /// Get the current minimum log level.
273 pub fn get_minimum_log_level() -> LogLevel {
274 ffi_sdk::ditto_logger_minimum_log_level_get()
275 }
276
277 #[deprecated(note = "Use `DittoLogger::set_minimum_log_level()` instead")]
278 /// Set the current minimum log level.
279 pub fn set_minimum_log_level(log_level: LogLevel) {
280 ffi_sdk::ditto_logger_minimum_log_level(log_level);
281 }
282}
283
284impl Ditto {
285 /// Activate an offline [`Ditto`] instance by setting a license token.
286 ///
287 /// You cannot initiate sync on an offline
288 /// ([`OfflinePlayground`], [`Manual`], or [`SharedKey`])
289 /// [`Ditto`] instance before you have activated it.
290 pub fn set_offline_only_license_token(&self, license_token: &str) -> Result<(), DittoError> {
291 if self.state.requires_offline_only_license_token() {
292 use ffi_sdk::LicenseVerificationResult;
293 use safer_ffi::prelude::{AsOut, ManuallyDropMut};
294 let c_license: char_p::Box = char_p::new(license_token);
295
296 let mut err_msg = None;
297 let out_err_msg = err_msg.manually_drop_mut().as_out();
298 let res =
299 ffi_sdk::ditto_verify_license(&self.ditto, c_license.as_ref(), Some(out_err_msg));
300
301 if res == LicenseVerificationResult::LicenseOk {
302 return Ok(());
303 }
304
305 let err_msg = err_msg.unwrap();
306 error!("{err_msg}");
307
308 match res {
309 LicenseVerificationResult::LicenseExpired => {
310 Err(DittoError::license(LicenseTokenError::Expired {
311 message: err_msg.as_ref().to_string(),
312 }))
313 }
314 LicenseVerificationResult::VerificationFailed => {
315 Err(DittoError::license(LicenseTokenError::VerificationFailed {
316 message: err_msg.as_ref().to_string(),
317 }))
318 }
319 LicenseVerificationResult::UnsupportedFutureVersion => Err(DittoError::license(
320 LicenseTokenError::UnsupportedFutureVersion {
321 message: err_msg.as_ref().to_string(),
322 },
323 )),
324 _ => panic!("Unexpected license verification result {:?}", res),
325 }
326 } else {
327 Err(DittoError::new(
328 ErrorKind::Internal,
329 "Offline license tokens should only be used for Manual, SharedKey or \
330 OfflinePlayground identities",
331 ))
332 }
333 }
334
335 /// Look for a license token from a given environment variable.
336 pub fn set_license_from_env(&self, var_name: &str) -> Result<(), DittoError> {
337 match env::var(var_name) {
338 Ok(token) => self.set_offline_only_license_token(&token),
339 Err(env::VarError::NotPresent) => {
340 let msg = format!("No license token found for env var {}", &var_name);
341 Err(DittoError::from_str(ErrorKind::Config, msg))
342 }
343 Err(e) => Err(DittoError::new(ErrorKind::Config, e)),
344 }
345 }
346}
347
348impl Ditto {
349 /// Returns a reference to the underlying local data store.
350 pub fn store(&self) -> &Store {
351 &self.store
352 }
353
354 /// Entrypoint to Ditto's [`Sync`] API for syncing documents between peers.
355 ///
356 /// [`Sync`]: crate::sync::Sync
357 pub fn sync(&self) -> &crate::sync::Sync {
358 &self.sync
359 }
360
361 #[cfg(feature = "experimental-bus")]
362 /// Returns a handle to the bus controller. This can be used to send either individual messages
363 /// or open streams to remote peers.
364 /// NOTE: This API is experimental and may change in future releases.
365 pub fn bus(&self) -> crate::experimental::bus::Bus {
366 ffi_sdk::dittoffi_get_experimental_bus(&self.ditto)
367 }
368
369 /// Return a reference to the [`SmallPeerInfo`] object.
370 pub fn small_peer_info(&self) -> &SmallPeerInfo {
371 &self.small_peer_info
372 }
373
374 /// Returns the site ID that the instance of Ditto is using as part of its identity.
375 #[doc(hidden)]
376 #[deprecated(note = "Use `ditto.presence().graph().local_peer.peer_key_string` instead")]
377 pub fn site_id(&self) -> u64 {
378 self.site_id
379 }
380
381 /// Returns the Ditto persistence directory path.
382 #[doc(hidden)]
383 #[deprecated(note = "Use `ditto.absolute_persistence_directory()` instead")]
384 pub fn persistence_directory(&self) -> &Path {
385 self.ditto_root.root_path()
386 }
387
388 /// The absolute path to the persistence directory used by Ditto to persist data.
389 ///
390 /// This returns the final, resolved absolute file path where Ditto stores its data.
391 /// The value depends on what was provided in [`DittoConfig::persistence_directory`].
392 ///
393 /// - If an **absolute path** was provided, it returns that path unchanged.
394 /// - If a **relative path** was provided, it returns the path resolved relative to
395 /// [`ditto.default_root_directory()`].
396 /// - If no path was provided, it returns the default path using the pattern
397 /// `{default_root}/ditto-{database-id}` where `{default_root}` corresponds to
398 /// [`ditto.default_root_directory()`] and `{database-id}` is the Ditto database ID in
399 /// lowercase.
400 ///
401 /// This property always returns a consistent value throughout the lifetime of the Ditto
402 /// instance and represents the actual directory being used for persistence.
403 ///
404 /// - Note: "Database ID" was previously referred to as "App ID" in older versions of the SDK.
405 ///
406 /// - Note: It is not recommended to directly read or write to this directory as its structure
407 /// and contents are managed by Ditto and may change in future versions.
408 ///
409 /// - Note: When [`DittoLogger`] is enabled, logs may be written to this directory even after a
410 /// Ditto instance has been deallocated. Please refer to the documentation of [`DittoLogger`]
411 /// for more information.
412 ///
413 /// - See also: [`DittoConfig::persistence_directory`]
414 ///
415 /// [`DittoConfig::persistence_directory`]: DittoConfig::persistence_directory
416 pub fn absolute_persistence_directory(&self) -> PathBuf {
417 let path = ffi_sdk::dittoffi_ditto_absolute_persistence_directory(&self.ditto);
418 PathBuf::from(path.to_str())
419 }
420
421 /// Returns an owned snapshot of the _effective_ `DittoConfig` as used by the core library.
422 ///
423 /// - Modifying this `DittoConfig` have no effect on the active Ditto configuration.
424 /// - The returned `DittoConfig` may be different than the one passed to [`Ditto::open{,_sync}`]
425 /// because it will have resolved details such as the absolute path to the persistence
426 /// directory.
427 pub fn config(&self) -> DittoConfig {
428 let config_cbor = ffi_sdk::dittoffi_ditto_config(&self.ditto);
429 serde_cbor::from_slice(&config_cbor).expect("bug: should deserialize DittoConfig")
430 }
431
432 /// Returns the application Id being used by this [`Ditto`] instance.
433 #[doc(hidden)]
434 #[deprecated(note = "Use `.app_id()` instead")]
435 pub fn application_id(&self) -> AppId {
436 AppId(ffi_sdk::ditto_auth_client_get_app_id(&self.ditto).into_string())
437 }
438
439 /// Returns the [`AppId`] being used by this [`Ditto`] instance.
440 pub fn app_id(&self) -> AppId {
441 AppId(ffi_sdk::ditto_auth_client_get_app_id(&self.ditto).into_string())
442 }
443
444 /// Set a custom identifier for the current device.
445 ///
446 /// When using [`observe_peers`](Ditto::observe_peers), each remote peer is represented by a
447 /// short UTF-8 “device name”. By default this will be a truncated version of the device’s
448 /// hostname. It does not need to be unique among peers. Configure the device name before
449 /// calling [`start_sync`](Ditto::start_sync). If it is too long it will be truncated.
450 pub fn set_device_name(&self, name: &str) {
451 let c_device_name: char_p::Box = char_p::new(name.to_owned());
452 // We don't currently expose the device name to the user so we don't
453 // need to worry about the returned (potentially truncated) value here
454 let _ = ffi_sdk::ditto_set_device_name(&self.ditto, c_device_name.as_ref());
455 }
456
457 /// Request information about Ditto peers in range of this device.
458 ///
459 /// This method returns an observer which should be held as long as updates are required. A
460 /// newly registered observer will have a peers update delivered to it immediately. Then it will
461 /// be invoked repeatedly when Ditto devices come and go, or the active connections to them
462 /// change.
463 #[doc(hidden)]
464 #[deprecated(note = "Use `presence().observe()` instead")]
465 #[allow(deprecated)]
466 pub fn observe_peers<H>(&self, handler: H) -> PeersObserver
467 where
468 H: Fn(crate::transport::v2::V2Presence) + Send + Sync + 'static,
469 {
470 self.presence.add_observer(handler)
471 }
472
473 /// Return a handle to the [`Presence`] API to monitor peers' activity in the Ditto mesh.
474 ///
475 /// # Example
476 ///
477 /// Use [`ditto.presence().graph()`] to request a current [`PresenceGraph`] of connected peers:
478 ///
479 /// ```
480 /// use dittolive_ditto::prelude::*;
481 ///
482 /// # fn example(ditto: &Ditto) {
483 /// let presence_graph: PresenceGraph = ditto.presence().graph();
484 /// println!("Ditto mesh right now: {presence_graph:#?}");
485 /// # }
486 /// ```
487 ///
488 /// # Example
489 ///
490 /// Use [`ditto.presence().observe(...)`] to subscribe to changes in mesh presence:
491 ///
492 /// ```
493 /// use dittolive_ditto::prelude::*;
494 ///
495 /// # fn example(ditto: &Ditto) {
496 /// let _observer = ditto.presence().observe(|graph| {
497 /// println!("Ditto mesh update! {graph:#?}");
498 /// });
499 ///
500 /// // The observer is cancelled when dropped.
501 /// // In a real application, hold onto it for as long as you need it alive.
502 /// drop(_observer);
503 /// # }
504 /// ```
505 ///
506 /// [`ditto.presence().graph()`]: crate::presence::Presence::graph
507 /// [`PresenceGraph`]: crate::presence::PresenceGraph
508 /// [`ditto.presence().observe(...)`]: crate::presence::Presence::observe
509 pub fn presence(&self) -> &Arc<Presence> {
510 &self.presence
511 }
512
513 /// Return a [`DiskUsage`] to monitor the disk usage of the Ditto
514 /// instance. It can be used to retrieve an immediate representation of the Ditto file system:
515 ///
516 /// ```
517 /// # use dittolive_ditto::prelude::Ditto;
518 /// # fn call_diskusage(ditto: Ditto) {
519 /// let fs_tree = ditto.disk_usage().exec();
520 /// # }
521 /// ```
522 /// Or to bind a callback to the changes :
523 /// ```
524 /// # use dittolive_ditto::prelude::Ditto;
525 /// # fn call_diskusage(ditto:Ditto) {
526 /// let handle = ditto.disk_usage().observe(|fs_tree| {
527 /// // do something with the graph
528 /// });
529 /// // The handle must be kept to keep receiving updates on the file system.
530 /// // To stop receiving update, drop the handle.
531 /// # }
532 /// ```
533 pub fn disk_usage(&self) -> &DiskUsage {
534 &self.disk_usage
535 }
536
537 #[doc(hidden)]
538 #[deprecated(note = "Use `ditto.absolute_persistence_directory()` instead")]
539 /// Returns the [`DittoRoot`] path.
540 pub fn root_dir(&self) -> &Path {
541 #[allow(deprecated)]
542 self.persistence_directory()
543 }
544
545 #[doc(hidden)]
546 #[deprecated(note = "Use `ditto.absolute_persistence_directory()` instead")]
547 /// Returns the ditto persistence data directory path.
548 pub fn data_dir(&self) -> &Path {
549 #[allow(deprecated)]
550 self.persistence_directory()
551 }
552
553 #[cfg(test)]
554 /// Returns the [`DittoRoot`].
555 pub fn root(&self) -> Arc<dyn DittoRoot> {
556 self.ditto_root.retain()
557 }
558
559 /// Returns the current [`DittoAuthenticator`], if it exists.
560 #[doc(hidden)]
561 #[deprecated(note = "Use `.auth()` instead")]
562 pub fn authenticator(&self) -> Option<DittoAuthenticator> {
563 self.auth()
564 }
565
566 /// Returns the current [`DittoAuthenticator`], if it exists.
567 ///
568 /// The [`DittoAuthenticator`] is available when using the [`OnlinePlayground`]
569 /// and [`OnlineWithAuthentication`] identities.
570 pub fn auth(&self) -> Option<DittoAuthenticator> {
571 self.fields.has_auth.then(|| DittoAuthenticator {
572 ditto_fields: Arc::downgrade(&self.fields),
573 })
574 }
575
576 /// Returns `true` if this `Ditto` instance has been activated with a valid
577 /// license token.
578 pub fn is_activated(&self) -> bool {
579 ffi_sdk::dittoffi_ditto_is_activated(&self.ditto)
580 }
581}
582
583// Constructors
584impl Ditto {
585 /// Entrypoint to constructing `Ditto` with the builder pattern.
586 ///
587 /// # Example
588 ///
589 /// ```no_run
590 /// # use std::sync::Arc;
591 /// use dittolive_ditto::prelude::*;
592 ///
593 /// fn main() -> anyhow::Result<()> {
594 /// let app_id = AppId::from_env("DITTO_APP_ID")?;
595 /// let playground_token = std::env::var("DITTO_PLAYGROUND_TOKEN")?;
596 /// let cloud_sync = true;
597 /// let custom_auth_url = None;
598 ///
599 /// // Initialize Ditto
600 /// let ditto = Ditto::builder()
601 /// .with_root(Arc::new(PersistentRoot::from_current_exe()?))
602 /// .with_identity(|ditto_root| {
603 /// identity::OnlinePlayground::new(
604 /// ditto_root,
605 /// app_id,
606 /// playground_token,
607 /// cloud_sync,
608 /// custom_auth_url,
609 /// )
610 /// })?
611 /// .build()?;
612 ///
613 /// Ok(())
614 /// }
615 /// ```
616 ///
617 /// [See the crate docs for examples of what to do next using `Ditto`][0].
618 ///
619 /// [0]: crate#playground-quickstart
620 pub fn builder() -> DittoBuilder {
621 #[allow(deprecated)]
622 DittoBuilder::new()
623 }
624
625 /// Construct a [`Ditto`] instance with sensible defaults.
626 ///
627 /// This instance will still need to have a license set (if necessary) and sync functionality
628 /// manually started.
629 #[doc(hidden)]
630 #[deprecated(note = "Use `Ditto::open(...)` or `Ditto::open_sync(...)` instead")]
631 pub fn new(app_id: AppId) -> Ditto {
632 Ditto::builder()
633 .with_root(Arc::new(
634 PersistentRoot::from_current_exe().expect("Invalid Ditto Root"),
635 ))
636 .with_identity(|ditto_root| identity::OfflinePlayground::new(ditto_root, app_id))
637 .expect("Invalid Ditto Identity")
638 .with_minimum_log_level(LogLevel::Info)
639 .build()
640 .expect("Failed to build Ditto Instance")
641 }
642
643 // This isn't public to customers and is only used internally. It's only currently used when we
644 // have access to the `DittoFields` and want to create a `Ditto` instance for whatever reason,
645 // but we don't want `Ditto` to be shut down when this `Ditto` instance is dropped.
646 //
647 // This should definitely be considered a hack for now.
648 pub(crate) fn new_temp(fields: Arc<DittoFields>) -> Ditto {
649 Ditto {
650 fields,
651 is_shut_down_able: false,
652 }
653 }
654}
655
656impl Ditto {
657 /// Removes all sync metadata for any remote peers which aren't currently connected. This method
658 /// shouldn't usually be called. Manually running garbage collection often will result in slower
659 /// sync times. Ditto automatically runs a garbage a collection process in the background at
660 /// optimal times.
661 ///
662 /// Manually running garbage collection is typically only useful during testing if large amounts
663 /// of data are being generated. Alternatively, if an entire data set is to be evicted and it's
664 /// clear that maintaining this metadata isn't necessary, then garbage collection could be run
665 /// after evicting the old data.
666 pub fn run_garbage_collection(&self) {
667 ffi_sdk::ditto_run_garbage_collection(&self.ditto);
668 }
669
670 /// Disable sync with peers running version 3 or lower of the Ditto SDK.
671 ///
672 /// Required for the execution of mutating DQL statements.
673 ///
674 /// This setting spreads to other peers on connection. Those peers will in
675 /// turn spread it further until all peers in the mesh take on the same
676 /// setting. This is irreversible and will persist across restarts of the
677 /// Ditto instance.
678 ///
679 /// Calling this method before calling [`start_sync`] is recommended whenever
680 /// possible. This improves performance of initial sync when this peer has
681 /// never before connected to a Ditto mesh for which sync with v3 peers is
682 /// disabled.
683 ///
684 /// [`start_sync`]: Self::start_sync
685 pub fn disable_sync_with_v3(&self) -> Result<(), DittoError> {
686 let res = ffi_sdk::ditto_disable_sync_with_v3(&self.ditto);
687 if res != 0 {
688 return Err(DittoError::from_ffi(ErrorKind::Internal));
689 }
690 Ok(())
691 }
692}
693
694#[doc(hidden)] // Not implemented
695pub struct TransportDiagnostics;
696
697/// `SiteId`s are used to identity Ditto peers
698// NOTE(nimo): Ensure that after hiding this module, SiteId is still reachable
699/// ```rust,no_run
700/// // Ensure SiteId is still reachable
701/// use dittolive_ditto::ditto::SiteId;
702/// // Test that deprecation note is useful/accurate
703/// use dittolive_ditto::prelude::*;
704/// # fn example(ditto: &Ditto) {
705/// let peer_key = ditto
706/// .presence()
707/// .graph()
708/// .local_peer
709/// .peer_key_string
710/// .to_string();
711/// # }
712/// ```
713#[deprecated(note = "Use `ditto.presence().graph().local_peer.peer_key_string` instead")]
714pub type SiteId = u64;
715
716#[derive(Clone, Debug)]
717// pub struct AppId(uuid::Uuid); // Demo apps still use arbitrary strings
718/// The ID of this Ditto App, used to determine which peers to sync with
719pub struct AppId(pub(crate) String); // neither String nor Vec<u8> are Copy
720
721impl AppId {
722 /// Generate a random AppId from a UUIDv4
723 pub fn generate() -> Self {
724 let uuid = uuid::Uuid::new_v4();
725 AppId::from_uuid(uuid)
726 }
727
728 /// Generate an AppId from a given UUIDv4
729 pub fn from_uuid(uuid: Uuid) -> Self {
730 let id_str = format!("{:x}", &uuid); // lower-case with hypens
731 AppId(id_str)
732 }
733
734 /// Attempt to grab a specific AppId from some environment variable
735 pub fn from_env(var: &str) -> Result<Self, DittoError> {
736 let id_str = env::var(var).map_err(|err| DittoError::new(ErrorKind::Config, err))?;
737 Ok(AppId(id_str))
738 }
739
740 /// Return the corresponding string
741 pub fn as_str(&self) -> &str {
742 &self.0
743 }
744
745 /// Return the corresponding c string
746 pub fn to_c_string(&self) -> char_p::Box {
747 char_p::new(self.0.as_str())
748 }
749
750 /// Return the default auth URL associated with the app Id. This is of the form
751 /// `https://{app_id}.cloud.ditto.live/` by default.
752 pub fn default_auth_url(&self) -> String {
753 format!("https://{}.cloud.ditto.live", self.0)
754 }
755
756 /// Return the default WebSocket sync URL which is of the form
757 /// `wss://{app_id}.cloud.ditto.live/` by default.
758 pub fn default_sync_url(&self) -> String {
759 format!("wss://{}.cloud.ditto.live", self.0)
760 }
761}
762
763use std::{fmt, fmt::Display, str::FromStr};
764
765impl Display for AppId {
766 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
767 write!(f, "{}", self.0)
768 }
769}
770
771impl FromStr for AppId {
772 type Err = DittoError;
773 fn from_str(s: &str) -> Result<AppId, DittoError> {
774 // later s will need to be a valid UUIDv4
775 Ok(AppId(s.to_string()))
776 }
777}
778
779#[cfg(test)]
780#[path = "tests.rs"]
781mod tests;