From d3427d86abb5e8ad7d106ce62762df809fed2737 Mon Sep 17 00:00:00 2001 From: namesty Date: Tue, 30 May 2023 13:00:19 +0200 Subject: [PATCH 1/2] (feat): added annotation if Scalar type value is Bytes --- packages/schema/bind/src/bindings/rust/functions.ts | 10 ++++++++++ .../bindings/rust/plugin/templates/module-rs.mustache | 2 +- .../bindings/rust/plugin/templates/types-rs.mustache | 10 +++++----- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/schema/bind/src/bindings/rust/functions.ts b/packages/schema/bind/src/bindings/rust/functions.ts index e346469954..27eaee4ba7 100644 --- a/packages/schema/bind/src/bindings/rust/functions.ts +++ b/packages/schema/bind/src/bindings/rust/functions.ts @@ -1,5 +1,6 @@ import { isKeyword } from "./types"; import { MustacheFn } from "../types"; +import { ScalarDefinition } from "@polywrap/wrap-manifest-types-js"; function replaceAt(str: string, index: number, replacement: string): string { return ( @@ -314,6 +315,15 @@ export const serdeKeyword: MustacheFn = () => { }; }; +export const serdeAnnotateIfBytes = () => { + return (value: ScalarDefinition | undefined): string => { + if (value?.type === "Bytes") { + return `#[serde(with = "serde_bytes")]\n `; + } + return ""; + }; +}; + export const serdeRenameIfCaseMismatch: MustacheFn = () => { return (value: string, render: (template: string) => string): string => { const type = render(value); diff --git a/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache b/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache index 0c6e4a2269..76eae6b621 100644 --- a/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache +++ b/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache @@ -12,7 +12,7 @@ use super::types::*; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Args{{#toUpper}}{{name}}{{/toUpper}} { {{#arguments}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/arguments}} } diff --git a/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache b/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache index d5b2064fc2..6166f9b208 100644 --- a/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache +++ b/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache @@ -17,7 +17,7 @@ use std::sync::Arc; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/envType}} @@ -29,7 +29,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/objectTypes}} @@ -54,7 +54,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/importedObjectTypes}} @@ -66,7 +66,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/importedEnvType}} @@ -93,7 +93,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#toUpper}}{{parent.type}}{{/toUpper}}Args{{#toUpper}}{{name}}{{/toUpper}} { {{#arguments}} - {{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/arguments}} } From 23283874383fd097057a024563385eef44074cb9 Mon Sep 17 00:00:00 2001 From: namesty Date: Tue, 30 May 2023 13:13:00 +0200 Subject: [PATCH 2/2] (chore): bytes types now annotated with serde_bytes --- packages/schema/bind/src/bindings/rust/functions.ts | 9 +++++---- .../bindings/rust/plugin/templates/module-rs.mustache | 2 +- .../bindings/rust/plugin/templates/types-rs.mustache | 10 +++++----- .../cases/bind/sanity/output/plugin-rs/types.rs | 2 ++ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/schema/bind/src/bindings/rust/functions.ts b/packages/schema/bind/src/bindings/rust/functions.ts index 27eaee4ba7..aca46a07b5 100644 --- a/packages/schema/bind/src/bindings/rust/functions.ts +++ b/packages/schema/bind/src/bindings/rust/functions.ts @@ -1,6 +1,5 @@ import { isKeyword } from "./types"; import { MustacheFn } from "../types"; -import { ScalarDefinition } from "@polywrap/wrap-manifest-types-js"; function replaceAt(str: string, index: number, replacement: string): string { return ( @@ -315,9 +314,11 @@ export const serdeKeyword: MustacheFn = () => { }; }; -export const serdeAnnotateIfBytes = () => { - return (value: ScalarDefinition | undefined): string => { - if (value?.type === "Bytes") { +export const serdeAnnotateIfBytes: MustacheFn = () => { + return (value: string, render: (template: string) => string): string => { + const scalarType: string | undefined = render(value); + + if (scalarType === "Bytes") { return `#[serde(with = "serde_bytes")]\n `; } return ""; diff --git a/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache b/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache index 76eae6b621..568a5d64d6 100644 --- a/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache +++ b/packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache @@ -12,7 +12,7 @@ use super::types::*; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Args{{#toUpper}}{{name}}{{/toUpper}} { {{#arguments}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/arguments}} } diff --git a/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache b/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache index 6166f9b208..746eea972b 100644 --- a/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache +++ b/packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache @@ -17,7 +17,7 @@ use std::sync::Arc; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/envType}} @@ -29,7 +29,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/objectTypes}} @@ -54,7 +54,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/importedObjectTypes}} @@ -66,7 +66,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { {{#properties}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/properties}} } {{/importedEnvType}} @@ -93,7 +93,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} { #[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{#toUpper}}{{parent.type}}{{/toUpper}}Args{{#toUpper}}{{name}}{{/toUpper}} { {{#arguments}} - {{#serdeAnnotateIfBytes}}{{scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, + {{#serdeAnnotateIfBytes}}{{#scalar}}{{type}}{{/scalar}}{{/serdeAnnotateIfBytes}}{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}}, {{/arguments}} } diff --git a/packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs b/packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs index fe5864e98c..fe3c18556b 100644 --- a/packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs +++ b/packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs @@ -47,7 +47,9 @@ pub struct CustomType { pub json: JSON::Value, #[serde(rename = "optJson")] pub opt_json: Option, + #[serde(with = "serde_bytes")] pub bytes: Vec, + #[serde(with = "serde_bytes")] #[serde(rename = "optBytes")] pub opt_bytes: Option>, pub boolean: bool,