Skip to content

Commit

Permalink
Merge pull request #198 from tauri-apps/feat/json-output2
Browse files Browse the repository at this point in the history
Feat: add `json` subcommand
  • Loading branch information
JonasKruckenberg authored Sep 15, 2023
2 parents c59ba49 + 8514300 commit b9660fa
Show file tree
Hide file tree
Showing 15 changed files with 1,173 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
with:
components: clippy
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --workspace -- -Dclippy::all -Dclippy::pedantic
- run: cargo clippy --workspace --features unstable -- -Dclippy::all -Dclippy::pedantic

rustfmt:
name: Rustfmt
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ serde = "1.0.188"
quote = "1.0.33"
proc-macro2 = "1.0.67"
syn = "2.0.33"
schemars = "0.8.12"
serde_json = "1.0"
tauri = "2.0.0-alpha.14"

[dependencies]
Expand All @@ -48,6 +50,7 @@ miette.workspace = true
env_logger = "0.10.0"
log.workspace = true
clap_complete = "4.4.1"
serde_json.workspace = true

[features]
unstable = ["dep:tauri-bindgen-gen-markdown"]
Expand Down
7 changes: 4 additions & 3 deletions crates/gen-guest-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ pub struct JavaScript {
impl JavaScript {
fn print_function(&self, intf_name: &str, func: &Function) -> String {
let docs = self.print_docs(func);
let ident = func.ident.to_lower_camel_case();
let name = func.ident.to_snake_case();
let ident = func.id.to_lower_camel_case();
let name = func.id.to_snake_case();
let params = print_function_params(&func.params);

let deserialize_result = func
Expand Down Expand Up @@ -109,7 +109,8 @@ export async function {ident} ({params}) {{
let docs = self.print_docs(func);
let mod_ident = mod_ident.to_snake_case();
let resource_ident = ident.to_snake_case();
let ident = func.ident.to_lower_camel_case();
let ident = func.id.to_lower_camel_case();

let params = print_function_params(&func.params);

let deserialize_result = func
Expand Down
4 changes: 2 additions & 2 deletions crates/gen-guest-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl RustWasm {
&BorrowMode::Owned,
);

let ident = func.ident.to_snake_case();
let ident = func.id.to_snake_case();

let param_idents = func
.params
Expand Down Expand Up @@ -157,7 +157,7 @@ impl RustGenerator for RustWasm {
);

let mod_ident = format!("{mod_ident}::resource::{}", ident.to_string().to_snake_case());
let ident = func.ident.to_snake_case();
let ident = func.id.to_snake_case();

let param_idents = func
.params
Expand Down
17 changes: 9 additions & 8 deletions crates/gen-guest-ts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl TypeScript {
pub fn print_function(&self, intf_name: &str, func: &Function) -> String {
let docs = print_docs(&func.docs);

let ident = func.ident.to_lower_camel_case();
let name = func.ident.to_snake_case();
let ident = func.id.to_lower_camel_case();
let name = func.id.to_snake_case();

let params = self.print_function_params(&func.params);

Expand Down Expand Up @@ -221,7 +221,7 @@ export async function {ident} ({params}) : {result} {{
.iter()
.map(|field| {
let docs = print_docs(&field.docs);
let ident = field.ident.to_lower_camel_case();
let ident = field.id.to_lower_camel_case();
let ty = self.print_type(&field.ty);

format!("{docs}\n{ident}: {ty},\n")
Expand All @@ -237,7 +237,7 @@ export async function {ident} ({params}) : {result} {{
.enumerate()
.map(|(i, field)| {
let docs = print_docs(&field.docs);
let ident = field.ident.to_upper_camel_case();
let ident = field.id.to_upper_camel_case();
let value: u64 = 2 << i;

format!("{docs}\n{ident} = {value},\n")
Expand All @@ -253,7 +253,7 @@ export async function {ident} ({params}) : {result} {{
.enumerate()
.map(|(i, case)| {
let docs = print_docs(&case.docs);
let case_ident = case.ident.to_upper_camel_case();
let case_ident = case.id.to_upper_camel_case();
let value = case
.ty
.as_ref()
Expand All @@ -271,7 +271,7 @@ export async function {ident} ({params}) : {result} {{
.iter()
.map(|case| {
let docs = print_docs(&case.docs);
let case_ident = case.ident.to_upper_camel_case();
let case_ident = case.id.to_upper_camel_case();

format!("{docs}\n{ident}{case_ident}")
})
Expand All @@ -286,7 +286,7 @@ export async function {ident} ({params}) : {result} {{
.iter()
.map(|case| {
let docs = print_docs(&case.docs);
let ident = case.ident.to_upper_camel_case();
let ident = case.id.to_upper_camel_case();

format!("{docs}\n{ident},\n")
})
Expand Down Expand Up @@ -321,9 +321,10 @@ export async function {ident} ({params}) : {result} {{
.iter()
.map(|func| {
let docs = print_docs(&func.docs);

let mod_ident = mod_ident.to_snake_case();
let resource_ident = ident.to_snake_case();
let ident = func.ident.to_lower_camel_case();
let ident = func.id.to_lower_camel_case();

let params = self.print_function_params(&func.params);
let result = func
Expand Down
4 changes: 2 additions & 2 deletions crates/gen-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ impl Host {
}

fn print_router_fn_definition(&self, mod_name: &str, func: &Function) -> TokenStream {
let func_name = func.ident.to_snake_case();
let func_name = func.id.to_snake_case();
let func_ident = format_ident!("{}", func_name);

let param_decl = match func.params.len() {
Expand Down Expand Up @@ -378,7 +378,7 @@ impl Host {
resource_name: &str,
method: &Function,
) -> TokenStream {
let func_name = method.ident.to_snake_case();
let func_name = method.id.to_snake_case();
let func_ident = format_ident!("{}", func_name);

let param_decl = method
Expand Down
12 changes: 6 additions & 6 deletions crates/gen-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub trait JavaScriptGenerator {
let fields = fields
.iter()
.map(|field| {
let ident = field.ident.to_lower_camel_case();
let ident = field.id.to_lower_camel_case();

format!("{ident}: {}", self.print_deserialize_ty(&field.ty))
})
Expand Down Expand Up @@ -177,7 +177,7 @@ pub trait JavaScriptGenerator {
.as_ref()
.map_or("null".to_string(), |ty| self.print_deserialize_ty(ty));

let ident = case.ident.to_upper_camel_case();
let ident = case.id.to_upper_camel_case();

format!(
"case {tag}:
Expand Down Expand Up @@ -205,7 +205,7 @@ pub trait JavaScriptGenerator {
.iter()
.enumerate()
.map(|(tag, case)| {
let ident = case.ident.to_upper_camel_case();
let ident = case.id.to_upper_camel_case();
format!(
"case {tag}:
return \"{ident}\"
Expand Down Expand Up @@ -346,7 +346,7 @@ pub trait JavaScriptGenerator {
fn print_serialize_record(&self, ident: &str, fields: &[RecordField]) -> String {
let inner = fields
.iter()
.map(|field| self.print_serialize_ty(&format!("val.{}", field.ident), &field.ty))
.map(|field| self.print_serialize_ty(&format!("val.{}", field.id), &field.ty))
.collect::<Vec<_>>()
.join(",\n");

Expand Down Expand Up @@ -378,7 +378,7 @@ pub trait JavaScriptGenerator {
.iter()
.enumerate()
.map(|(tag, case)| {
let prop_access = format!("val.{}", case.ident.to_upper_camel_case());
let prop_access = format!("val.{}", case.id.to_upper_camel_case());

let inner = case.ty.as_ref().map_or(String::new(), |ty| {
self.print_serialize_ty(&prop_access, ty)
Expand Down Expand Up @@ -409,7 +409,7 @@ pub trait JavaScriptGenerator {
.iter()
.enumerate()
.map(|(tag, case)| {
let ident = case.ident.to_upper_camel_case();
let ident = case.id.to_upper_camel_case();
format!(
"case \"{ident}\":
serializeU32(out, {tag})
Expand Down
16 changes: 6 additions & 10 deletions crates/gen-markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Markdown {
.map(|field| {
format!(
"#### {ident}: `{ty}`\n{docs}\n",
ident = field.ident,
ident = field.id,
ty = self.print_ty(&field.ty),
docs = field.docs
)
Expand All @@ -103,7 +103,7 @@ impl Markdown {
.map(|field| {
format!(
"#### {ident}\n{docs}\n",
ident = field.ident,
ident = field.id,
docs = field.docs
)
})
Expand All @@ -117,7 +117,7 @@ impl Markdown {
.map(|case| {
format!(
"#### {ident}{ty}\n{docs}\n",
ident = case.ident,
ident = case.id,
ty = case
.ty
.as_ref()
Expand All @@ -134,11 +134,7 @@ impl Markdown {
let cases = cases
.iter()
.map(|case| {
format!(
"#### {ident}\n{docs}\n",
ident = case.ident,
docs = case.docs
)
format!("#### {ident}\n{docs}\n", ident = case.id, docs = case.docs)
})
.collect::<String>();

Expand All @@ -164,7 +160,7 @@ impl Markdown {
.map(|func| {
format!(
"### Method {ident}\n\n`func {ident} ({params}){result}`\n\n{docs}",
ident = func.ident,
ident = func.id,
params = self.print_named_types(&func.params),
result = func
.result
Expand All @@ -184,7 +180,7 @@ impl Markdown {
fn print_function(&self, func: &Function) -> String {
format!(
"### Function {ident}\n\n` func {ident} ({params}){result}`\n\n{docs}",
ident = func.ident,
ident = func.id,
params = self.print_named_types(&func.params),
result = func
.result
Expand Down
10 changes: 5 additions & 5 deletions crates/gen-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ pub trait RustGenerator {
let borrow_attr = self
.needs_borrow(&field.ty, mode)
.then_some(quote! { #[serde(borrow)] });
let ident = format_ident!("{}", field.ident.to_snake_case());
let ident = format_ident!("{}", field.id.to_snake_case());
let ty = self.print_ty(&field.ty, mode);

quote! {
Expand All @@ -248,7 +248,7 @@ pub trait RustGenerator {
let fields = fields
.iter()
.enumerate()
.map(|(i, FlagsField { docs, ident })| {
.map(|(i, FlagsField { docs, id: ident })| {
let docs = self.print_docs(docs);
let ident = format_ident!("{}", ident.TO_SHOUTY_SNEK_CASE());
let i = Literal::usize_unsuffixed(i);
Expand Down Expand Up @@ -295,7 +295,7 @@ pub trait RustGenerator {

fn print_variant_case(&self, case: &VariantCase, mode: &BorrowMode) -> TokenStream {
let docs = self.print_docs(&case.docs);
let ident = format_ident!("{}", case.ident.to_upper_camel_case());
let ident = format_ident!("{}", case.id.to_upper_camel_case());

let payload = case.ty.as_ref().map(|ty| {
let ty = self.print_ty(ty, mode);
Expand Down Expand Up @@ -332,7 +332,7 @@ pub trait RustGenerator {

fn print_enum_case(&self, case: &EnumCase) -> TokenStream {
let docs = self.print_docs(&case.docs);
let ident = format_ident!("{}", case.ident.to_upper_camel_case());
let ident = format_ident!("{}", case.id.to_upper_camel_case());

quote! {
#docs
Expand Down Expand Up @@ -383,7 +383,7 @@ pub trait RustGenerator {
results_mode: &BorrowMode,
) -> TokenStream {
let docs = self.print_docs(&sig.func.docs);
let ident = format_ident!("{}", sig.func.ident.to_snake_case());
let ident = format_ident!("{}", sig.func.id.to_snake_case());

let pub_ = (!sig.private).then_some(quote! { pub });
let unsafe_ = sig.unsafe_.then_some(quote! { unsafe });
Expand Down
4 changes: 4 additions & 0 deletions crates/wit-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ id-arena = "2.2.1"
miette.workspace = true
distance = "0.4.0"
log.workspace = true
serde = { workspace = true, features = ["derive"] }
schemars.workspace = true

[dev-dependencies]
pretty_assertions = "1.4.0"
schemars = { version = "0.8.12", features = ["impl_json_schema"] }
serde_json.workspace = true
Loading

0 comments on commit b9660fa

Please sign in to comment.