Skip to content

Commit

Permalink
Merge pull request #1775 from polywrap/namesty/byte-bindings
Browse files Browse the repository at this point in the history
Rust Plugin Bindings: Bytes
  • Loading branch information
dOrgJelli authored Jun 15, 2023
2 parents e2cb084 + 2328387 commit 1a46cb7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
11 changes: 11 additions & 0 deletions packages/schema/bind/src/bindings/rust/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -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}}
Expand All @@ -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}}
Expand All @@ -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}}
Expand All @@ -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}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ pub struct CustomType {
pub json: JSON::Value,
#[serde(rename = "optJson")]
pub opt_json: Option<JSON::Value>,
#[serde(with = "serde_bytes")]
pub bytes: Vec<u8>,
#[serde(with = "serde_bytes")]
#[serde(rename = "optBytes")]
pub opt_bytes: Option<Vec<u8>>,
pub boolean: bool,
Expand Down

0 comments on commit 1a46cb7

Please sign in to comment.