1
1
use serde:: Serialize ;
2
2
use serde_json:: Value as JsonValue ;
3
- use std:: fmt:: Display ;
4
3
5
4
/// Formats a function name and argument to be evaluated as callback.
6
5
///
@@ -25,10 +24,7 @@ use std::fmt::Display;
25
24
/// }).expect("failed to serialize"));
26
25
/// assert!(cb.contains(r#"window["callback-function-name"]({"value":"some value"})"#));
27
26
/// ```
28
- pub fn format_callback < T : Into < JsonValue > , S : AsRef < str > + Display > (
29
- function_name : S ,
30
- arg : T ,
31
- ) -> String {
27
+ pub fn format_callback < T : Into < JsonValue > , S : AsRef < str > > ( function_name : S , arg : T ) -> String {
32
28
format ! (
33
29
r#"
34
30
if (window["{fn}"]) {{
@@ -37,7 +33,7 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
37
33
console.warn("[TAURI] Couldn't find callback id {fn} in window. This happens when the app is reloaded while Rust is running an asynchronous operation.")
38
34
}}
39
35
"# ,
40
- fn = function_name,
36
+ fn = function_name. as_ref ( ) ,
41
37
arg = arg. into( ) . to_string( )
42
38
)
43
39
}
@@ -57,17 +53,17 @@ pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
57
53
/// ```
58
54
/// use tauri_api::rpc::format_callback_result;
59
55
/// let res: Result<u8, &str> = Ok(5);
60
- /// let cb = format_callback_result(res, "success_cb".to_string() , "error_cb".to_string() ).expect("failed to format");
56
+ /// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
61
57
/// assert!(cb.contains(r#"window["success_cb"](5)"#));
62
58
///
63
59
/// let res: Result<&str, &str> = Err("error message here");
64
- /// let cb = format_callback_result(res, "success_cb".to_string() , "error_cb".to_string() ).expect("failed to format");
60
+ /// let cb = format_callback_result(res, "success_cb", "error_cb").expect("failed to format");
65
61
/// assert!(cb.contains(r#"window["error_cb"]("error message here")"#));
66
62
/// ```
67
63
pub fn format_callback_result < T : Serialize , E : Serialize > (
68
64
result : Result < T , E > ,
69
- success_callback : String ,
70
- error_callback : String ,
65
+ success_callback : impl AsRef < str > ,
66
+ error_callback : impl AsRef < str > ,
71
67
) -> crate :: Result < String > {
72
68
let rpc = match result {
73
69
Ok ( res) => format_callback ( success_callback, serde_json:: to_value ( res) ?) ,
0 commit comments