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 SchemaId = u16;
16
17pub type SchemaVersion = u8;
19
20#[derive(
22 Copy,
23 Clone,
24 Encode,
25 Decode,
26 DecodeWithMemTracking,
27 PartialEq,
28 Debug,
29 TypeInfo,
30 Eq,
31 MaxEncodedLen,
32 Serialize,
33 Deserialize,
34)]
35pub enum ModelType {
36 AvroBinary,
38 Parquet,
40}
41
42#[derive(
44 Copy,
45 Clone,
46 Encode,
47 Decode,
48 DecodeWithMemTracking,
49 PartialEq,
50 Debug,
51 TypeInfo,
52 Eq,
53 MaxEncodedLen,
54 Serialize,
55 Deserialize,
56)]
57pub enum PayloadLocation {
58 OnChain,
60 IPFS,
62 Itemized,
64 Paginated,
66}
67
68#[bitflags]
70#[repr(u16)]
71#[derive(
72 Copy,
73 Clone,
74 RuntimeDebug,
75 PartialEq,
76 Eq,
77 Encode,
78 Decode,
79 DecodeWithMemTracking,
80 MaxEncodedLen,
81 TypeInfo,
82 Serialize,
83 Deserialize,
84)]
85pub enum SchemaSetting {
86 AppendOnly,
89 SignatureRequired,
92}
93
94#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
96pub struct SchemaSettings(pub BitFlags<SchemaSetting>);
97
98#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
100#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
101pub struct SchemaResponse {
102 pub schema_id: SchemaId,
104 #[cfg_attr(feature = "std", serde(with = "as_string"))]
106 pub model: Vec<u8>,
107 pub model_type: ModelType,
109 pub payload_location: PayloadLocation,
111 pub settings: Vec<SchemaSetting>,
113}
114
115#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
117#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
118pub struct SchemaInfoResponse {
119 pub schema_id: SchemaId,
121 pub model_type: ModelType,
123 pub payload_location: PayloadLocation,
125 pub settings: Vec<SchemaSetting>,
127}
128
129pub trait SchemaProvider<SchemaId> {
131 fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponse>;
133
134 fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse>;
136}
137
138pub trait SchemaValidator<SchemaId> {
140 fn are_all_schema_ids_valid(schema_ids: &[SchemaId]) -> bool;
142
143 #[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
145 fn set_schema_count(n: SchemaId);
146}
147
148impl SchemaSettings {
149 pub fn all_disabled() -> Self {
151 Self(BitFlags::EMPTY)
152 }
153 pub fn get_enabled(&self) -> BitFlags<SchemaSetting> {
155 self.0
156 }
157 pub fn is_enabled(&self, grant: SchemaSetting) -> bool {
159 self.0.contains(grant)
160 }
161 pub fn set(&mut self, grant: SchemaSetting) {
163 self.0.insert(grant)
164 }
165 pub fn from(settings: BitFlags<SchemaSetting>) -> Self {
167 Self(settings)
168 }
169}
170impl_codec_bitflags!(SchemaSettings, u16, SchemaSetting);
171
172#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
174#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
175pub struct SchemaVersionResponse {
176 #[cfg_attr(feature = "std", serde(with = "as_string"))]
178 pub schema_name: Vec<u8>,
179 pub schema_version: SchemaVersion,
181 pub schema_id: SchemaId,
183}
184
185#[cfg(test)]
186mod tests {
187 use super::*;
188
189 #[test]
190 fn schema_settings_when_disabled_has_no_enabled() {
191 let settings = SchemaSettings::all_disabled();
192 assert_eq!(settings.get_enabled(), BitFlags::EMPTY);
193 }
194
195 #[test]
196 fn schema_settings_set_from_all_enabled_check() {
197 let settings = SchemaSettings::from(BitFlags::ALL);
198 assert!(settings.is_enabled(SchemaSetting::AppendOnly));
199 assert!(settings.is_enabled(SchemaSetting::SignatureRequired));
200 }
201}