diff --git a/packages/schema/bind/src/bindings/rust/functions.ts b/packages/schema/bind/src/bindings/rust/functions.ts index e34646995..aca46a07b 100644 --- a/packages/schema/bind/src/bindings/rust/functions.ts +++ b/packages/schema/bind/src/bindings/rust/functions.ts @@ -314,6 +314,17 @@ export const serdeKeyword: MustacheFn = () => { }; }; +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 ""; + }; +}; + 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 0c6e4a226..568a5d64d 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}}{{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 d5b2064fc..746eea972 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}}{{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}} - {{#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}} - {{#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}} - {{#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}} - {{#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 fe5864e98..fe3c18556 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,