dpp/data_contract/v0/accessors/
mod.rs

1use crate::data_contract::accessors::v0::{DataContractV0Getters, DataContractV0Setters};
2use crate::data_contract::config::DataContractConfig;
3use crate::data_contract::document_type::{DocumentType, DocumentTypeRef};
4use crate::data_contract::errors::DataContractError;
5
6use crate::data_contract::v0::DataContractV0;
7use crate::data_contract::DocumentName;
8
9use crate::data_contract::document_type::accessors::{
10    DocumentTypeV0Getters, DocumentTypeV0Setters,
11};
12use platform_value::Identifier;
13use std::collections::BTreeMap;
14
15impl DataContractV0Getters for DataContractV0 {
16    fn id(&self) -> Identifier {
17        self.id
18    }
19
20    fn id_ref(&self) -> &Identifier {
21        &self.id
22    }
23
24    fn system_version_type(&self) -> u16 {
25        0
26    }
27
28    fn version(&self) -> u32 {
29        self.version
30    }
31
32    fn owner_id(&self) -> Identifier {
33        self.owner_id
34    }
35
36    fn document_type_cloned_for_name(&self, name: &str) -> Result<DocumentType, DataContractError> {
37        self.document_type_cloned_optional_for_name(name)
38            .ok_or_else(|| {
39                DataContractError::DocumentTypeNotFound(
40                    "can not get document type from contract".to_string(),
41                )
42            })
43    }
44
45    fn document_type_borrowed_for_name(
46        &self,
47        name: &str,
48    ) -> Result<&DocumentType, DataContractError> {
49        self.document_types.get(name).ok_or_else(|| {
50            DataContractError::DocumentTypeNotFound(
51                "can not get document type from contract".to_string(),
52            )
53        })
54    }
55
56    fn document_type_for_name(&self, name: &str) -> Result<DocumentTypeRef<'_>, DataContractError> {
57        self.document_type_optional_for_name(name).ok_or_else(|| {
58            DataContractError::DocumentTypeNotFound(
59                "can not get document type from contract".to_string(),
60            )
61        })
62    }
63
64    fn document_type_optional_for_name(&self, name: &str) -> Option<DocumentTypeRef<'_>> {
65        self.document_types
66            .get(name)
67            .map(|document_type| document_type.as_ref())
68    }
69
70    fn document_type_cloned_optional_for_name(&self, name: &str) -> Option<DocumentType> {
71        self.document_types.get(name).cloned()
72    }
73
74    fn has_document_type_for_name(&self, name: &str) -> bool {
75        self.document_types.contains_key(name)
76    }
77
78    fn document_types_with_contested_indexes(&self) -> BTreeMap<&DocumentName, &DocumentType> {
79        self.document_types
80            .iter()
81            .filter(|(_, document_type)| {
82                document_type
83                    .indexes()
84                    .iter()
85                    .any(|(_, index)| index.contested_index.is_some())
86            })
87            .collect()
88    }
89
90    fn document_types(&self) -> &BTreeMap<DocumentName, DocumentType> {
91        &self.document_types
92    }
93
94    fn document_types_mut(&mut self) -> &mut BTreeMap<DocumentName, DocumentType> {
95        &mut self.document_types
96    }
97
98    fn config(&self) -> &DataContractConfig {
99        &self.config
100    }
101
102    fn config_mut(&mut self) -> &mut DataContractConfig {
103        &mut self.config
104    }
105}
106
107impl DataContractV0Setters for DataContractV0 {
108    fn set_id(&mut self, id: Identifier) {
109        self.id = id;
110
111        self.document_types
112            .iter_mut()
113            .for_each(|(_, document_type)| document_type.set_data_contract_id(id))
114    }
115
116    fn set_version(&mut self, version: u32) {
117        self.version = version;
118    }
119
120    fn increment_version(&mut self) {
121        self.version += 1;
122    }
123
124    fn set_owner_id(&mut self, owner_id: Identifier) {
125        self.owner_id = owner_id;
126    }
127
128    fn set_config(&mut self, config: DataContractConfig) {
129        self.config = config;
130    }
131}