|
@@ -1,12 +1,15 @@
|
|
|
-/// Formats a function to be evaluated as callback.
|
|
|
-/// If the arg is a string literal, it needs the proper quotes.
|
|
|
+use serde::Serialize;
|
|
|
+use serde_json::Value as JsonValue;
|
|
|
+use std::fmt::Display;
|
|
|
+
|
|
|
+/// Formats a function name and argument to be evaluated as callback.
|
|
|
///
|
|
|
/// # Examples
|
|
|
/// ```
|
|
|
/// use tauri_api::rpc::format_callback;
|
|
|
/// // callback with a string argument
|
|
|
-/// // returns `window["callback-function-name"]("the string response")`
|
|
|
-/// format_callback("callback-function-name".to_string(), r#""the string response""#.to_string());
|
|
|
+/// let cb = format_callback("callback-function-name", "the string response");
|
|
|
+/// assert_eq!(cb, r#"window["callback-function-name"]("the string response")"#);
|
|
|
/// ```
|
|
|
///
|
|
|
/// ```
|
|
@@ -17,39 +20,50 @@
|
|
|
/// struct MyResponse {
|
|
|
/// value: String
|
|
|
/// }
|
|
|
-/// // this returns `window["callback-function-name"]({value: "some value"})`
|
|
|
-/// format_callback("callback-function-name".to_string(), serde_json::to_string(&MyResponse {
|
|
|
+/// let cb = format_callback("callback-function-name", serde_json::to_value(&MyResponse {
|
|
|
/// value: "some value".to_string()
|
|
|
-/// }).expect("failed to serialize type"));
|
|
|
+/// }).expect("failed to serialize"));
|
|
|
+/// assert_eq!(cb, r#"window["callback-function-name"]({"value":"some value"})"#);
|
|
|
/// ```
|
|
|
-pub fn format_callback(function_name: String, arg: String) -> String {
|
|
|
- let formatted_string = &format!("window[\"{}\"]({})", function_name, arg);
|
|
|
- formatted_string.to_string()
|
|
|
+pub fn format_callback<T: Into<JsonValue>, S: AsRef<str> + Display>(
|
|
|
+ function_name: S,
|
|
|
+ arg: T,
|
|
|
+) -> String {
|
|
|
+ format!(r#"window["{}"]({})"#, function_name, arg.into().to_string())
|
|
|
}
|
|
|
|
|
|
-/// Formats a Result type to its callback version.
|
|
|
+/// Formats a Result type to its Promise response.
|
|
|
/// Useful for Promises handling.
|
|
|
+/// If the Result `is_ok()`, the callback will be the `success_callback` function name and the argument will be the Ok value.
|
|
|
+/// If the Result `is_err()`, the callback will be the `error_callback` function name and the argument will be the Err value.
|
|
|
+///
|
|
|
+/// * `result` the Result to check
|
|
|
+/// * `success_callback` the function name of the Ok callback. Usually the `resolve` of the JS Promise.
|
|
|
+/// * `error_callback` the function name of the Err callback. Usually the `reject` of the JS Promise.
|
|
|
///
|
|
|
-/// If the Result is Ok, `format_callback` will be called directly.
|
|
|
-/// If the result is an Err, we assume the error message is a string, and quote it.
|
|
|
+/// Note that the callback strings are automatically generated by the `promisified` helper.
|
|
|
///
|
|
|
/// # Examples
|
|
|
/// ```
|
|
|
/// use tauri_api::rpc::format_callback_result;
|
|
|
-/// // returns `window["success_cb"](5)`
|
|
|
-/// format_callback_result(Ok("5".to_string()), "success_cb".to_string(), "error_cb".to_string());
|
|
|
-/// // returns `window["error_cb"]("error message here")`
|
|
|
-/// format_callback_result(Err("error message here".to_string()), "success_cb".to_string(), "error_cb".to_string());
|
|
|
+/// let res: Result<u8, &str> = Ok(5);
|
|
|
+/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
|
|
|
+/// assert_eq!(cb, r#"window["success_cb"](5)"#);
|
|
|
+///
|
|
|
+/// let res: Result<&str, &str> = Err("error message here");
|
|
|
+/// let cb = format_callback_result(res, "success_cb".to_string(), "error_cb".to_string()).expect("failed to format");
|
|
|
+/// assert_eq!(cb, r#"window["error_cb"]("error message here")"#);
|
|
|
/// ```
|
|
|
-pub fn format_callback_result(
|
|
|
- result: Result<String, String>,
|
|
|
- callback: String,
|
|
|
+pub fn format_callback_result<T: Serialize, E: Serialize>(
|
|
|
+ result: Result<T, E>,
|
|
|
+ success_callback: String,
|
|
|
error_callback: String,
|
|
|
-) -> String {
|
|
|
- match result {
|
|
|
- Ok(res) => format_callback(callback, res),
|
|
|
- Err(err) => format_callback(error_callback, format!("\"{}\"", err)),
|
|
|
- }
|
|
|
+) -> crate::Result<String> {
|
|
|
+ let rpc = match result {
|
|
|
+ Ok(res) => format_callback(success_callback, serde_json::to_value(res)?),
|
|
|
+ Err(err) => format_callback(error_callback, serde_json::to_value(err)?),
|
|
|
+ };
|
|
|
+ Ok(rpc)
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
@@ -62,16 +76,9 @@ mod test {
|
|
|
fn qc_formating(f: String, a: String) -> bool {
|
|
|
// can not accept empty strings
|
|
|
if f != "" && a != "" {
|
|
|
- // get length of function and argument
|
|
|
- let alen = &a.len();
|
|
|
- let flen = &f.len();
|
|
|
// call format callback
|
|
|
- let fc = format_callback(f, a);
|
|
|
- // get length of the resulting string
|
|
|
- let fclen = fc.len();
|
|
|
-
|
|
|
- // if formatted string equals the length of the argument and the function plus 12 then its correct.
|
|
|
- fclen == alen + flen + 12
|
|
|
+ let fc = format_callback(f.clone(), a.clone());
|
|
|
+ fc == format!(r#"window["{}"]({})"#, f, serde_json::Value::String(a))
|
|
|
} else {
|
|
|
true
|
|
|
}
|
|
@@ -80,33 +87,18 @@ mod test {
|
|
|
// check arbitrary strings in format_callback_result
|
|
|
#[quickcheck]
|
|
|
fn qc_format_res(result: Result<String, String>, c: String, ec: String) -> bool {
|
|
|
- // match on result to decide how to call the function.
|
|
|
- match result {
|
|
|
- // if ok, get length of result and callback strings.
|
|
|
- Ok(r) => {
|
|
|
- let rlen = r.len();
|
|
|
- let clen = c.len();
|
|
|
-
|
|
|
- // take the ok string from result and pass it into format_callback_result as an ok.
|
|
|
- let resp = format_callback_result(Ok(r), c, ec);
|
|
|
- // get response string length
|
|
|
- let reslen = resp.len();
|
|
|
+ let resp = format_callback_result(result.clone(), c.clone(), ec.clone())
|
|
|
+ .expect("failed to format callback result");
|
|
|
+ let (function, value) = match result {
|
|
|
+ Ok(v) => (c, v),
|
|
|
+ Err(e) => (ec, e),
|
|
|
+ };
|
|
|
|
|
|
- // if response string length equals result and callback length plus 12 characters then it is correct.
|
|
|
- reslen == rlen + clen + 12
|
|
|
- }
|
|
|
- // If Err, get length of Err and Error callback
|
|
|
- Err(err) => {
|
|
|
- let eclen = ec.len();
|
|
|
- let errlen = err.len();
|
|
|
- // pass err as Err into format_callback_result with callback and error callback
|
|
|
- let resp = format_callback_result(Err(err), c, ec);
|
|
|
- // get response string length
|
|
|
- let reslen = resp.len();
|
|
|
-
|
|
|
- // if length of response string equals the error length and the error callback length plus 14 characters then its is correct.
|
|
|
- reslen == eclen + errlen + 14
|
|
|
- }
|
|
|
- }
|
|
|
+ resp
|
|
|
+ == format!(
|
|
|
+ r#"window["{}"]({})"#,
|
|
|
+ function,
|
|
|
+ serde_json::Value::String(value),
|
|
|
+ )
|
|
|
}
|
|
|
}
|