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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! # What is Ditto?
//!
//! Ditto is a cross-platform, peer-to-peer database that allows apps
//! to sync **with and without** internet connectivity.
//!
//! Install Ditto into your application, then use the APIs to read and write data
//! into its storage system, and it will then automatically sync any changes to other devices.
//!
//! Unlike other synchronization solutions, Ditto is designed for "peer-to-peer" scenarios
//! where it can directly communicate with other devices even without an Internet connection.
//!
//! Additionally, Ditto automatically manages the complexity of using multiple network transports,
//! like Bluetooth, P2P Wi-Fi, and Local Area Network,
//! to find and connect to other devices and then synchronize any changes.
//!
//! # Ditto Platform Docs
//!
//! Visit <https://docs.ditto.live> to learn about the full Ditto platform,
//! including multi-language SDKs, the Ditto Cloud offering, and more.
//!
//! Rust developers should be sure to check out these essential topics:
//!
//! - [Ditto Edge Sync Platform Basics][000]
//! - [Mesh Networking 101][001]
//! - [Data-Handling Essentials][002]
//! - [Ditto Query Language (DQL)][003]
//!
//! [000]: https://docs.ditto.live/basic/about
//! [001]: https://docs.ditto.live/basic/mesh-networking-101
//! [002]: https://docs.ditto.live/basic/data-handling-essentials
//! [003]: https://docs.ditto.live/dql
//!
//! # Playground Quickstart
//!
//! Ditto offers a "playground" mode that lets you start playing and developing
//! with Ditto without any authentication hassle.
//!
//! - [Visit our credentials docs to learn how to get your Ditto AppID and Playground Token][100]
//!
//! [100]: https://docs.ditto.live/get-started/sync-credentials
//!
//! ```rust,no_run
//! # use std::sync::Arc;
//! use dittolive_ditto::prelude::*;
//!
//! fn main() -> anyhow::Result<()> {
//!     let app_id = AppId::from_env("DITTO_APP_ID")?;
//!     let playground_token = std::env::var("DITTO_PLAYGROUND_TOKEN")?;
//!     let cloud_sync = true;
//!     let custom_auth_url = None;
//!
//!     // Initialize Ditto
//!     let ditto = Ditto::builder()
//!         .with_root(Arc::new(PersistentRoot::from_current_exe()?))
//!         .with_identity(|ditto_root| {
//!             identity::OnlinePlayground::new(
//!                 ditto_root,
//!                 app_id,
//!                 playground_token,
//!                 cloud_sync,
//!                 custom_auth_url,
//!             )
//!         })?
//!         .build()?;
//!
//!     // Start syncing with peers
//!     ditto.start_sync()?;
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Write data using Ditto Query Language (DQL)
//!
//! The preferred method to write data to Ditto is by using DQL.
//! To do this, we'll first access the Ditto [`Store`][store], then
//! execute a DQL insert statement.
//!
//! ```
//! use dittolive_ditto::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize)]
//! struct Car {
//!     color: String,
//!     make: String,
//! }
//!
//! async fn dql_insert_car(ditto: &Ditto, car: &Car) -> anyhow::Result<()> {    
//!     let store = ditto.store();
//!     let query_result = store.execute(
//!         // `cars` is the collection name
//!         "INSERT INTO cars DOCUMENTS (:newCar)",
//!         Some(serde_json::json!({
//!             "newCar": car
//!         }).into())
//!     ).await?;
//!
//!     // Optional: See the count of items inserted
//!     let item_count = query_result.item_count();
//!
//!     // Optional: Inspect each item that was inserted
//!     for query_item in query_result.iter() {
//!         println!("Inserted: {}", query_item.json_string());
//!     }
//!
//!     Ok(())
//! }
//!
//! // To call:
//! # async fn call_dql_insert(ditto: Ditto) -> anyhow::Result<()> {
//! let my_car = Car {
//!     color: "blue".to_string(),
//!     make: "ford".to_string(),
//! };
//! dql_insert_car(&ditto, &my_car).await?;
//! # Ok(())
//! # }
//! ```
//!
//! - See the [DQL INSERT documentation][200] for more info on DQL inserts
//! - See [`QueryResult`] and [`QueryResultItem`] to learn about the returned values
//! - See the [Ditto Rust template repository][201] for full examples
//! - Tip: Make sure you have [`serde`] added to your `Cargo.toml` with the `derive` feature
//! - Tip: Make sure you have [`serde_json`] added to your `Cargo.toml`
//!
//! [`QueryResult`]: crate::store::dql::QueryResult
//! [`QueryResultItem`]: crate::store::dql::QueryResultItem
//! [200]: https://docs.ditto.live/dql/insert
//! [201]: https://github.com/getditto/template-rust/blob/25ff8da0f2eb753c20c871f01c70512c368a9235/src/bin/simple_dql.rs#L124-L142
//! [`serde`]: https://docs.rs/serde
//! [`serde_json`]: https://docs.rs/serde_json
//!
//! ## Read data using DQL
//!
//! ```
//! use dittolive_ditto::prelude::*;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Serialize, Deserialize)]
//! struct Car {
//!     color: String,
//!     make: String,
//! }
//!
//! async fn dql_select_cars(ditto: &Ditto, color: &str) -> anyhow::Result<Vec<Car>> {
//!     let store = ditto.store();
//!     let query_result = store.execute(
//!         "SELECT * FROM cars WHERE color = :myColor",
//!         Some(serde_json::json!({
//!             "myColor": color
//!         }).into())
//!     ).await?;
//!    
//!     let cars = query_result.iter()
//!         .map(|query_item| query_item.deserialize_value::<Car>())
//!         .collect::<Result<Vec<Car>, _>>()?;
//!
//!     Ok(cars)
//! }
//!
//! // To call:
//! # async fn call_dql_select(ditto: Ditto) -> anyhow::Result<()> {
//! let cars: Vec<Car> = dql_select_cars(&ditto, "blue").await?;
//! # Ok(())
//! # }
//! ```
//!
//! - See the [DQL SELECT documentation][300] for more info on DQL selects
//! - See the [Ditto Rust template repository][301] for full examples
//!
//! [300]: https://docs.ditto.live/dql/select
//! [301]: https://github.com/getditto/template-rust/blob/25ff8da0f2eb753c20c871f01c70512c368a9235/src/bin/simple_dql.rs#L144-L158

