lrcall 0.1.5

lrcall is a Rust procedure call framework that is compatible with local and remote procedure calls.
Documentation
use crate::client::stub::Stub;
use crate::client::RpcError;
use crate::{context, RequestName, ServerError};
use std::collections::HashMap;
use std::hash::Hash;
use std::io;

/// A mock stub that returns user-specified responses.
pub struct Mock<Req, Resp> {
    responses: HashMap<Req, Resp>,
}

impl<Req, Resp> Mock<Req, Resp>
where
    Req: Eq + Hash,
{
    /// Returns a new mock, mocking the specified (request, response) pairs.
    pub fn new<const N: usize>(responses: [(Req, Resp); N]) -> Self {
        Self { responses: HashMap::from(responses) }
    }
}

impl<Req, Resp> Stub for Mock<Req, Resp>
where
    Req: Eq + Hash + RequestName,
    Resp: Clone,
{
    type Req = Req;
    type Resp = Resp;

    async fn call(&self, _: context::Context, request: Self::Req) -> Result<Resp, RpcError> {
        self.responses.get(&request).cloned().map(Ok).unwrap_or_else(|| {
            Err(RpcError::Server(ServerError {
                kind: io::ErrorKind::NotFound,
                detail: "mock (request, response) entry not found".into(),
            }))
        })
    }
}