Skip to content
Merged
67 changes: 61 additions & 6 deletions src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ pub use self::{composite::*, fields::*, path::*, variant::*};
pub struct Type<T: Form = MetaForm> {
/// The unique path to the type. Can be empty for built-in types
#[serde(skip_serializing_if = "Path::is_empty", default)]
pub path: Path<T>,
path: Path<T>,
/// The generic type parameters of the type in use. Empty for non generic types
#[serde(rename = "params", skip_serializing_if = "Vec::is_empty", default)]
pub type_params: Vec<T::Type>,
type_params: Vec<T::Type>,
/// The actual type definition
#[serde(rename = "def")]
pub type_def: TypeDef<T>,
type_def: TypeDef<T>,
}

impl IntoCompact for Type {
Expand Down Expand Up @@ -103,6 +103,26 @@ impl Type {
}
}

impl<T> Type<T>
where
T: Form,
{
/// Returns the path of the type
pub fn path(&self) -> &Path<T> {
&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<T> {
&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(
Expand Down Expand Up @@ -177,10 +197,10 @@ pub enum TypeDefPrimitive {
#[serde(bound(serialize = "T::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))]
pub struct TypeDefArray<T: Form = MetaForm> {
/// 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::Type,
type_param: T::Type,
}

impl IntoCompact for TypeDefArray {
Expand All @@ -201,6 +221,21 @@ impl TypeDefArray {
}
}

impl<T> TypeDefArray<T>
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::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))]
Expand Down Expand Up @@ -237,13 +272,23 @@ impl TypeDefTuple {
}
}

impl<T> TypeDefTuple<T>
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::Type: Serialize", deserialize = "T::Type: DeserializeOwned"))]
pub struct TypeDefSequence<T: Form = MetaForm> {
/// The element type of the sequence type.
#[serde(rename = "type")]
pub type_param: T::Type,
type_param: T::Type,
}

impl IntoCompact for TypeDefSequence {
Expand Down Expand Up @@ -275,3 +320,13 @@ impl TypeDefSequence {
Self::new(MetaType::new::<T>())
}
}

impl<T> TypeDefSequence<T>
where
T: Form
{
/// Returns the element type of the sequence type.
pub fn type_param(&self) -> &T::Type {
&self.type_param
}
}
7 changes: 6 additions & 1 deletion src/ty/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
))]
pub struct Path<T: Form = MetaForm> {
/// The segments of the namespace.
pub segments: Vec<T::String>,
segments: Vec<T::String>,
}

impl<T> Default for Path<T>
Expand Down Expand Up @@ -110,6 +110,11 @@ impl<T> Path<T>
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()
Expand Down