1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
pub use serde_json::Value;
use codec::{ MethodCodec, MethodCall, MethodCallResult };

pub struct JsonMethodCodec;

impl MethodCodec for JsonMethodCodec {
    type R = Value;

    fn decode_method_call(buf: &[u8]) -> Option<MethodCall<Self::R>> {
        unsafe {
            let s = std::str::from_utf8_unchecked(buf);
            serde_json::from_str::<MethodCall<Self::R>>(s).ok()
        }
    }

    fn decode_envelope(buf: &[u8]) -> Option<MethodCallResult<Self::R>> {
        unsafe {
            let s = std::str::from_utf8_unchecked(buf);
            let json: Value = serde_json::from_str(s).unwrap();
            if let Value::Array(mut v) = json {
                if v.len() == 1 {
                    return Some(MethodCallResult::Ok(v.swap_remove(0)));
                } else if v.len() == 3 {
                    return Some(MethodCallResult::Err{
                        code: v[0].as_str().unwrap().to_owned(),
                        message: v[1].as_str().unwrap().to_owned(),
                        details: v.swap_remove(2)
                    });
                }
            } 
            error!("Invalid envelope: {}", s);
            None
        }
    }

    fn encode_method_call(v: &MethodCall<Self::R>) -> Vec<u8> {
        let json = json!(v);
        let s = serde_json::to_string(&json).unwrap();
        s.into_bytes()
    }

    fn encode_success_envelope(v: &Self::R) -> Vec<u8> {
        let json = json!([v]);
        let s = serde_json::to_string(&json).unwrap();
        s.into_bytes()
    }

    fn encode_error_envelope(code: &str, message: &str, v: &Self::R) -> Vec<u8> {
        let json = json!([code, message, v]);
        let s = serde_json::to_string(&json).unwrap();
        s.into_bytes()
    }

}