diff --git a/runtime-modules/content-directory/src/mock.rs b/runtime-modules/content-directory/src/mock.rs index 24117b5eb6..152c6f362d 100644 --- a/runtime-modules/content-directory/src/mock.rs +++ b/runtime-modules/content-directory/src/mock.rs @@ -973,12 +973,12 @@ impl PropertyType { } pub fn single_text(text_max_len: TextMaxLength) -> PropertyType { - let text_type = SingleValuePropertyType(Type::::Text(text_max_len)); + let text_type = Type::::Text(text_max_len); PropertyType::::Single(text_type) } pub fn single_text_hash(text_hash_max_len: HashedTextMaxLength) -> PropertyType { - let text_type = SingleValuePropertyType(Type::::Hash(text_hash_max_len)); + let text_type = Type::::Hash(text_hash_max_len); PropertyType::::Single(text_type) } diff --git a/runtime-modules/content-directory/src/schema.rs b/runtime-modules/content-directory/src/schema.rs index 2c9b814cc9..c00861937d 100644 --- a/runtime-modules/content-directory/src/schema.rs +++ b/runtime-modules/content-directory/src/schema.rs @@ -64,6 +64,27 @@ impl Default for Type { } } +impl Type { + /// Ensure `Type` specific `TextMaxLengthConstraint` or `HashedTextMaxLengthConstraint` satisfied + pub fn ensure_property_type_size_is_valid(&self) -> Result<(), Error> { + if let Type::Text(text_max_len) = self { + ensure!( + *text_max_len <= T::TextMaxLengthConstraint::get(), + Error::::TextPropertyTooLong + ); + } + + if let Type::Hash(hashed_text_max_len) = self { + ensure!( + *hashed_text_max_len <= T::HashedTextMaxLengthConstraint::get(), + Error::::HashedTextPropertyTooLong + ); + } + + Ok(()) + } +} + /// Vector property type representation #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)] @@ -93,18 +114,9 @@ impl VecPropertyType { /// Ensure `Type` specific `TextMaxLengthConstraint` & `VecMaxLengthConstraint` satisfied fn ensure_property_type_size_is_valid(&self) -> Result<(), Error> { - if let Type::Text(text_max_len) = self.vec_type { - ensure!( - text_max_len <= T::TextMaxLengthConstraint::get(), - Error::::TextPropertyTooLong - ); - } - if let Type::Hash(hash_text_max_len) = self.vec_type { - ensure!( - hash_text_max_len <= T::HashedTextMaxLengthConstraint::get(), - Error::::HashedTextPropertyTooLong - ); - } + // Ensure Type specific TextMaxLengthConstraint or HashedTextMaxLengthConstraint satisfied + self.vec_type.ensure_property_type_size_is_valid()?; + ensure!( self.max_length <= T::VecMaxLengthConstraint::get(), Error::::VecPropertyTooLong @@ -121,57 +133,17 @@ impl VecPropertyType { } } -/// `Type` enum wrapper -#[cfg_attr(feature = "std", derive(Serialize, Deserialize))] -#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)] -pub struct SingleValuePropertyType(pub Type); - -impl Default for SingleValuePropertyType { - fn default() -> Self { - Self(Type::default()) - } -} - -impl SingleValuePropertyType { - /// Ensure `Type` specific `TextMaxLengthConstraint` or `HashedTextMaxLengthConstraint` satisfied - fn ensure_property_type_size_is_valid(&self) -> Result<(), Error> { - if let Type::Text(text_max_len) = self.0 { - ensure!( - text_max_len <= T::TextMaxLengthConstraint::get(), - Error::::TextPropertyTooLong - ); - } - - if let Type::Hash(hashed_text_max_len) = self.0 { - ensure!( - hashed_text_max_len <= T::HashedTextMaxLengthConstraint::get(), - Error::::HashedTextPropertyTooLong - ); - } - - Ok(()) - } -} - -impl Deref for SingleValuePropertyType { - type Target = Type; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -/// Enum, representing either `SingleValuePropertyType` or `VecPropertyType` +/// Enum, representing either `Type` or `VecPropertyType` #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)] pub enum PropertyType { - Single(SingleValuePropertyType), + Single(Type), Vector(VecPropertyType), } impl Default for PropertyType { fn default() -> Self { - Self::Single(SingleValuePropertyType::default()) + Self::Single(Type::default()) } } @@ -184,9 +156,9 @@ impl PropertyType { } } - fn as_vec_type(&self) -> Option<&VecPropertyType> { - if let PropertyType::Vector(single_value_property_type) = self { - Some(single_value_property_type) + pub fn as_vec_type(&self) -> Option<&VecPropertyType> { + if let PropertyType::Vector(vec_value_property_type) = self { + Some(vec_value_property_type) } else { None } diff --git a/runtime-modules/content-directory/src/tests/transaction.rs b/runtime-modules/content-directory/src/tests/transaction.rs index 31a87df670..3c463669da 100644 --- a/runtime-modules/content-directory/src/tests/transaction.rs +++ b/runtime-modules/content-directory/src/tests/transaction.rs @@ -7,8 +7,7 @@ fn transaction_success() { assert_ok!(create_simple_class(LEAD_ORIGIN, ClassType::Valid)); // Create single reference property - let property_type_reference = - SingleValuePropertyType(Type::Reference(FIRST_CLASS_ID, true)); + let property_type_reference = Type::Reference(FIRST_CLASS_ID, true); let property = Property::::with_name_and_type( PropertyNameLengthConstraint::get().max() as usize, PropertyType::Single(property_type_reference),