Skip to content

Commit

Permalink
fix: improve handling of buffer type in JSON encoded structs, update …
Browse files Browse the repository at this point in the history
…xtp-bindgen version
  • Loading branch information
zshipko committed Sep 6, 2024
1 parent cd6ace1 commit d6e7e2b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 15 deletions.
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"typescript": "^5.3.2"
},
"dependencies": {
"@dylibso/xtp-bindgen": "1.0.0-rc.5",
"@dylibso/xtp-bindgen": "1.0.0-rc.7",
"ejs": "^3.1.10"
}
}
10 changes: 0 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ function makePublic(s: string) {
return "pub " + s;
}

function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1);
}

function camelToSnakeCase(s: string) {
return s.split(/(?=[A-Z])/).join("_").toLowerCase();
}

export function render() {
const tmpl = Host.inputString();
const ctx = {
Expand All @@ -96,8 +88,6 @@ export function render() {
toRustType,
makePublic,
jsonWrappedRustType,
capitalize,
camelToSnakeCase,
};

const output = ejs.render(tmpl, ctx);
Expand Down
2 changes: 2 additions & 0 deletions template/Cargo.toml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ extism-pdk = "1.1.0"
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
base64-serde = "0.7"
base64 = "0.21"
4 changes: 4 additions & 0 deletions template/src/pdk.rs.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ macro_rules! try_input_json {
}}
}

use base64_serde::base64_serde_type;

base64_serde_type!(Base64Standard, base64::engine::general_purpose::STANDARD);

<% schema.exports.forEach(ex => { -%>
Expand Down Expand Up @@ -74,6 +77,7 @@ pub struct <%- capitalize(schema.name) %> {
<% } -%>
#[serde(rename = "<%- p.name %>")]
<% if (p.nullable) { %>#[serde(default)]<% } %>
<% if (p.type === "buffer") { %> #[serde(with = "Base64Standard")] <% } %>
<%- makePublic(camelToSnakeCase(p.name)) %>: <%- p.nullable ? `Option<${toRustType(p)}>` : toRustType(p) %>,
<% }) %>
Expand Down

0 comments on commit d6e7e2b

Please sign in to comment.