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
use_prelude!();
use super::DittoRga;

/// Represents a mutable CRDT rga that can be updated while updating a document.
///
/// This class can't be instantiated directly. It's returned from the `get_mut`
/// method of DittoMutDocument.
pub struct DittoMutableRga {
    document: *mut ffi_sdk::Document,
    path: char_p::Box,
    rga: DittoRga,
}

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

impl DittoMutableRga {
    /// Helper function to have a properly indexed path.
    fn formatted_path(&self, index: usize) -> String {
        format!("{}[{}]", &self.path, index)
    }

    /// The operation may fail if the content of the Rga can not be serialized into T
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn get<T: DeserializeOwned>(&self, index: usize) -> Result<T, DittoError> {
        self.rga.get(index)
    }
    /// Set a value at the specified index.
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn set<T: Serialize>(&mut self, value: T, index: usize) -> Result<(), DittoError> {
        let path = self.formatted_path(index);
        let document = self.document;
        // needed to get a ref mut to the document pointer
        unsafe {
            document.as_mut().unwrap().set(&path, &value)?;
        }
        Ok(())
    }

    /// Remove a value at the specified index.
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn remove(&mut self, index: usize) -> Result<(), DittoError> {
        self.rga.remove(index)?;
        let path = self.formatted_path(index);
        let document = self.document;
        // needed to get a ref mut to the document pointer
        unsafe {
            document.as_mut().unwrap().remove(&path)?;
        }

        Ok(())
    }

    /// Push a value on to the end of the RGA.
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn push<T: Serialize + Clone>(&mut self, value: T) -> Result<(), DittoError> {
        let path = self.path.clone();
        let document = self.document;
        // needed to get a ref mut to the document pointer
        unsafe {
            document
                .as_mut()
                .unwrap()
                .push(path.to_str(), value.clone())?;
        }

        self.rga.push(value)?;
        Ok(())
    }

    /// Pop a value off the end of the RGA.
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn pop(&mut self) -> Result<serde_json::Value, DittoError> {
        let path = self.path.clone();
        let document = self.document;
        // needed to get a ref mut to the document pointer
        let value = unsafe { document.as_mut().unwrap().pop(path.to_str()) }?;

        self.rga.pop();
        Ok(value)
    }

    /// Inserts a value into the RGA at the index specified.
    #[deprecated(
        note = "DittoRga usage should be replaced. Use arrays inside DittoRegisters instead"
    )]
    pub fn insert<T: Clone + Serialize>(
        &mut self,
        value: T,
        index: usize,
    ) -> Result<(), DittoError> {
        self.rga.insert(index, value.clone())?;
        let path = self.formatted_path(index);
        let document = self.document;
        // needed to get a ref mut to the document pointer
        unsafe {
            document.as_mut().unwrap().insert(&path, value)?;
        }

        Ok(())
    }
}