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}
74
75#[bitflags]
77#[repr(u16)]
78#[derive(
79 Copy,
80 Clone,
81 RuntimeDebug,
82 PartialEq,
83 Eq,
84 Encode,
85 Decode,
86 DecodeWithMemTracking,
87 MaxEncodedLen,
88 TypeInfo,
89 Serialize,
90 Deserialize,
91)]
92pub enum SchemaSetting {
93 AppendOnly,
96 SignatureRequired,
99}
100
101#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
103pub struct SchemaSettings(pub BitFlags<IntentSetting>);
104
105pub type IntentSetting = SchemaSetting;
107
108pub type IntentSettings = SchemaSettings;
110
111#[derive(
113 Copy,
114 Clone,
115 Encode,
116 Decode,
117 DecodeWithMemTracking,
118 PartialEq,
119 Debug,
120 TypeInfo,
121 Eq,
122 MaxEncodedLen,
123 Serialize,
124 Deserialize,
125)]
126pub enum SchemaStatus {
127 Active,
129 Deprecated,
131 Unsupported,
133}
134
135#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
137#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
138pub struct IntentGroupResponse {
139 pub intent_group_id: IntentGroupId,
141 pub intent_ids: Vec<IntentId>,
143}
144
145#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
147#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
148pub struct IntentResponse {
149 pub intent_id: IntentId,
151 pub payload_location: PayloadLocation,
153 pub settings: Vec<IntentSetting>,
155 pub schema_ids: Option<Vec<SchemaId>>,
157}
158
159#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
161#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
162pub struct SchemaResponse {
163 pub schema_id: SchemaId,
165 #[cfg_attr(feature = "std", serde(with = "as_string"))]
167 pub model: Vec<u8>,
168 pub model_type: ModelType,
170 pub payload_location: PayloadLocation,
172 pub settings: Vec<SchemaSetting>,
174}
175
176#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
178#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
179pub struct SchemaResponseV2 {
180 pub schema_id: SchemaId,
182 pub intent_id: IntentId,
184 #[cfg_attr(feature = "std", serde(with = "as_string"))]
186 pub model: Vec<u8>,
187 pub model_type: ModelType,
189 pub status: SchemaStatus,
191 pub payload_location: PayloadLocation,
193 pub settings: Vec<IntentSetting>,
195}
196
197impl Into<SchemaResponse> for SchemaResponseV2 {
198 fn into(self) -> SchemaResponse {
200 SchemaResponse {
201 schema_id: self.schema_id,
202 model: self.model,
203 model_type: self.model_type,
204 payload_location: self.payload_location,
205 settings: self.settings,
206 }
207 }
208}
209
210#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
212#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
213pub struct SchemaInfoResponse {
214 pub schema_id: SchemaId,
216 pub model_type: ModelType,
218 pub payload_location: PayloadLocation,
220 pub settings: Vec<IntentSetting>,
222 pub intent_id: IntentId,
224 pub status: SchemaStatus,
227}
228
229#[derive(
230 Copy,
231 Clone,
232 Encode,
233 Decode,
234 DecodeWithMemTracking,
235 PartialEq,
236 Debug,
237 TypeInfo,
238 Eq,
239 MaxEncodedLen,
240 Serialize,
241 Deserialize,
242)]
243pub enum MappedEntityIdentifier {
245 Intent(IntentId),
247 IntentGroup(IntentGroupId),
249}
250
251impl Default for MappedEntityIdentifier {
252 fn default() -> Self {
253 Self::Intent(Default::default())
254 }
255}
256
257#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
259#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
260pub struct NameLookupResponse {
261 pub name: Vec<u8>,
263 pub entity_id: MappedEntityIdentifier,
265}
266
267pub trait SchemaProvider<SchemaId> {
269 fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponseV2>;
271
272 fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse>;
274
275 fn get_intent_by_id(intent_id: IntentId) -> Option<IntentResponse>;
277}
278
279pub trait SchemaValidator<SchemaId> {
281 fn are_all_intent_ids_valid(intent_ids: &[IntentId]) -> bool;
283
284 #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
286 fn set_schema_count(n: SchemaId);
287
288 #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
290 fn set_intent_count(n: IntentId);
291}
292
293impl IntentSettings {
294 pub fn all_disabled() -> Self {
296 Self(BitFlags::EMPTY)
297 }
298 pub fn get_enabled(&self) -> BitFlags<IntentSetting> {
300 self.0
301 }
302 pub fn is_enabled(&self, grant: IntentSetting) -> bool {
304 self.0.contains(grant)
305 }
306 pub fn set(&mut self, grant: IntentSetting) {
308 self.0.insert(grant)
309 }
310 pub fn from(settings: BitFlags<IntentSetting>) -> Self {
312 Self(settings)
313 }
314}
315impl_codec_bitflags!(IntentSettings, u16, IntentSetting);
316
317impl From<Vec<IntentSetting>> for IntentSettings {
318 fn from(settings: Vec<IntentSetting>) -> Self {
320 Self(BitFlags::from_iter(settings))
321 }
322}
323
324#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
326#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
327pub struct SchemaVersionResponse {
328 #[cfg_attr(feature = "std", serde(with = "as_string"))]
330 pub schema_name: Vec<u8>,
331 pub schema_version: SchemaVersion,
333 pub schema_id: SchemaId,
335}
336
337#[cfg(test)]
338mod tests {
339 use super::*;
340
341 #[test]
342 fn intent_settings_when_disabled_has_no_enabled() {
343 let settings = IntentSettings::all_disabled();
344 assert_eq!(settings.get_enabled(), BitFlags::EMPTY);
345 }
346
347 #[test]
348 fn intent_settings_set_from_all_enabled_check() {
349 let settings = IntentSettings::from(BitFlags::ALL);
350 assert!(settings.is_enabled(IntentSetting::AppendOnly));
351 assert!(settings.is_enabled(IntentSetting::SignatureRequired));
352 }
353}