#![allow(clippy::all)]
#![warn(rust_2018_idioms)]
#![warn(clippy::correctness)]
#![cfg_attr(not(test), warn(clippy::perf))]
#![cfg_attr(
    doc,
    warn(
        rustdoc::bare_urls,
        rustdoc::broken_intra_doc_links,
        rustdoc::invalid_codeblock_attributes,
        rustdoc::invalid_rust_codeblocks,
    )
)]

#[allow(unused_imports)]
#[macro_use]
extern crate ext_trait;
#[allow(unused_extern_crates)]
extern crate ffi_sdk;
#[macro_use]
extern crate macro_rules_attribute;

#[cfg(test)]
extern crate self as dittolive_ditto;

#[macro_use]
#[doc(hidden)]
/// Internal utility functions / macros
pub mod utils;

pub mod disk_usage;

/// Provides access to authentication information and methods for logging on to Ditto Cloud.
/// Relevant when using an [`OnlineWithAuthentication`](identity::OnlineWithAuthentication)
/// identity.
pub mod auth;

pub mod ditto;

pub mod error;

#[cfg(feature = "experimental")]
pub mod experimental;

pub mod identity;

pub mod fs;

pub mod logger;

pub mod observer;

pub mod prelude;

pub mod small_peer_info;

pub mod store;

pub mod subscription;

pub mod sync;

pub mod transport;

pub mod types;

#[cfg(test)]
pub(crate) mod test_helpers;