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
use_prelude!();

use super::DittoCounter;

pub struct DittoMutableCounter {
    document: *mut ffi_sdk::Document,
    path: char_p::Box,
    counter: DittoCounter,
}

impl DittoMutableCounter {

    pub fn new (
        value: f64,
        document: *mut ffi_sdk::Document,
        path: String,
    ) -> Result<Self> {
        let path = char_p::Box::try_from(path).map_err(|_| ErrorKind::Internal)?;
        Ok(Self {
            counter: DittoCounter::new_with_value(value),
            document,
            path,
        })
    }
}

impl MutableValue for DittoMutableCounter {
    type BaseType = DittoCounter;
    fn mutable_version(base: Self::BaseType, document: &mut ffi_sdk::Document, path: char_p::Box)
        -> Result<Self>
    {
        Ok(Self { counter: base, document, path })
    }
}

impl DittoMutableCounter {
    pub fn increment(&mut self, amount: f64) -> Result<()> {
        // We have an existing document
        unsafe {
            ffi_sdk::ditto_document_increment_counter(
                &mut *self.document,
                self.path.as_ref(),
                amount,
            ).into_rust_result()?;
        }
        self.counter.value += amount;
        Ok(())
    }
}