1extern crate alloc;
2use crate::impl_codec_bitflags;
3#[cfg(feature = "std")]
4use crate::utils;
5use alloc::{vec, vec::Vec};
6use enumflags2::{bitflags, BitFlags};
7use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, MaxEncodedLen};
8use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
9use serde::{Deserialize, Serialize};
10use sp_runtime::RuntimeDebug;
11#[cfg(feature = "std")]
12use utils::*;
13
14pub type IntentGroupId = u16;
16
17pub type IntentId = u16;
19
20pub type SchemaId = u16;
22
23pub type SchemaVersion = u8;
26
27#[derive(
29 Copy,
30 Clone,
31 Encode,
32 Decode,
33 DecodeWithMemTracking,
34 PartialEq,
35 Debug,
36 TypeInfo,
37 Eq,
38 MaxEncodedLen,
39 Serialize,
40 Deserialize,
41)]
42pub enum ModelType {
43 AvroBinary,
45 Parquet,
47}
48
49#[derive(
51 Copy,
52 Clone,
53 Encode,
54 Decode,
55 DecodeWithMemTracking,
56 PartialEq,
57 Debug,
58 TypeInfo,
59 Eq,
60 MaxEncodedLen,
61 Serialize,
62 Deserialize,
63)]
64pub enum PayloadLocation {
65 OnChain,
67 IPFS,
69 Itemized,
71 Paginated,
73 OffChain,
75}
76
77#[bitflags]
79#[repr(u16)]
80#[derive(
81 Copy,
82 Clone,
83 RuntimeDebug,
84 PartialEq,
85 Eq,
86 Encode,
87 Decode,
88 DecodeWithMemTracking,
89 MaxEncodedLen,
90 TypeInfo,
91 Serialize,
92 Deserialize,
93)]
94pub enum SchemaSetting {
95 AppendOnly,
98 SignatureRequired,
101}
102
103#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
105pub struct SchemaSettings(pub BitFlags<IntentSetting>);
106
107pub type IntentSetting = SchemaSetting;
109
110pub type IntentSettings = SchemaSettings;
112
113#[derive(
115 Copy,
116 Clone,
117 Encode,
118 Decode,
119 DecodeWithMemTracking,
120 PartialEq,
121 Debug,
122 TypeInfo,
123 Eq,
124 MaxEncodedLen,
125 Serialize,
126 Deserialize,
127)]
128pub enum SchemaStatus {
129 Active,
131 Deprecated,
133 Unsupported,
135}
136
137#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
139#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
140pub struct IntentGroupResponse {
141 pub intent_group_id: IntentGroupId,
143 pub intent_ids: Vec<IntentId>,
145}
146
147#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
149#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
150pub struct IntentResponse {
151 pub intent_id: IntentId,
153 pub payload_location: PayloadLocation,
155 pub settings: Vec<IntentSetting>,
157 pub schema_ids: Option<Vec<SchemaId>>,
159}
160
161#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
163#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
164pub struct SchemaResponse {
165 pub schema_id: SchemaId,
167 #[cfg_attr(feature = "std", serde(with = "as_string"))]
169 pub model: Vec<u8>,
170 pub model_type: ModelType,
172 pub payload_location: PayloadLocation,
174 pub settings: Vec<SchemaSetting>,
176}
177
178#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
180#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
181pub struct SchemaResponseV2 {
182 pub schema_id: SchemaId,
184 pub intent_id: IntentId,
186 #[cfg_attr(feature = "std", serde(with = "as_string"))]
188 pub model: Vec<u8>,
189 pub model_type: ModelType,
191 pub status: SchemaStatus,
193 pub payload_location: PayloadLocation,
195 pub settings: Vec<IntentSetting>,
197}
198
199impl Into<SchemaResponse> for SchemaResponseV2 {
200 fn into(self) -> SchemaResponse {
202 SchemaResponse {
203 schema_id: self.schema_id,
204 model: self.model,
205 model_type: self.model_type,
206 payload_location: self.payload_location,
207 settings: self.settings,
208 }
209 }
210}
211
212#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
214#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
215pub struct SchemaInfoResponse {
216 pub schema_id: SchemaId,
218 pub model_type: ModelType,
220 pub payload_location: PayloadLocation,
222 pub settings: Vec<IntentSetting>,
224 pub intent_id: IntentId,
226 pub status: SchemaStatus,
229}
230
231#[derive(
232 Copy,
233 Clone,
234 Encode,
235 Decode,
236 DecodeWithMemTracking,
237 PartialEq,
238 Debug,
239 TypeInfo,
240 Eq,
241 MaxEncodedLen,
242 Serialize,
243 Deserialize,
244)]
245pub enum MappedEntityIdentifier {
247 Intent(IntentId),
249 IntentGroup(IntentGroupId),
251}
252
253impl Default for MappedEntityIdentifier {
254 fn default() -> Self {
255 Self::Intent(Default::default())
256 }
257}
258
259#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
261#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
262pub struct NameLookupResponse {
263 pub name: Vec<u8>,
265 pub entity_id: MappedEntityIdentifier,
267}
268
269pub trait SchemaProvider<SchemaId> {
271 fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponseV2>;
273
274 fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse>;
276
277 fn get_intent_by_id(intent_id: IntentId) -> Option<IntentResponse>;
279}
280
281pub trait SchemaValidator<SchemaId> {
283 fn are_all_intent_ids_valid(intent_ids: &[IntentId]) -> bool;
285
286 #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
288 fn set_schema_count(n: SchemaId);
289
290 #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
292 fn set_intent_count(n: IntentId);
293}
294
295impl IntentSettings {
296 pub fn all_disabled() -> Self {
298 Self(BitFlags::EMPTY)
299 }
300 pub fn get_enabled(&self) -> BitFlags<IntentSetting> {
302 self.0
303 }
304 pub fn is_enabled(&self, grant: IntentSetting) -> bool {
306 self.0.contains(grant)
307 }
308 pub fn set(&mut self, grant: IntentSetting) {
310 self.0.insert(grant)
311 }
312 pub fn from(settings: BitFlags<IntentSetting>) -> Self {
314 Self(settings)
315 }
316}
317impl_codec_bitflags!(IntentSettings, u16, IntentSetting);
318
319impl From<Vec<IntentSetting>> for IntentSettings {
320 fn from(settings: Vec<IntentSetting>) -> Self {
322 Self(BitFlags::from_iter(settings))
323 }
324}
325
326#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
328#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
329pub struct SchemaVersionResponse {
330 #[cfg_attr(feature = "std", serde(with = "as_string"))]
332 pub schema_name: Vec<u8>,
333 pub schema_version: SchemaVersion,
335 pub schema_id: SchemaId,
337}
338
339#[cfg(test)]
340mod tests {
341 use super::*;
342
343 #[test]
344 fn intent_settings_when_disabled_has_no_enabled() {
345 let settings = IntentSettings::all_disabled();
346 assert_eq!(settings.get_enabled(), BitFlags::EMPTY);
347 }
348
349 #[test]
350 fn intent_settings_set_from_all_enabled_check() {
351 let settings = IntentSettings::from(BitFlags::ALL);
352 assert!(settings.is_enabled(IntentSetting::AppendOnly));
353 assert!(settings.is_enabled(IntentSetting::SignatureRequired));
354 }
355}