use arbitrary::Arbitrary;
use embedded_hal::pwm::{self, ErrorKind, ErrorType, SetDutyCycle};
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}
impl pwm::Error for Error {
fn kind(&self) -> ErrorKind {
self.kind
}
}
impl<'a> Arbitrary<'a> for Error {
fn arbitrary(_: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Error {
kind: ErrorKind::Other,
})
}
}
#[derive(Debug, Arbitrary)]
pub struct ArbitraryPwm {
max_duty_cycle: u16,
maybe_error: Vec<Result<(), Error>>,
}
impl ErrorType for ArbitraryPwm {
type Error = Error;
}
impl SetDutyCycle for ArbitraryPwm {
fn max_duty_cycle(&self) -> u16 {
self.max_duty_cycle
}
fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
if duty > self.max_duty_cycle {
return Err(Error {
kind: ErrorKind::Other,
});
}
if let Some(result) = self.maybe_error.pop() {
return result;
}
Ok(())
}
}