Skip to content

Commit 30296eb

Browse files
committed
Better documentation
1 parent 9152929 commit 30296eb

9 files changed

+533
-280
lines changed

src/blob.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131
use std::ffi::c_void;
3232
use std::marker::PhantomData;
3333

34-
/** A binary attachment to a Document. */
34+
/// A binary attachment to a Document
3535
pub struct Blob {
3636
pub(crate) cbl_ref: *const CBLBlob,
3737
}
@@ -46,7 +46,7 @@ impl CblRef for Blob {
4646
impl Blob {
4747
//////// CREATION
4848

49-
/** Creates a new blob, given its contents as a byte array. */
49+
/// Creates a new blob, given its contents as a byte array.
5050
pub fn new_from_data(data: &[u8], content_type: &str) -> Self {
5151
unsafe {
5252
let blob = CBLBlob_CreateWithData(
@@ -57,8 +57,8 @@ impl Blob {
5757
}
5858
}
5959

60-
/** Creates a new blob from data that has has been written to a [`Writer`].
61-
You should then add the blob to a document as a property, using [`Slot::put_blob`]. */
60+
/// Creates a new blob from data that has has been written to a [`Writer`].
61+
/// You should then add the blob to a document as a property, using [`Slot::put_blob`].
6262
pub fn new_from_stream(mut stream: BlobWriter, content_type: &str) -> Self {
6363
unsafe {
6464
let blob = CBLBlob_CreateWithStream(from_str(content_type).get_ref(), stream.get_ref());
@@ -67,7 +67,7 @@ impl Blob {
6767
}
6868
}
6969

70-
// called by FleeceReference::as_blob()
70+
/// called by FleeceReference::as_blob()
7171
pub(crate) fn from_value<V: FleeceReference>(value: &V) -> Option<Self> {
7272
unsafe {
7373
let blob = FLDict_GetBlob(FLValue_AsDict(value._fleece_ref()));
@@ -81,30 +81,30 @@ impl Blob {
8181

8282
//////// ACCESSORS
8383

84-
/** The length of the content data in bytes. */
84+
/// The length of the content data in bytes
8585
pub fn length(&self) -> u64 {
8686
unsafe { CBLBlob_Length(self.get_ref()) }
8787
}
8888

89-
/** The unique digest of the blob: A base64-encoded SHA-1 digest of its data. */
89+
/// The unique digest of the blob: A base64-encoded SHA-1 digest of its data
9090
pub fn digest(&self) -> &str {
9191
unsafe { CBLBlob_Digest(self.get_ref()).as_str().unwrap() }
9292
}
9393

94-
/** The MIME type assigned to the blob, if any. */
94+
/// The MIME type assigned to the blob, if any
9595
pub fn content_type(&self) -> Option<&str> {
9696
unsafe { CBLBlob_ContentType(self.get_ref()).as_str() }
9797
}
9898

99-
/** The blob's metadata properties as a dictionary. */
99+
/// The blob's metadata properties as a dictionary
100100
pub fn properties(&self) -> Dict {
101101
unsafe { Dict::new(CBLBlob_Properties(self.get_ref())) }
102102
}
103103

104104
//////// READING:
105105

106-
/** Reads the blob's contents into memory and returns them as a byte array.
107-
This can potentially allocate a lot of memory! */
106+
/// Reads the blob's contents into memory and returns them as a byte array.
107+
/// This can potentially allocate a lot of memory!
108108
pub fn load_content(&self) -> Result<Vec<u8>> {
109109
unsafe {
110110
let mut err = CBLError::default();
@@ -113,7 +113,7 @@ impl Blob {
113113
}
114114
}
115115

116-
/** Opens a stream for reading a blob's content from disk. */
116+
/// Opens a stream for reading a blob's content from disk.
117117
pub fn open_content(&self) -> Result<BlobReader> {
118118
check_ptr(
119119
|err| unsafe { CBLBlob_OpenContentStream(self.get_ref(), err) },
@@ -146,15 +146,15 @@ impl Clone for Blob {
146146
//////// BLOB ADDITIONS FOR ARRAY / DICT:
147147

148148
impl Slot<'_> {
149-
/** Stores a Blob reference in an Array or Dict. This is how you add a Blob to a Document. */
149+
/// Stores a Blob reference in an Array or Dict. This is how you add a Blob to a Document.
150150
pub fn put_blob(self, blob: &mut Blob) {
151151
unsafe { FLSlot_SetBlob(self.get_ref(), blob.get_ref() as *mut CBLBlob) }
152152
}
153153
}
154154

155155
//////// BLOB READER
156156

157-
/** A stream for reading Blob conents. */
157+
/// A stream for reading Blob conents
158158
pub struct BlobReader<'r> {
159159
pub blob: &'r Blob,
160160
stream_ref: *mut CBLBlobReadStream,
@@ -192,9 +192,8 @@ impl Drop for BlobReader<'_> {
192192

193193
//////// BLOB WRITER
194194

195-
/** A stream for writing data that will become a Blob's contents.
196-
After you're done writing the data, call [`Blob::new_from_stream`],
197-
then add the Blob to a document property via [`Slot::put_blob`]. */
195+
/// A stream for writing data that will become a Blob's contents.
196+
/// After you're done writing the data, call [`Blob::new_from_stream`], then add the Blob to a document property via [`Slot::put_blob`].
198197
pub struct BlobWriter<'d> {
199198
stream_ref: *mut CBLBlobWriteStream,
200199
db: PhantomData<&'d mut Database>,

src/collection.rs

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,67 @@ use crate::{
1010

1111
pub static DEFAULT_NAME: &str = "_default";
1212

13+
/// A Collection represents a collection which is a container for documents.
14+
///
15+
/// A collection can be thought as a table in a relational database. Each collection belongs to
16+
/// a scope which is simply a namespce, and has a name which is unique within its scope.
17+
///
18+
/// When a new database is created, a default collection named "_default" will be automatically
19+
/// created. The default collection is created under the default scope named "_default".
20+
/// The name of the default collection and scope can be referenced by using
21+
/// kCBLDefaultCollectionName and \ref kCBLDefaultScopeName constant.
22+
///
23+
/// @note The default collection cannot be deleted.
24+
///
25+
/// When creating a new collection, the collection name, and the scope name are required.
26+
/// The naming rules of the collections and scopes are as follows:
27+
/// - Must be between 1 and 251 characters in length.
28+
/// - Can only contain the characters A-Z, a-z, 0-9, and the symbols _, -, and %.
29+
/// - Cannot start with _ or %.
30+
/// - Both scope and collection names are case sensitive.
31+
///
32+
/// ## `CBLCollection` Lifespan
33+
/// `CBLCollection` is ref-counted. Same as the CBLDocument, the CBLCollection objects
34+
/// created or retrieved from the database must be released after you are done using them.
35+
/// When the database is closed or released, the collection objects will become invalid,
36+
/// most operations on the invalid \ref CBLCollection object will fail with either the
37+
/// \ref kCBLErrorNotOpen error or null/zero/empty result.
38+
///
39+
/// ##Legacy Database and API
40+
/// When using the legacy database, the existing documents and indexes in the database will be
41+
/// automatically migrated to the default collection.
42+
///
43+
/// Any pre-existing database functions that refer to documents, listeners, and indexes without
44+
/// specifying a collection such as \ref CBLDatabase_GetDocument will implicitly operate on
45+
/// the default collection. In other words, they behave exactly the way they used to, but
46+
/// collection-aware code should avoid them and use the new Collection API instead.
47+
/// These legacy functions are deprecated and will be removed eventually.
1348
#[derive(Debug, PartialEq, Eq)]
1449
pub struct Collection {
1550
cbl_ref: *mut CBLCollection,
1651
}
1752

1853
impl Collection {
54+
pub const DEFAULT_NAME: &str = "_default";
55+
pub const DEFAULT_FULLE_NAME: &str = "_default._default";
56+
57+
//////// CONSTRUCTORS:
58+
59+
/// Takes ownership of the object and increase it's reference counter.
1960
pub(crate) fn retain(cbl_ref: *mut CBLCollection) -> Self {
2061
Self {
2162
cbl_ref: unsafe { retain(cbl_ref) },
2263
}
2364
}
2465

25-
/** Returns the scope of the collection. */
66+
////////
67+
68+
/// Returns the scope of the collection.
2669
pub fn scope(&self) -> Scope {
2770
unsafe { Scope::retain(CBLCollection_Scope(self.get_ref())) }
2871
}
2972

30-
/** Returns the collection name. */
73+
/// Returns the collection name.
3174
pub fn name(&self) -> String {
3275
unsafe {
3376
CBLCollection_Name(self.get_ref())
@@ -36,7 +79,7 @@ impl Collection {
3679
}
3780
}
3881

39-
/** Returns the collection full name */
82+
/// Returns the collection full name.
4083
pub fn full_name(&self) -> String {
4184
unsafe {
4285
CBLCollection_FullName(self.get_ref())
@@ -45,17 +88,17 @@ impl Collection {
4588
}
4689
}
4790

48-
/** Returns the collection's database */
91+
/// Returns the collection's database.
4992
pub fn database(&self) -> Database {
5093
unsafe { Database::wrap(CBLCollection_Database(self.get_ref())) }
5194
}
5295

53-
/** Returns the number of documents in the collection. */
96+
/// Returns the number of documents in the collection.
5497
pub fn count(&self) -> u64 {
5598
unsafe { CBLCollection_Count(self.get_ref()) }
5699
}
57100

58-
/** Registers a collection change listener callback. It will be called after one or more documents are changed on disk. */
101+
/// Registers a collection change listener callback. It will be called after one or more documents are changed on disk.
59102
pub fn add_listener(
60103
&mut self,
61104
listener: CollectionChangeListener,
@@ -97,8 +140,8 @@ impl Clone for Collection {
97140
}
98141
}
99142

100-
/** A collection change listener callback, invoked after one or more documents are changed on disk. */
101-
type CollectionChangeListener = Box<dyn Fn(Collection, Vec<String>)>;
143+
/// A collection change listener callback, invoked after one or more documents are changed on disk.
144+
pub type CollectionChangeListener = Box<dyn Fn(Collection, Vec<String>)>;
102145

103146
#[no_mangle]
104147
unsafe extern "C" fn c_collection_change_listener(

0 commit comments

Comments
 (0)