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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::validate::RequiredValidationType;
use crate::EntryDefIndex;
use holochain_serialized_bytes::prelude::*;
const DEFAULT_REQUIRED_VALIDATIONS: u8 = 5;
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum EntryDefId {
App(String),
CapClaim,
CapGrant,
}
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct RequiredValidations(pub u8);
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct EntryDef {
pub id: EntryDefId,
pub visibility: EntryVisibility,
pub required_validations: RequiredValidations,
pub required_validation_type: RequiredValidationType,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct EntryDefs(pub Vec<EntryDef>);
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, SerializedBytes, Copy, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum EntryVisibility {
Public,
Private,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, SerializedBytes)]
pub enum EntryDefsCallbackResult {
Defs(EntryDefs),
}
impl Default for EntryVisibility {
fn default() -> Self {
Self::Public
}
}
impl From<u8> for RequiredValidations {
fn from(u: u8) -> Self {
Self(u)
}
}
impl From<RequiredValidations> for u8 {
fn from(required_validations: RequiredValidations) -> Self {
required_validations.0
}
}
impl Default for RequiredValidations {
fn default() -> Self {
Self(DEFAULT_REQUIRED_VALIDATIONS)
}
}
impl EntryVisibility {
pub fn is_public(&self) -> bool {
*self == EntryVisibility::Public
}
}
impl EntryDef {
pub fn new(
id: EntryDefId,
visibility: EntryVisibility,
required_validations: RequiredValidations,
required_validation_type: RequiredValidationType,
) -> Self {
Self {
id,
visibility,
required_validations,
required_validation_type,
}
}
#[cfg(any(test, feature = "test_utils"))]
pub fn default_with_id<I: Into<EntryDefId>>(id: I) -> Self {
EntryDef::new(
id.into(),
Default::default(),
Default::default(),
Default::default(),
)
}
}
impl EntryDefs {
pub fn entry_def_index_from_id(&self, entry_def_id: EntryDefId) -> Option<EntryDefIndex> {
self.0
.iter()
.position(|entry_def| entry_def.id == entry_def_id)
.map(|u_size| EntryDefIndex(u_size as u8))
}
}
impl std::ops::Index<usize> for EntryDefs {
type Output = EntryDef;
fn index(&self, i: usize) -> &Self::Output {
&self.0[i]
}
}
impl IntoIterator for EntryDefs {
type Item = EntryDef;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl From<Vec<EntryDef>> for EntryDefs {
fn from(v: Vec<EntryDef>) -> Self {
Self(v)
}
}
impl From<Vec<EntryDef>> for EntryDefsCallbackResult {
fn from(v: Vec<EntryDef>) -> Self {
Self::Defs(v.into())
}
}
impl From<String> for EntryDefId {
fn from(s: String) -> Self {
Self::App(s)
}
}
impl From<&str> for EntryDefId {
fn from(s: &str) -> Self {
Self::App(s.to_string())
}
}