diff --git a/concordium-cis2/src/lib.rs b/concordium-cis2/src/lib.rs index ffee52b9..4293a7ac 100644 --- a/concordium-cis2/src/lib.rs +++ b/concordium-cis2/src/lib.rs @@ -1065,14 +1065,10 @@ impl From for Receiver { } /// Additional information to include with a transfer. -#[derive(Debug, Serialize, Clone)] +#[derive(Debug, Serialize, Clone, SchemaType)] +#[concordium(transparent)] pub struct AdditionalData(#[concordium(size_length = 2)] Vec); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for AdditionalData { - fn get_type() -> schema::Type { schema::Type::ByteList(schema::SizeLength::U16) } -} - impl AdditionalData { /// Construct an AdditionalData containing no data. pub fn empty() -> Self { AdditionalData(Vec::new()) } @@ -1105,18 +1101,12 @@ pub struct Transfer { } /// The parameter type for the contract function `transfer`. -#[derive(Debug, Serialize, Clone)] +#[derive(Debug, Serialize, Clone, SchemaType)] +#[concordium(transparent)] pub struct TransferParams( #[concordium(size_length = 2)] pub Vec>, ); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for TransferParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(Transfer::::get_type())) - } -} - impl From>> for TransferParams { fn from(transfers: Vec>) -> Self { TransferParams(transfers) } } @@ -1150,16 +1140,10 @@ pub struct UpdateOperator { } /// The parameter type for the contract function `updateOperator`. -#[derive(Debug, Serialize, Clone)] +#[derive(Debug, Serialize, Clone, SchemaType)] +#[concordium(transparent)] pub struct UpdateOperatorParams(#[concordium(size_length = 2)] pub Vec); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for UpdateOperatorParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(UpdateOperator::get_type())) - } -} - /// A query for the balance of a given address for a given token. // Note: For the serialization to be derived according to the CIS2 // specification, the order of the fields cannot be changed. @@ -1174,33 +1158,21 @@ pub struct BalanceOfQuery { /// The parameter type for the contract function `balanceOf`. // Note: For the serialization to be derived according to the CIS2 // specification, the order of the fields cannot be changed. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct BalanceOfQueryParams { /// List of balance queries. #[concordium(size_length = 2)] pub queries: Vec>, } -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for BalanceOfQueryParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(BalanceOfQuery::::get_type())) - } -} - /// The response which is sent back when calling the contract function /// `balanceOf`. /// It consists of the list of results corresponding to the list of queries. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct BalanceOfQueryResponse(#[concordium(size_length = 2)] pub Vec); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for BalanceOfQueryResponse { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(A::get_type())) - } -} - impl From> for BalanceOfQueryResponse { fn from(results: Vec) -> Self { BalanceOfQueryResponse(results) } } @@ -1221,34 +1193,22 @@ pub struct OperatorOfQuery { } /// The parameter type for the contract function `operatorOf`. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct OperatorOfQueryParams { /// List of operatorOf queries. #[concordium(size_length = 2)] pub queries: Vec, } -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for OperatorOfQueryParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(OperatorOfQuery::get_type())) - } -} - /// The response which is sent back when calling the contract function /// `operatorOf`. /// It consists of the list of result in the same order and length as the /// queries in the parameter. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct OperatorOfQueryResponse(#[concordium(size_length = 2)] pub Vec); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for OperatorOfQueryResponse { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(bool::get_type())) - } -} - impl From> for OperatorOfQueryResponse { fn from(results: Vec) -> Self { OperatorOfQueryResponse(results) } } @@ -1260,33 +1220,21 @@ impl AsRef<[bool]> for OperatorOfQueryResponse { /// The parameter type for the contract function `tokenMetadata`. // Note: For the serialization to be derived according to the CIS2 // specification, the order of the fields cannot be changed. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct TokenMetadataQueryParams { /// List of balance queries. #[concordium(size_length = 2)] pub queries: Vec, } -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for TokenMetadataQueryParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(T::get_type())) - } -} - /// The response which is sent back when calling the contract function /// `tokenMetadata`. /// It consists of the list of results corresponding to the list of queries. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct TokenMetadataQueryResponse(#[concordium(size_length = 2)] pub Vec); -// Implemented manually to hide the newtype wrapper. -impl schema::SchemaType for TokenMetadataQueryResponse { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(MetadataUrl::get_type())) - } -} - impl From> for TokenMetadataQueryResponse { fn from(results: Vec) -> Self { TokenMetadataQueryResponse(results) } } @@ -1354,17 +1302,13 @@ impl<'a> StandardIdentifier<'a> { /// Consists of a string of ASCII characters up to a length of 255. /// /// See [StandardIdentifier] for the borrowed version. -#[derive(Debug, Serialize, PartialEq, Eq)] +#[derive(Debug, Serialize, PartialEq, Eq, SchemaType)] +#[concordium(transparent)] pub struct StandardIdentifierOwned { #[concordium(size_length = 1)] id: String, } -// This is implemented manually to flatten and simplify the schema. -impl schema::SchemaType for StandardIdentifierOwned { - fn get_type() -> schema::Type { schema::Type::String(schema::SizeLength::U8) } -} - impl StandardIdentifierOwned { /// Validate and construct a standard identifier. pub fn new(id: String) -> Result { @@ -1391,20 +1335,14 @@ impl StandardIdentifierOwned { } /// The parameter type for the contract function `supports`. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct SupportsQueryParams { /// The list of support queries. #[concordium(size_length = 2)] pub queries: Vec, } -// This is implemented manually to flatten and simplify the schema. -impl schema::SchemaType for SupportsQueryParams { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(StandardIdentifierOwned::get_type())) - } -} - /// The query result type for whether a smart contract supports a standard. // Note: For the serialization to be derived according to the CIS0 // specification, the order of the variants and their fields cannot be changed. @@ -1421,20 +1359,14 @@ pub enum SupportResult { /// The response which is sent back when calling the contract function /// `supports`. It consists of a list of results corresponding to the list of /// queries. -#[derive(Debug, Serialize)] +#[derive(Debug, Serialize, SchemaType)] +#[concordium(transparent)] pub struct SupportsQueryResponse { /// List of support results corresponding to the list of queries. #[concordium(size_length = 2)] pub results: Vec, } -// This is implemented manually to flatten and simplify the schema. -impl schema::SchemaType for SupportsQueryResponse { - fn get_type() -> schema::Type { - schema::Type::List(schema::SizeLength::U16, Box::new(SupportResult::get_type())) - } -} - impl From> for SupportsQueryResponse { fn from(results: Vec) -> Self { SupportsQueryResponse { diff --git a/concordium-contracts-common b/concordium-contracts-common index 05f30142..79f73e24 160000 --- a/concordium-contracts-common +++ b/concordium-contracts-common @@ -1 +1 @@ -Subproject commit 05f3014267624c1e841cf062b500014250588890 +Subproject commit 79f73e24a9b0d12a92fb40e4fe006415006582ed diff --git a/concordium-std/tests/derive-schema-type/success-transparent.rs b/concordium-std/tests/derive-schema-type/success-transparent.rs new file mode 100644 index 00000000..848c8a4e --- /dev/null +++ b/concordium-std/tests/derive-schema-type/success-transparent.rs @@ -0,0 +1,44 @@ +/// Test ensuring derivation works for newtype wrappers with transparent +use concordium_std::*; + +/// Simple struct +#[derive(SchemaType)] +#[concordium(transparent)] +struct MyStruct { + field: u32, +} + +/// Nested struct +#[derive(SchemaType)] +#[concordium(transparent)] +struct MyOtherStruct { + field: MyStruct, +} + +/// With field attributes +#[derive(SchemaType)] +#[concordium(transparent)] +struct MyCollection { + #[concordium(size_length = 1)] + field: Vec, +} + +fn main() { + { + let schema_type = ::get_type(); + assert_eq!(schema_type, schema::Type::U32); + } + + { + let schema_type = ::get_type(); + assert_eq!(schema_type, schema::Type::U32); + } + + { + let schema_type = ::get_type(); + assert_eq!( + schema_type, + schema::Type::List(schema::SizeLength::U8, Box::new(schema::Type::U32)) + ); + } +}