use crate::rpc::{Request, Result};
use ac_primitives::RpcParams;
use serde::de::DeserializeOwned;
use std::{collections::HashMap, sync::RwLock};
type MethodKey = String;
type SerializedValue = String;
#[derive(Debug)]
pub struct RpcClientMock {
pub state: RwLock<HashMap<MethodKey, SerializedValue>>,
}
impl RpcClientMock {
pub fn new(state: HashMap<MethodKey, SerializedValue>) -> Self {
Self { state: RwLock::new(state) }
}
pub fn update_entry(&self, key: MethodKey, value: SerializedValue) {
let mut lock = self.state.write().unwrap();
lock.insert(key, value);
}
}
#[maybe_async::maybe_async(?Send)]
impl Request for RpcClientMock {
async fn request<R: DeserializeOwned>(&self, method: &str, _params: RpcParams) -> Result<R> {
let lock = self.state.read().unwrap();
let response = lock.get(method).unwrap();
let deserialized_value: R = serde_json::from_str(response).unwrap();
Ok(deserialized_value)
}
}