pub trait Transport {
fn call(&mut self, opnum: u16, stub: &[u8]) -> std::result::Result<Vec<u8>, String>;
}
pub struct MockTransport {
pub script: Vec<(u16, Vec<u8>)>,
pub calls: Vec<(u16, Vec<u8>)>,
}
impl MockTransport {
pub fn new(script: Vec<(u16, Vec<u8>)>) -> Self {
MockTransport {
script,
calls: Vec::new(),
}
}
}
impl Transport for MockTransport {
fn call(&mut self, opnum: u16, stub: &[u8]) -> Result<Vec<u8>, String> {
self.calls.push((opnum, stub.to_vec()));
if self.script.is_empty() {
return Err(format!("no scripted response for opnum {opnum}"));
}
let (want, reply) = self.script.remove(0);
if want != opnum {
return Err(format!("opnum mismatch: got {opnum}, want {want}"));
}
Ok(reply)
}
}