Skip to content

Commit

Permalink
preserve Rust docs in JS bindings (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy authored Oct 18, 2021
1 parent 777d3b2 commit 266a717
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 38 deletions.
6 changes: 1 addition & 5 deletions codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ const _lib = await Plug.prepare(opts, {
).join(", ")
} });
${
Object.keys(decl).map((def) =>
`export type ${def} = {
${typescript[def]}
}`
).join("\n")
Object.keys(decl).map((def) => typescript[def]).join("\n")
}
${
Object.keys(signature).map((sig) =>
Expand Down
56 changes: 27 additions & 29 deletions example/bindings/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,47 @@ import { Plug } from "https://deno.land/x/[email protected]/mod.ts";
const encode = (s: string) => new TextEncoder().encode(s);
const opts = {
name: "add",
url: "target/debug",
url: "target/debug"
};
const _lib = await Plug.prepare(opts, {
test_serde: { parameters: ["buffer", "usize"], result: "u8" },
test_mixed: { parameters: ["isize", "buffer", "usize"], result: "i32" },
add: { parameters: ["i32", "i32"], result: "i32" },
add2: { parameters: ["buffer", "usize"], result: "i32" },
test_mixed_order: {
parameters: ["i32", "buffer", "usize", "i32"],
result: "i32",
},
});
export type MyStruct = {
arr: Array<string>;
};
add: { parameters: [ "i32", "i32" ], result: "i32" }, test_mixed: { parameters: [ "isize", "buffer", "usize" ], result: "i32" }, test_serde: { parameters: [ "buffer", "usize" ], result: "u8" }, add2: { parameters: [ "buffer", "usize" ], result: "i32" }, test_mixed_order: { parameters: [ "i32", "buffer", "usize", "i32" ], result: "i32" } });
export type OptionStruct = {
maybe: string | undefined | null;
maybe: string | undefined | null;
};
/**
* Doc comment for `Input` struct.
* ...testing multiline
**/
export type Input = {
/**
* Doc comments get
* transformed to JS doc
* comments.
**/
a: number;
b: number;
};
export function test_serde(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0));
return _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number;
export type MyStruct = {
arr: Array<string>;
};
export function add(a0: number,a1: number) {

return _lib.symbols.add(a0, a1) as number
}
export function test_mixed(a0: number, a1: Input) {
export function test_mixed(a0: number,a1: Input) {
const a1_buf = encode(JSON.stringify(a1));
return _lib.symbols.test_mixed(a0, a1_buf, a1_buf.byteLength) as number;
return _lib.symbols.test_mixed(a0, a1_buf, a1_buf.byteLength) as number
}
export function add(a0: number, a1: number) {
return _lib.symbols.add(a0, a1) as number;
export function test_serde(a0: MyStruct) {
const a0_buf = encode(JSON.stringify(a0));
return _lib.symbols.test_serde(a0_buf, a0_buf.byteLength) as number
}
export function add2(a0: Input) {
const a0_buf = encode(JSON.stringify(a0));
return _lib.symbols.add2(a0_buf, a0_buf.byteLength) as number;
return _lib.symbols.add2(a0_buf, a0_buf.byteLength) as number
}
export function test_mixed_order(a0: number, a1: Input, a2: number) {
export function test_mixed_order(a0: number,a1: Input,a2: number) {
const a1_buf = encode(JSON.stringify(a1));
return _lib.symbols.test_mixed_order(
a0,
a1_buf,
a1_buf.byteLength,
a2,
) as number;
return _lib.symbols.test_mixed_order(a0, a1_buf, a1_buf.byteLength, a2) as number
}

5 changes: 5 additions & 0 deletions example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ fn add(a: i32, b: i32) -> i32 {
// Test Structs

#[deno_bindgen]
/// Doc comment for `Input` struct.
/// ...testing multiline
pub struct Input {
/// Doc comments get
/// transformed to JS doc
/// comments.
a: i32,
b: i32,
}
Expand Down
43 changes: 39 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn process_struct(
.as_ref()
.expect("Field without ident")
.to_string();

match field.ty {
syn::Type::Path(ref ty) => {
let segment = &ty.path.segments.first().unwrap();
Expand All @@ -305,16 +306,50 @@ fn process_struct(
_ => unimplemented!(),
};

typescript.push(format!(" {}: {};", ident, types_to_ts(&field.ty)));
let doc_str = get_docs(&field.attrs);
typescript.push(format!(
"{} {}: {};",
doc_str,
ident,
types_to_ts(&field.ty)
));
}

metadata.type_defs.insert(name.to_string(), fmap.clone());
metadata
.ts_types
.insert(name.to_string(), typescript.join("\n").to_string());

let doc_str = get_docs(&input.attrs);
let typescript = format!(
"{}export type {} = {{\n {}\n}};",
doc_str,
name,
typescript.join("\n")
);
metadata.ts_types.insert(name.to_string(), typescript);
Ok(fmap)
}

fn get_docs(attrs: &Vec<syn::Attribute>) -> String {
let mut doc: Vec<String> = vec![];
for attr in attrs {
if let Ok(syn::Meta::NameValue(meta)) = attr.parse_meta() {
if !meta.path.is_ident("doc") {
continue;
}
if let syn::Lit::Str(lit) = meta.lit {
doc.push(lit.value());
}
}
}

let doc_str = if doc.len() > 0 {
format!("/**\n *{}\n **/\n", doc.join("\n *"))
} else {
String::new()
};

doc_str
}

fn types_to_ts(ty: &syn::Type) -> String {
match ty {
syn::Type::Array(_) => String::from("any"),
Expand Down

0 comments on commit 266a717

Please sign in to comment.