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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::element::daml_data::DamlData;
use crate::element::visitor::DamlElementVisitor;
#[cfg(feature = "full")]
use crate::element::DamlDefValue;
use crate::element::DamlVisitableElement;
use crate::element::{serialize, DamlType, DamlTypeVarWithKind};
use bounded_static::ToStatic;
use itertools::Itertools;
use serde::Serialize;
use std::borrow::Cow;
use std::collections::HashMap;
use std::iter::once;
const ROOT_MODULE_NAME: &str = "root";
#[derive(Debug, Serialize, Clone, ToStatic)]
pub struct DamlModule<'a> {
path: Vec<Cow<'a, str>>,
flags: DamlFeatureFlags,
synonyms: Vec<DamlDefTypeSyn<'a>>,
#[serde(serialize_with = "serialize::serialize_map")]
child_modules: HashMap<Cow<'a, str>, DamlModule<'a>>,
#[serde(serialize_with = "serialize::serialize_map")]
data_types: HashMap<Cow<'a, str>, DamlData<'a>>,
#[cfg(feature = "full")]
values: HashMap<Cow<'a, str>, DamlDefValue<'a>>,
}
impl<'a> DamlModule<'a> {
pub fn new_root() -> Self {
Self::new_empty(vec![])
}
pub fn new_leaf(
path: Vec<Cow<'a, str>>,
flags: DamlFeatureFlags,
synonyms: Vec<DamlDefTypeSyn<'a>>,
data_types: HashMap<Cow<'a, str>, DamlData<'a>>,
#[cfg(feature = "full")] values: HashMap<Cow<'a, str>, DamlDefValue<'a>>,
) -> Self {
Self {
path,
flags,
synonyms,
child_modules: HashMap::default(),
data_types,
#[cfg(feature = "full")]
values,
}
}
pub fn flags(&self) -> &DamlFeatureFlags {
&self.flags
}
pub fn synonyms(&self) -> &[DamlDefTypeSyn<'_>] {
&self.synonyms
}
pub fn path(&self) -> impl Iterator<Item = &str> {
self.path.iter().map(AsRef::as_ref)
}
pub fn child_modules(&self) -> impl Iterator<Item = &DamlModule<'a>> {
self.child_modules.values()
}
pub fn data_types(&self) -> impl Iterator<Item = &DamlData<'a>> {
self.data_types.values()
}
#[cfg(feature = "full")]
pub fn values(&self) -> impl Iterator<Item = &DamlDefValue<'a>> {
self.values.values()
}
pub fn is_root(&self) -> bool {
self.path.is_empty()
}
pub fn is_leaf(&self) -> bool {
self.child_modules.is_empty()
}
pub fn local_name(&self) -> &str {
self.path.last().map(AsRef::as_ref).map_or_else(|| ROOT_MODULE_NAME, AsRef::as_ref)
}
pub fn child_module<S: AsRef<str>>(&self, name: S) -> Option<&DamlModule<'_>> {
self.child_modules.get(name.as_ref())
}
pub fn child_module_path<'b, S: AsRef<str>>(&'a self, relative_path: &'b [S]) -> Option<&'a DamlModule<'a>> {
match relative_path {
[] => Some(self),
[head, tail @ ..] => match self.child_module(head.as_ref()) {
Some(m) => m.child_module_path(tail),
None => None,
},
}
}
pub fn data_type<S: AsRef<str>>(&self, name: S) -> Option<&DamlData<'a>> {
self.data_types.get(name.as_ref())
}
#[cfg(feature = "full")]
pub fn value<S: AsRef<str>>(&self, name: S) -> Option<&DamlDefValue<'a>> {
self.values.get(name.as_ref())
}
#[doc(hidden)]
pub(crate) fn child_module_or_new(&mut self, name: &'a str) -> &mut Self {
let path = &self.path;
self.child_modules.entry(Cow::from(name)).or_insert_with(|| {
DamlModule::new_empty(path.iter().map(ToOwned::to_owned).chain(once(Cow::from(name))).collect())
})
}
#[doc(hidden)]
pub(crate) fn take_from(&mut self, other: Self) {
debug_assert_eq!(self.path, other.path);
self.flags = other.flags;
self.data_types = other.data_types;
self.synonyms = other.synonyms;
#[cfg(feature = "full")]
{
self.values = other.values;
}
}
fn new_empty(path: Vec<Cow<'a, str>>) -> Self {
Self {
path,
flags: DamlFeatureFlags::default(),
synonyms: Vec::default(),
child_modules: HashMap::default(),
data_types: HashMap::default(),
#[cfg(feature = "full")]
values: HashMap::default(),
}
}
}
impl<'a> DamlVisitableElement<'a> for DamlModule<'a> {
fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor) {
visitor.pre_visit_module(self);
self.synonyms.iter().for_each(|syn| syn.accept(visitor));
if visitor.sort_elements() {
self.data_types.values().sorted_by_key(|ty| ty.name()).for_each(|data| data.accept(visitor));
self.child_modules.values().sorted_by_key(|&m| m.path.clone()).for_each(|module| module.accept(visitor));
} else {
self.data_types.values().for_each(|data| data.accept(visitor));
self.child_modules.values().for_each(|module| module.accept(visitor));
}
#[cfg(feature = "full")]
self.values.values().for_each(|value| value.accept(visitor));
visitor.post_visit_module(self);
}
}
#[derive(Debug, Serialize, Clone, ToStatic)]
pub struct DamlDefTypeSyn<'a> {
params: Vec<DamlTypeVarWithKind<'a>>,
ty: DamlType<'a>,
name: Vec<Cow<'a, str>>,
}
impl<'a> DamlDefTypeSyn<'a> {
pub fn new(params: Vec<DamlTypeVarWithKind<'a>>, ty: DamlType<'a>, name: Vec<Cow<'a, str>>) -> Self {
Self {
params,
ty,
name,
}
}
pub fn params(&self) -> &[DamlTypeVarWithKind<'_>] {
&self.params
}
pub fn ty(&self) -> &DamlType<'_> {
&self.ty
}
pub fn name(&self) -> impl Iterator<Item = &str> {
self.name.iter().map(AsRef::as_ref)
}
}
impl<'a> DamlVisitableElement<'a> for DamlDefTypeSyn<'a> {
fn accept(&'a self, visitor: &'a mut impl DamlElementVisitor) {
visitor.pre_visit_def_type_syn(self);
self.params.iter().for_each(|param| param.accept(visitor));
self.ty.accept(visitor);
visitor.post_visit_def_type_syn(self);
}
}
#[derive(Debug, Serialize, Copy, Clone, Default, ToStatic)]
pub struct DamlFeatureFlags {
forbid_party_literals: bool,
dont_divulge_contract_ids_in_create_arguments: bool,
dont_disclose_non_consuming_choices_to_observers: bool,
}
impl DamlFeatureFlags {
pub fn new(
forbid_party_literals: bool,
dont_divulge_contract_ids_in_create_arguments: bool,
dont_disclose_non_consuming_choices_to_observers: bool,
) -> Self {
Self {
forbid_party_literals,
dont_divulge_contract_ids_in_create_arguments,
dont_disclose_non_consuming_choices_to_observers,
}
}
pub fn forbid_party_literals(self) -> bool {
self.forbid_party_literals
}
pub fn dont_divulge_contract_ids_in_create_arguments(self) -> bool {
self.dont_divulge_contract_ids_in_create_arguments
}
pub fn dont_disclose_non_consuming_choices_to_observers(self) -> bool {
self.dont_disclose_non_consuming_choices_to_observers
}
}