diff --git a/Cargo.toml b/Cargo.toml index 37a028e3..749b0f0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"] scale-info-derive = { version = "0.2.0", path = "derive", default-features = false, optional = true } serde = { version = "1", default-features = false, features = ["derive", "alloc"] } derive_more = { version = "0.99.1", default-features = false, features = ["from"] } -scale = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] } [features] default = ["std"] diff --git a/README.md b/README.md index fa4e6e5b..44bbcc82 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ pub struct Type { /// The unique path to the type. Can be empty for built-in types path: Path, /// The generic type parameters of the type in use. Empty for non generic types - type_params: Vec, + type_params: Vec, /// The actual type definition type_def: TypeDef, } diff --git a/src/form.rs b/src/form.rs index 10902f3e..85d85e1a 100644 --- a/src/form.rs +++ b/src/form.rs @@ -40,8 +40,8 @@ use serde::Serialize; /// instantiated out of the flux and compact forms that require some sort of /// interning data structures. pub trait Form { - /// The type identifier type. - type TypeId: PartialEq + Eq + PartialOrd + Ord + Clone + core::fmt::Debug; + /// The type representing the type. + type Type: PartialEq + Eq + PartialOrd + Ord + Clone + core::fmt::Debug; /// The string type. type String: Serialize + PartialEq + Eq + PartialOrd + Ord + Clone + core::fmt::Debug; } @@ -54,7 +54,7 @@ pub trait Form { pub enum MetaForm {} impl Form for MetaForm { - type TypeId = MetaType; + type Type = MetaType; type String = &'static str; } @@ -71,6 +71,6 @@ impl Form for MetaForm { pub enum CompactForm {} impl Form for CompactForm { - type TypeId = UntrackedSymbol; + type Type = UntrackedSymbol; type String = String; } diff --git a/src/interner.rs b/src/interner.rs index a3b811cc..782f5352 100644 --- a/src/interner.rs +++ b/src/interner.rs @@ -31,7 +31,8 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[serde(transparent)] pub struct UntrackedSymbol { - id: NonZeroU32, + /// The index to the symbol in the interner table. + pub id: NonZeroU32, #[serde(skip)] marker: PhantomData T>, } diff --git a/src/registry.rs b/src/registry.rs index 391edbce..91648a8a 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -181,6 +181,14 @@ pub struct RegistryReadOnly { types: Vec>, } +impl From for RegistryReadOnly { + fn from(registry: Registry) -> Self { + RegistryReadOnly { + types: registry.types.values().cloned().collect::>(), + } + } +} + impl RegistryReadOnly { /// Returns the type definition for the given identifier, `None` if no type found for that ID. pub fn resolve(&self, id: NonZeroU32) -> Option<&Type> { diff --git a/src/ty/composite.rs b/src/ty/composite.rs index 0de269e8..cbe3b790 100644 --- a/src/ty/composite.rs +++ b/src/ty/composite.rs @@ -50,11 +50,12 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] #[serde(rename_all = "lowercase")] pub struct TypeDefComposite { + /// The fields of the composite type. #[serde(skip_serializing_if = "Vec::is_empty", default)] fields: Vec>, } @@ -80,3 +81,13 @@ impl TypeDefComposite { } } } + +impl TypeDefComposite +where + T: Form, +{ + /// Returns the fields of the composite type. + pub fn fields(&self) -> &[Field] { + &self.fields + } +} diff --git a/src/ty/fields.rs b/src/ty/fields.rs index ea9d1d7f..1ac29bda 100644 --- a/src/ty/fields.rs +++ b/src/ty/fields.rs @@ -28,8 +28,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// This can be a named field of a struct type or a struct variant. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] pub struct Field { /// The name of the field. None for unnamed fields. @@ -37,7 +37,7 @@ pub struct Field { name: Option, /// The type of the field. #[serde(rename = "type")] - ty: T::TypeId, + ty: T::Type, } impl IntoCompact for Field { @@ -94,3 +94,18 @@ impl Field { Self::new(None, MetaType::new::()) } } + +impl Field +where + T: Form, +{ + /// Returns the name of the field. None for unnamed fields. + pub fn name(&self) -> Option<&T::String> { + self.name.as_ref() + } + + /// Returns the type of the field. + pub fn ty(&self) -> &T::Type { + &self.ty + } +} diff --git a/src/ty/mod.rs b/src/ty/mod.rs index 48fdb13f..7ee9478a 100644 --- a/src/ty/mod.rs +++ b/src/ty/mod.rs @@ -33,8 +33,8 @@ pub use self::{composite::*, fields::*, path::*, variant::*}; /// A [`Type`] definition with optional metadata. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] pub struct Type { /// The unique path to the type. Can be empty for built-in types @@ -42,7 +42,7 @@ pub struct Type { path: Path, /// The generic type parameters of the type in use. Empty for non generic types #[serde(rename = "params", skip_serializing_if = "Vec::is_empty", default)] - type_params: Vec, + type_params: Vec, /// The actual type definition #[serde(rename = "def")] type_def: TypeDef, @@ -103,11 +103,31 @@ impl Type { } } +impl Type +where + T: Form, +{ + /// Returns the path of the type + pub fn path(&self) -> &Path { + &self.path + } + + /// Returns the generic type parameters of the type + pub fn type_params(&self) -> &[T::Type] { + &self.type_params + } + + /// Returns the definition of the type + pub fn type_def(&self) -> &TypeDef { + &self.type_def + } +} + /// The possible types a SCALE encodable Rust value could have. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, From, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] #[serde(rename_all = "camelCase")] pub enum TypeDef { @@ -174,13 +194,13 @@ pub enum TypeDefPrimitive { /// An array type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound(serialize = "T::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))] pub struct TypeDefArray { /// The length of the array type. - pub len: u32, + len: u32, /// The element type of the array type. #[serde(rename = "type")] - pub type_param: T::TypeId, + type_param: T::Type, } impl IntoCompact for TypeDefArray { @@ -201,13 +221,29 @@ impl TypeDefArray { } } +#[allow(clippy::len_without_is_empty)] +impl TypeDefArray +where + T: Form, +{ + /// Returns the length of the array type. + pub fn len(&self) -> u32 { + self.len + } + + /// Returns the element type of the array type. + pub fn type_param(&self) -> &T::Type { + &self.type_param + } +} + /// A type to refer to tuple types. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound(serialize = "T::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))] #[serde(transparent)] pub struct TypeDefTuple { /// The types of the tuple fields. - fields: Vec, + fields: Vec, } impl IntoCompact for TypeDefTuple { @@ -237,13 +273,23 @@ impl TypeDefTuple { } } +impl TypeDefTuple +where + T: Form, +{ + /// Returns the types of the tuple fields. + pub fn fields(&self) -> &[T::Type] { + &self.fields + } +} + /// A type to refer to a sequence of elements of the same type. #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode, Debug)] -#[serde(bound(serialize = "T::TypeId: Serialize", deserialize = "T::TypeId: DeserializeOwned"))] +#[serde(bound(serialize = "T::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))] pub struct TypeDefSequence { /// The element type of the sequence type. #[serde(rename = "type")] - type_param: T::TypeId, + type_param: T::Type, } impl IntoCompact for TypeDefSequence { @@ -275,3 +321,13 @@ impl TypeDefSequence { Self::new(MetaType::new::()) } } + +impl TypeDefSequence +where + T: Form, +{ + /// Returns the element type of the sequence type. + pub fn type_param(&self) -> &T::Type { + &self.type_param + } +} diff --git a/src/ty/path.rs b/src/ty/path.rs index f934de84..89c74da7 100644 --- a/src/ty/path.rs +++ b/src/ty/path.rs @@ -31,8 +31,8 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Serialize, Deserialize, Encode, Decode)] #[serde(transparent)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] pub struct Path { /// The segments of the namespace. @@ -110,6 +110,11 @@ impl Path where T: Form, { + /// Returns the segments of the Path + pub fn segments(&self) -> &[T::String] { + &self.segments + } + /// Returns `true` if the path is empty pub fn is_empty(&self) -> bool { self.segments.is_empty() diff --git a/src/ty/variant.rs b/src/ty/variant.rs index eca6fe34..0f9515d2 100644 --- a/src/ty/variant.rs +++ b/src/ty/variant.rs @@ -63,11 +63,12 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, From, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] #[serde(rename_all = "lowercase")] pub struct TypeDefVariant { + /// The variants of a variant type #[serde(skip_serializing_if = "Vec::is_empty", default)] variants: Vec>, } @@ -94,6 +95,16 @@ impl TypeDefVariant { } } +impl TypeDefVariant +where + T: Form, +{ + /// Returns the variants of a variant type + pub fn variants(&self) -> &[Variant] { + &self.variants + } +} + /// A struct enum variant with either named (struct) or unnamed (tuple struct) /// fields. /// @@ -111,13 +122,13 @@ impl TypeDefVariant { /// ``` #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Debug, Serialize, Deserialize, Encode, Decode)] #[serde(bound( - serialize = "T::TypeId: Serialize, T::String: Serialize", - deserialize = "T::TypeId: DeserializeOwned, T::String: DeserializeOwned" + serialize = "T::Type: Serialize, T::String: Serialize", + deserialize = "T::Type: DeserializeOwned, T::String: DeserializeOwned" ))] pub struct Variant { - /// The name of the struct variant. + /// The name of the variant. name: T::String, - /// The fields of the struct variant. + /// The fields of the variant. #[serde(skip_serializing_if = "Vec::is_empty", default)] fields: Vec>, /// The discriminant of the variant. @@ -162,3 +173,23 @@ impl Variant { } } } + +impl Variant +where + T: Form, +{ + /// Returns the name of the variant + pub fn name(&self) -> &T::String { + &self.name + } + + /// Returns the fields of the struct variant. + pub fn fields(&self) -> &[Field] { + &self.fields + } + + /// Returns the discriminant of the variant. + pub fn discriminant(&self) -> Option { + self.discriminant + } +} diff --git a/test_suite/Cargo.toml b/test_suite/Cargo.toml index 830b5e15..87038fb1 100644 --- a/test_suite/Cargo.toml +++ b/test_suite/Cargo.toml @@ -12,7 +12,7 @@ license = "Apache-2.0" [dependencies] scale-info = { path = "..", features = ["derive"] } -scale = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } +scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] } serde = "1.0" serde_json = "1.0" pretty_assertions = "0.6.1" diff --git a/test_suite/derive_tests_no_std/Cargo.toml b/test_suite/derive_tests_no_std/Cargo.toml index de28ea8e..80622db3 100644 --- a/test_suite/derive_tests_no_std/Cargo.toml +++ b/test_suite/derive_tests_no_std/Cargo.toml @@ -11,7 +11,7 @@ license = "Apache-2.0" [dependencies] scale-info = { path = "../..", default-features = false, features = ["derive"] } -scale = { package = "parity-scale-codec", version = "1.3.4", features = ["full"] } +scale = { package = "parity-scale-codec", version = "1.3", features = ["full"] } libc = { version = "0.2", default-features = false }