Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
use crate::prelude::{
any::TypeId,
fmt::Debug,
string::String,
marker::PhantomData,
};

use crate::{
Expand All @@ -51,7 +51,7 @@ pub trait Form {
/// The type representing the type.
type Type: PartialEq + Eq + PartialOrd + Ord + Clone + Debug;
/// The string type.
type String: Serialize + PartialEq + Eq + PartialOrd + Ord + Clone + Debug;
type String: PartialEq + Eq + PartialOrd + Ord + Clone + Debug;
}

/// A meta meta-type.
Expand All @@ -76,9 +76,12 @@ impl Form for MetaForm {
///
/// `type String` is owned in order to enable decoding
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize, Debug)]
pub enum CompactForm {}
pub struct CompactForm<S = &'static str>(PhantomData<S>);

impl Form for CompactForm {
impl<S> Form for CompactForm<S>
where
S: PartialEq + Eq + PartialOrd + Ord + Clone + Debug,
{
type Type = UntrackedSymbol<TypeId>;
type String = String;
type String = S;
}
22 changes: 15 additions & 7 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
use crate::prelude::{
any::TypeId,
collections::BTreeMap,
fmt::Debug,
mem,
num::NonZeroU32,
string::ToString,
vec::Vec,
};

Expand All @@ -50,6 +50,7 @@ use scale::{
Encode,
};
use serde::{
de::DeserializeOwned,
Deserialize,
Serialize,
};
Expand All @@ -67,7 +68,7 @@ impl IntoCompact for &'static str {
type Output = <CompactForm as Form>::String;

fn into_compact(self, _registry: &mut Registry) -> Self::Output {
self.to_string()
self
}
}

Expand Down Expand Up @@ -201,8 +202,12 @@ impl Registry {

/// A read-only registry, to be used for decoding/deserializing
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Decode)]
pub struct RegistryReadOnly {
types: Vec<Type<CompactForm>>,
#[serde(bound(serialize = "S: Serialize", deserialize = "S: DeserializeOwned",))]
pub struct RegistryReadOnly<S = &'static str>
where
S: PartialEq + Eq + PartialOrd + Ord + Clone + Debug,
{
types: Vec<Type<CompactForm<S>>>,
}

impl From<Registry> for RegistryReadOnly {
Expand All @@ -213,14 +218,17 @@ impl From<Registry> for RegistryReadOnly {
}
}

impl RegistryReadOnly {
impl<S> RegistryReadOnly<S>
where
S: PartialEq + Eq + PartialOrd + Ord + Clone + Debug,
{
/// Returns the type definition for the given identifier, `None` if no type found for that ID.
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<CompactForm>> {
pub fn resolve(&self, id: NonZeroU32) -> Option<&Type<CompactForm<S>>> {
self.types.get((id.get() - 1) as usize)
}

/// Returns an iterator for all types paired with their associated NonZeroU32 identifier.
pub fn enumerate(&self) -> impl Iterator<Item = (NonZeroU32, &Type<CompactForm>)> {
pub fn enumerate(&self) -> impl Iterator<Item = (NonZeroU32, &Type<CompactForm<S>>)> {
self.types.iter().enumerate().map(|(i, ty)| {
let id = NonZeroU32::new(i as u32 + 1).expect("i + 1 > 0; qed");
(id, ty)
Expand Down
5 changes: 3 additions & 2 deletions test_suite/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use scale_info::{
form::CompactForm,
prelude::{
num::NonZeroU32,
string::String,
vec,
vec::Vec,
},
Expand Down Expand Up @@ -60,7 +61,7 @@ fn scale_encode_then_decode_to_readonly() {
let mut encoded = registry.encode();
let original_serialized = serde_json::to_value(registry).unwrap();

let readonly_decoded = RegistryReadOnly::decode(&mut &encoded[..]).unwrap();
let readonly_decoded = RegistryReadOnly::<String>::decode(&mut &encoded[..]).unwrap();
assert!(readonly_decoded
.resolve(NonZeroU32::new(1).unwrap())
.is_some());
Expand All @@ -76,7 +77,7 @@ fn json_serialize_then_deserialize_to_readonly() {

let original_serialized = serde_json::to_value(registry).unwrap();
// assert_eq!(original_serialized, serde_json::Value::Null);
let readonly_deserialized: RegistryReadOnly =
let readonly_deserialized: RegistryReadOnly<String> =
serde_json::from_value(original_serialized.clone()).unwrap();
assert!(readonly_deserialized
.resolve(NonZeroU32::new(1).unwrap())
Expand Down