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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
*
* * Copyright (c) 2025 Couchbase, Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
//! # Couchbase Rust SDK
//!
//! The official Couchbase SDK for Rust, providing asynchronous access to
//! [Couchbase Server](https://www.couchbase.com/) from Rust applications.
//!
//! This crate enables you to interact with a Couchbase cluster for key-value (KV) operations,
//! SQL++ (N1QL) queries, Full-Text Search (FTS), sub-document operations, and cluster management.
//!
//! # Getting Started
//!
//! Add the dependency to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! couchbase = "1.0.0-beta.1"
//! tokio = { version = "1", features = ["full"] }
//! serde = { version = "1", features = ["derive"] }
//! serde_json = "1"
//! ```
//!
//! ## Connecting to a Cluster
//!
//! The entry point is the [`Cluster`](cluster::Cluster), which is created by calling
//! [`Cluster::connect`](cluster::Cluster::connect) with a connection string and
//! [`ClusterOptions`](options::cluster_options::ClusterOptions).
//!
//! ```rust,no_run
//! use couchbase::cluster::Cluster;
//! use couchbase::authenticator::PasswordAuthenticator;
//! use couchbase::options::cluster_options::ClusterOptions;
//!
//! # async fn example() -> couchbase::error::Result<()> {
//! let authenticator = PasswordAuthenticator::new("username", "password");
//! let options = ClusterOptions::new(authenticator.into());
//!
//! let cluster = Cluster::connect("couchbase://localhost", options).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Basic Key-Value Operations
//!
//! Once connected, open a [`Bucket`](bucket::Bucket), navigate to a [`Scope`](scope::Scope)
//! and [`Collection`](collection::Collection), and perform CRUD operations:
//!
//! ```rust,no_run
//! use couchbase::cluster::Cluster;
//! use couchbase::authenticator::PasswordAuthenticator;
//! use couchbase::options::cluster_options::ClusterOptions;
//! use serde::{Deserialize, Serialize};
//!
//! #[derive(Debug, Serialize, Deserialize)]
//! struct User {
//! name: String,
//! email: String,
//! age: u32,
//! }
//!
//! # async fn example() -> couchbase::error::Result<()> {
//! let authenticator = PasswordAuthenticator::new("username", "password");
//! let options = ClusterOptions::new(authenticator.into());
//! let cluster = Cluster::connect("couchbase://localhost", options).await?;
//!
//! let bucket = cluster.bucket("my-bucket");
//! let collection = bucket.default_collection();
//!
//! // Upsert a document
//! let user = User { name: "Alice".into(), email: "alice@example.com".into(), age: 30 };
//! let result = collection.upsert("user::alice", &user, None).await?;
//! println!("CAS: {}", result.cas());
//!
//! // Get a document
//! let result = collection.get("user::alice", None).await?;
//! let user: User = result.content_as()?;
//! println!("Got user: {:?}", user);
//!
//! // Remove a document
//! collection.remove("user::alice", None).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## SQL++ (N1QL) Queries
//!
//! Execute SQL++ queries against the cluster or a specific scope using
//! [`Cluster::query`](cluster::Cluster::query) or [`Scope::query`](scope::Scope::query):
//!
//! ```rust,no_run
//! use couchbase::cluster::Cluster;
//! use couchbase::authenticator::PasswordAuthenticator;
//! use couchbase::options::cluster_options::ClusterOptions;
//! use couchbase::options::query_options::QueryOptions;
//! use futures::TryStreamExt;
//! use serde_json::Value;
//!
//! # async fn example() -> couchbase::error::Result<()> {
//! # let authenticator = PasswordAuthenticator::new("username", "password");
//! # let options = ClusterOptions::new(authenticator.into());
//! # let cluster = Cluster::connect("couchbase://localhost", options).await?;
//! let opts = QueryOptions::new()
//! .add_positional_parameter("Alice")?;
//!
//! let mut result = cluster.query(
//! "SELECT * FROM `my-bucket` WHERE name = $1",
//! opts,
//! ).await?;
//!
//! let rows: Vec<Value> = result.rows().try_collect().await?;
//! for row in rows {
//! println!("{:?}", row);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Sub-Document Operations
//!
//! Operate on parts of a JSON document using [`lookup_in`](collection::Collection) and
//! [`mutate_in`](collection::Collection) with sub-document specs:
//!
//! ```rust,no_run
//! use couchbase::subdoc::lookup_in_specs::LookupInSpec;
//! use couchbase::subdoc::mutate_in_specs::MutateInSpec;
//! # use couchbase::cluster::Cluster;
//! # use couchbase::authenticator::PasswordAuthenticator;
//! # use couchbase::options::cluster_options::ClusterOptions;
//!
//! # async fn example() -> couchbase::error::Result<()> {
//! # let authenticator = PasswordAuthenticator::new("username", "password");
//! # let options = ClusterOptions::new(authenticator.into());
//! # let cluster = Cluster::connect("couchbase://localhost", options).await?;
//! # let collection = cluster.bucket("b").default_collection();
//! // Lookup sub-document paths
//! let result = collection.lookup_in("user::alice", &[
//! LookupInSpec::get("name", None),
//! LookupInSpec::exists("email", None),
//! ], None).await?;
//!
//! let name: String = result.content_as(0)?;
//! let email_exists: bool = result.exists(1)?;
//!
//! // Mutate sub-document paths
//! collection.mutate_in("user::alice", &[
//! MutateInSpec::upsert("age", 31, None)?,
//! MutateInSpec::insert("verified", true, None)?,
//! ], None).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Handling
//!
//! All fallible operations return [`error::Result<T>`], which wraps
//! [`error::Error`]. Inspect the error kind via [`Error::kind()`](error::Error::kind):
//!
//! ```rust,no_run
//! use couchbase::error::{Error, ErrorKind};
//! # use couchbase::cluster::Cluster;
//! # use couchbase::authenticator::PasswordAuthenticator;
//! # use couchbase::options::cluster_options::ClusterOptions;
//!
//! # async fn example() -> couchbase::error::Result<()> {
//! # let authenticator = PasswordAuthenticator::new("username", "password");
//! # let options = ClusterOptions::new(authenticator.into());
//! # let cluster = Cluster::connect("couchbase://localhost", options).await?;
//! # let collection = cluster.bucket("b").default_collection();
//! match collection.get("nonexistent-key", None).await {
//! Ok(result) => println!("Found document"),
//! Err(e) if *e.kind() == ErrorKind::DocumentNotFound => {
//! println!("Document does not exist");
//! }
//! Err(e) => return Err(e),
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Architecture
//!
//! The SDK follows the Couchbase data model hierarchy:
//!
//! - **[`Cluster`](cluster::Cluster)** — Top-level entry point. Connect here, run cluster-level
//! queries, and access management APIs.
//! - **[`Bucket`](bucket::Bucket)** — Represents a Couchbase bucket. Obtained from `Cluster::bucket()`.
//! - **[`Scope`](scope::Scope)** — A namespace within a bucket. Obtained from `Bucket::scope()`.
//! Supports scoped queries and Full-Text Search.
//! - **[`Collection`](collection::Collection)** — Holds documents. Obtained from `Scope::collection()`.
//! Supports all KV, sub-document, and data structure operations.
//! - **[`BinaryCollection`](collection::BinaryCollection)** — Accessed via `Collection::binary()`.
//! Provides binary append/prepend and counter operations.
//!
//! # Modules Overview
//!
//! | Module | Description |
//! |--------|-------------|
//! | [`authenticator`] | Authentication types ([`PasswordAuthenticator`](authenticator::PasswordAuthenticator), [`CertificateAuthenticator`](authenticator::CertificateAuthenticator)) |
//! | [`cluster`] | [`Cluster`](cluster::Cluster) — the main entry point |
//! | [`bucket`] | [`Bucket`](bucket::Bucket) — bucket-level operations |
//! | [`scope`] | [`Scope`](scope::Scope) — scoped queries and search |
//! | [`collection`] | [`Collection`](collection::Collection) and [`BinaryCollection`](collection::BinaryCollection) |
//! | [`collection_ds`] | Data structure helpers (list, map, set, queue) on `Collection` |
//! | [`options`] | Option structs for configuring every operation |
//! | [`results`] | Result types returned by operations |
//! | [`error`] | [`Error`](error::Error) and [`ErrorKind`](error::ErrorKind) |
//! | [`subdoc`] | Sub-document operation specs ([`LookupInSpec`](subdoc::lookup_in_specs::LookupInSpec), [`MutateInSpec`](subdoc::mutate_in_specs::MutateInSpec)) |
//! | [`search`] | Full-Text Search queries, facets, sorting, and vector search |
//! | [`transcoding`] | Encoding/decoding helpers (JSON, raw binary, raw JSON, raw string) |
//! | [`management`] | Cluster management (buckets, collections, users, query indexes, search indexes) |
//! | [`durability_level`] | [`DurabilityLevel`](durability_level::DurabilityLevel) constants |
//! | [`mod@mutation_state`] | [`MutationState`](mutation_state::MutationState) for scan consistency |
//! | [`retry`] | Retry strategies ([`BestEffortRetryStrategy`](retry::BestEffortRetryStrategy)) |
//! | [`diagnostics`] | Connection state types |
//! | [`service_type`] | [`ServiceType`](service_type::ServiceType) constants |
//! | [`logging_meter`] | [`LoggingMeter`](logging_meter::LoggingMeter) for operation metrics |
//! | [`threshold_logging_tracer`] | [`ThresholdLoggingTracer`](threshold_logging_tracer::ThresholdLoggingTracer) for slow-operation logging |
//!
//! # Feature Flags
//!
//! | Feature | Default | Description |
//! |---------|---------|-------------|
//! | `default-tls` | ✅ | Alias for `rustls-tls` |
//! | `rustls-tls` | ✅ | Use `rustls` for TLS (recommended) |
//! | `native-tls` | ❌ | Use the platform's native TLS stack instead of `rustls` |
//! | `unstable-dns-options` | ❌ | Enable DNS-SRV bootstrap configuration (volatile) |
//! | `unstable-error-construction` | ❌ | Allow explicit `Error` construction (e.g. for mocking) |
//!
//! Note that the SDK does not typically use feature flags for API stability levels.
//! Instead, unstable features are commented with **uncommitted** or **volatile**.
extern crate core;