fm_script_client/
result.rs1use serde::de::DeserializeOwned;
2use serde::ser::Error;
3use serde_json::Value;
4
5pub struct Void;
6
7pub trait ScriptResultDeserialize: Sized {
8 fn from_string(script_result: Option<String>) -> Result<Self, serde_json::Error>;
9 fn from_value(script_result: Option<Value>) -> Result<Self, serde_json::Error>;
10}
11
12impl<T: DeserializeOwned> ScriptResultDeserialize for T {
13 fn from_string(script_result: Option<String>) -> Result<Self, serde_json::Error> {
14 match script_result {
15 Some(string) => serde_json::from_str(&string),
16 None => Err(serde_json::Error::custom("Missing script result")),
17 }
18 }
19
20 fn from_value(script_result: Option<Value>) -> Result<Self, serde_json::Error> {
21 match script_result {
22 Some(value) => serde_json::from_value(value),
23 None => Err(serde_json::Error::custom("Missing script result")),
24 }
25 }
26}
27
28impl ScriptResultDeserialize for Void {
29 fn from_string(_script_result: Option<String>) -> Result<Self, serde_json::Error> {
30 Ok(Void)
31 }
32
33 fn from_value(_script_result: Option<Value>) -> Result<Self, serde_json::Error> {
34 Ok(Void)
35 }
36}