use crate::impl_codec_bitflags;
#[cfg(feature = "std")]
use crate::utils;
use enumflags2::{bitflags, BitFlags};
use parity_scale_codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use scale_info::{build::Fields, meta_type, Path, Type, TypeInfo, TypeParameter};
use serde::{Deserialize, Serialize};
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;
#[cfg(feature = "std")]
use utils::*;
pub type SchemaId = u16;
pub type SchemaVersion = u8;
#[derive(
Copy,
Clone,
Encode,
Decode,
PartialEq,
Debug,
TypeInfo,
Eq,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum ModelType {
AvroBinary,
Parquet,
}
#[derive(
Copy,
Clone,
Encode,
Decode,
PartialEq,
Debug,
TypeInfo,
Eq,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum PayloadLocation {
OnChain,
IPFS,
Itemized,
Paginated,
}
#[bitflags]
#[repr(u16)]
#[derive(
Copy,
Clone,
RuntimeDebug,
PartialEq,
Eq,
Encode,
Decode,
MaxEncodedLen,
TypeInfo,
Serialize,
Deserialize,
)]
pub enum SchemaSetting {
AppendOnly,
SignatureRequired,
}
#[derive(Clone, Copy, PartialEq, Eq, Default, RuntimeDebug)]
pub struct SchemaSettings(pub BitFlags<SchemaSetting>);
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
pub struct SchemaResponse {
pub schema_id: SchemaId,
#[cfg_attr(feature = "std", serde(with = "as_string"))]
pub model: Vec<u8>,
pub model_type: ModelType,
pub payload_location: PayloadLocation,
pub settings: Vec<SchemaSetting>,
}
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
pub struct SchemaInfoResponse {
pub schema_id: SchemaId,
pub model_type: ModelType,
pub payload_location: PayloadLocation,
pub settings: Vec<SchemaSetting>,
}
pub trait SchemaProvider<SchemaId> {
fn get_schema_by_id(schema_id: SchemaId) -> Option<SchemaResponse>;
fn get_schema_info_by_id(schema_id: SchemaId) -> Option<SchemaInfoResponse>;
}
pub trait SchemaValidator<SchemaId> {
fn are_all_schema_ids_valid(schema_ids: &Vec<SchemaId>) -> bool;
#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
fn set_schema_count(n: SchemaId);
}
impl SchemaSettings {
pub fn all_disabled() -> Self {
Self(BitFlags::EMPTY)
}
pub fn get_enabled(&self) -> BitFlags<SchemaSetting> {
self.0
}
pub fn is_enabled(&self, grant: SchemaSetting) -> bool {
self.0.contains(grant)
}
pub fn set(&mut self, grant: SchemaSetting) {
self.0.insert(grant)
}
pub fn from(settings: BitFlags<SchemaSetting>) -> Self {
Self(settings)
}
}
impl_codec_bitflags!(SchemaSettings, u16, SchemaSetting);
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Clone, Encode, Decode, PartialEq, Debug, TypeInfo, Eq)]
pub struct SchemaVersionResponse {
#[cfg_attr(feature = "std", serde(with = "as_string"))]
pub schema_name: Vec<u8>,
pub schema_version: SchemaVersion,
pub schema_id: SchemaId,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn schema_settings_when_disabled_has_no_enabled() {
let settings = SchemaSettings::all_disabled();
assert_eq!(settings.get_enabled(), BitFlags::EMPTY);
}
#[test]
fn schema_settings_set_from_all_enabled_check() {
let settings = SchemaSettings::from(BitFlags::ALL);
assert!(settings.is_enabled(SchemaSetting::AppendOnly));
assert!(settings.is_enabled(SchemaSetting::SignatureRequired));
}
}