use super::{Command, Response};
use audit::*;
use serialization::deserialize;
use session::{Session, SessionError, SessionErrorKind::ProtocolError};
use {Adapter, CommandType};
pub fn get_command_audit_option<A: Adapter>(
session: &mut Session<A>,
command: CommandType,
) -> Result<AuditOption, SessionError> {
let command_audit_options = get_all_command_audit_options(session)?;
Ok(command_audit_options
.iter()
.find(|opt| opt.command_type() == command)
.map(|opt| opt.audit_option())
.unwrap_or(AuditOption::Off))
}
pub fn get_all_command_audit_options<A>(
session: &mut Session<A>,
) -> Result<Vec<AuditCommand>, SessionError>
where
A: Adapter,
{
let response = session.send_command(GetOptionCommand {
tag: AuditTag::Command,
})?;
Ok(deserialize(&response.0)?)
}
pub fn get_force_audit_option<A: Adapter>(
session: &mut Session<A>,
) -> Result<AuditOption, SessionError> {
let response = session.send_command(GetOptionCommand {
tag: AuditTag::Force,
})?;
ensure!(
response.0.len() == 1,
ProtocolError,
"expected 1-byte response, got {}",
response.0.len()
);
AuditOption::from_u8(response.0[0]).map_err(|e| err!(ProtocolError, e))
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct GetOptionCommand {
pub tag: AuditTag,
}
impl Command for GetOptionCommand {
type ResponseType = GetOptionResponse;
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct GetOptionResponse(pub(crate) Vec<u8>);
impl Response for GetOptionResponse {
const COMMAND_TYPE: CommandType = CommandType::GetOption;
}