Skip to content

Commit

Permalink
refactor copied code
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsDK committed Jul 18, 2023
1 parent e15f6ba commit db96f25
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 125 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ In simple scenarios you can use `map_err` to convert these errors to `String`s.
You can find an example using [thiserror](https://github.com/dtolnay/thiserror) [here](https://github.com/MatsDK/TauRPC/blob/main/example/src-tauri/src/main.rs).
You can also find more information about this in the [Tauri guides](https://tauri.app/v1/guides/features/command/#error-handling).

# Extra options for procedures

Inside your procedures trait you can add attributes to the defined methods. This can be used to ignore or rename a method. Renaming will change the name of the procedure on the frontend.

```rust
#[taurpc::procedures]
trait Api {
// #[taurpc(skip)]
#[taurpc(alias = "_hello_world_")]
async fn hello_world();
}
```

# Calling the frontend

Trigger [events](https://tauri.app/v1/guides/features/events/) on your TypeScript frontend from your Rust backend with a fully-typed experience.
Expand Down Expand Up @@ -212,7 +225,8 @@ trigger.send_to("main").hello_world()?;
- [x] Struct inputs
- [x] Sharing state
- [ ] Use Tauri's managed state?
- [ ] Renaming methods
- [x] Renaming methods
- [ ] Nested routes
- [ ] Merging routers
- [x] Custom error handling
- [x] Typed outputs
Expand Down
1 change: 1 addition & 0 deletions example/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async fn main() {
.send_to("main")
.update_state(String::from("test2"))?;

trigger.with_alias()?;
trigger.update_state(String::from("test"))?;
}

Expand Down
6 changes: 5 additions & 1 deletion example/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const call_backend = async () => {
await taurpc.update_state(value);
await taurpc.get_window();
await taurpc.method_with_alias()
await taurpc.method_with_alias();
// console.log("before sleep");
// await taurpc.with_sleep();
// console.log("after sleep");
Expand Down Expand Up @@ -35,6 +35,10 @@
on("update_state", (value) => {
console.log(value);
});
on("method_with_alias", () => {
console.log("method with alias called");
});
});
onDestroy(() => {
Expand Down
16 changes: 12 additions & 4 deletions taurpc/taurpc-macros/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{ext::IdentExt, spanned::Spanned, Ident, Pat, PatType};

pub(crate) fn parse_args(args: &Vec<PatType>, message: &Ident) -> syn::Result<Vec<TokenStream2>> {
args.iter().map(|arg| parse_arg(arg, message)).collect()
/// Generate the code that extracts and deserializes the args from the tauri message.
pub(crate) fn parse_args(
args: &Vec<PatType>,
message: &Ident,
proc_ident: &Ident,
) -> syn::Result<Vec<TokenStream2>> {
args.iter()
.map(|arg| parse_arg(arg, message, proc_ident))
.collect()
}

fn parse_arg(arg: &PatType, message: &Ident) -> syn::Result<TokenStream2> {
fn parse_arg(arg: &PatType, message: &Ident, proc_ident: &Ident) -> syn::Result<TokenStream2> {
let key = parse_arg_key(arg)?;

// catch self arguments that use FnArg::Typed syntax
Expand All @@ -17,9 +24,10 @@ fn parse_arg(arg: &PatType, message: &Ident) -> syn::Result<TokenStream2> {
));
}

// this way tauri knows how to deserialize the different types of the args
Ok(quote!(::tauri::command::CommandArg::from_command(
::tauri::command::CommandItem {
name: "placeholder",
name: #proc_ident,
key: #key,
message: &#message
}
Expand Down
10 changes: 6 additions & 4 deletions taurpc/taurpc-macros/src/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::extend_errors;
use proc_macro2::Ident;
use quote::format_ident;
use syn::{
parse::{Parse, ParseStream},
spanned::Spanned,
Attribute, Expr, LitStr, MetaNameValue, Token,
};

/// Attributes added on the procedures trait itself, `#[taurpc::procedures( ... )]`.
#[derive(Debug, Default)]
pub struct ProceduresAttrs {
pub event_trigger_ident: Option<Ident>,
Expand Down Expand Up @@ -44,9 +44,8 @@ impl Parse for ProceduresAttrs {
);
}

let event_trigger_ident = p.path.segments.last().unwrap();
result.event_trigger_ident =
Some(format_ident!("{}", event_trigger_ident.ident));
let ident = p.path.get_ident().unwrap();
result.event_trigger_ident = Some(ident.clone());
}
}
}
Expand All @@ -57,6 +56,9 @@ impl Parse for ProceduresAttrs {
}
}

/// Attributes defined on methods inside a procedures trait.
/// Parse the attributes to make sure they are defined in the correct way, like `#[taurpc( ... )]`, accumulate
/// all errors and then display them together with `extend_errors!()`.
#[derive(Default, Debug)]
pub struct MethodAttrs {
pub(crate) skip: bool,
Expand Down
Loading

0 comments on commit db96f25

Please sign in to comment.