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
use std::io;

pub trait ResponseHandler: Clone {
    fn try_make_static_response_bytes(
        &mut self,
        service_name: &'static str,
        fn_name: &'static str,
        request_bytes: &[u8],
    ) -> io::Result<Option<Vec<u8>>>;

    fn parse_response_bytes(&mut self, response_bytes: &[u8]) -> io::Result<Option<usize>>;
}

#[derive(Clone)]
pub struct DefaultResponseHandler;

impl ResponseHandler for DefaultResponseHandler {
    fn try_make_static_response_bytes(
        &mut self,
        _service_name: &'static str,
        _fn_name: &'static str,
        _request_bytes: &[u8],
    ) -> io::Result<Option<Vec<u8>>> {
        Ok(None)
    }

    fn parse_response_bytes(&mut self, response_bytes: &[u8]) -> io::Result<Option<usize>> {
        Ok(Some(response_bytes.len()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::error;

    #[test]
    fn with_default_response_handler() -> Result<(), Box<dyn error::Error>> {
        let mut h = DefaultResponseHandler;

        assert_eq!(
            h.try_make_static_response_bytes("my_service", "my_fn", &b""[..])?,
            None
        );

        assert_eq!(h.parse_response_bytes(&b"foo"[..])?, Some(3));

        Ok(())
    }
}