use eh1::pwm::{ErrorKind, ErrorType, SetDutyCycle};
use crate::{common::Generic, eh1::MockError};
#[derive(PartialEq, Clone, Debug)]
pub struct Transaction {
kind: TransactionKind,
err: Option<MockError>,
}
impl Transaction {
pub fn new(kind: TransactionKind) -> Transaction {
Transaction { kind, err: None }
}
pub fn max_duty_cycle(duty: u16) -> Transaction {
Transaction::new(TransactionKind::GetMaxDutyCycle(duty))
}
pub fn set_duty_cycle(duty: u16) -> Transaction {
Transaction::new(TransactionKind::SetDutyCycle(duty))
}
pub fn with_error(mut self, error: MockError) -> Self {
self.err = Some(error);
self
}
}
#[derive(PartialEq, Clone, Debug)]
pub enum TransactionKind {
GetMaxDutyCycle(u16),
SetDutyCycle(u16),
}
pub type Mock = Generic<Transaction>;
impl eh1::pwm::Error for MockError {
fn kind(&self) -> ErrorKind {
ErrorKind::Other
}
}
impl ErrorType for Mock {
type Error = MockError;
}
impl SetDutyCycle for Mock {
fn max_duty_cycle(&self) -> u16 {
let mut s = self.clone();
let Transaction { kind, err } = s.next().expect("no expectation for max_duty_cycle call");
assert_eq!(err, None, "error not supported by max_duty_cycle!");
match kind {
TransactionKind::GetMaxDutyCycle(duty) => duty,
other => panic!("expected max_duty_cycle, got {:?}", other),
}
}
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
let Transaction { kind, err } =
self.next().expect("no expectation for set_duty_cycle call");
assert_eq!(
kind,
TransactionKind::SetDutyCycle(duty),
"expected set_duty_cycle"
);
if let Some(e) = err {
Err(e)
} else {
Ok(())
}
}
}