use std::sync::{Arc, Mutex};
use uuid::Uuid;
use super::{command, state::State, MockHsm};
use crate::{
command::Code,
connector::{self, Connection, ErrorKind::ConnectionFailed, Message},
};
pub struct MockConnection(Arc<Mutex<State>>);
impl MockConnection {
pub(super) fn new(hsm: &MockHsm) -> Self {
MockConnection(hsm.0.clone())
}
}
impl Connection for MockConnection {
fn send_message(&self, _uuid: Uuid, message: Message) -> Result<Message, connector::Error> {
let command = message
.parse()
.map_err(|e| err!(ConnectionFailed, "error parsing command: {}", e))?;
let mut state = self
.0
.lock()
.map_err(|e| err!(ConnectionFailed, "error obtaining state lock: {}", e))?;
match command.command_type {
Code::CreateSession => command::create_session(&mut state, &command),
Code::AuthenticateSession => command::authenticate_session(&mut state, &command),
Code::SessionMessage => command::session_message(&mut state, command),
unsupported => fail!(ConnectionFailed, "unsupported command: {:?}", unsupported),
}
.map(Message::from)
}
}