Skip to main content

dittolive_ditto/store/query_builder/live_query/
single_document_event.rs

1use_prelude!();
2use super::*;
3
4/// Use [`pending_id_specific_op.observe_local(...)`] to receive `SingleDocumentLiveQueryEvent`s
5/// describing document changes.
6///
7/// Provides information about a live query event relating to a single document live query.
8///
9/// [`pending_id_specific_op.observe_local(...)`]: crate::store::query_builder::PendingIdSpecificOperation::observe_local
10#[derive(Debug)]
11pub struct SingleDocumentLiveQueryEvent {
12    /// States whether the event is an `initial` event or not.
13    pub(crate) is_initial: bool,
14    pub(crate) old_document: Option<ffi_sdk::BoxedDocument>,
15}
16
17impl SingleDocumentLiveQueryEvent {
18    /// Return true if the event is an "initial"
19    pub fn is_initial(&self) -> bool {
20        self.is_initial
21    }
22
23    /// Return the old boxed document
24    pub fn old_document(&self) -> &Option<ffi_sdk::BoxedDocument> {
25        &self.old_document
26    }
27}
28
29impl SingleDocumentLiveQueryEvent {
30    /// Return hash of the eventually contained document.
31    pub fn hash(&self, doc: &Option<ffi_sdk::BoxedDocument>) -> Result<u64, DittoError> {
32        let zero_or_one_doc: &[_] = doc.as_ref().map_or(&[], ::core::slice::from_ref);
33        ffi_sdk::ditto_documents_hash(zero_or_one_doc.into()).ok()
34    }
35
36    /// Return hash of the mnemonic of the eventually contained document.
37    pub fn hash_mnemonic(
38        &self,
39        doc: &Option<ffi_sdk::BoxedDocument>,
40    ) -> Result<String, DittoError> {
41        let zero_or_one_doc: &[_] = doc.as_ref().map_or(&[], ::core::slice::from_ref);
42        let mnemonic_c_str =
43            { ffi_sdk::ditto_documents_hash_mnemonic(zero_or_one_doc.into()).ok()? };
44        Ok(mnemonic_c_str.into_string())
45    }
46}