use core::cmp::Ordering;
use core::fmt::{self, Debug, Display, Formatter};
use core::ops::Not;
use core::str::FromStr;
use crate::core::CoreExt;
pub trait Register: Copy + Ord + Debug + Display {
type Value: Copy + Debug + Display;
fn bytes(self) -> u16;
}
#[derive(Debug)]
pub enum NoRegs {}
#[allow(clippy::non_canonical_clone_impl)]
impl Clone for NoRegs {
fn clone(&self) -> Self { unreachable!() }
}
impl Copy for NoRegs {}
#[allow(clippy::non_canonical_clone_impl)]
impl PartialEq for NoRegs {
fn eq(&self, _: &Self) -> bool { unreachable!() }
}
impl Eq for NoRegs {}
impl Ord for NoRegs {
fn cmp(&self, _: &Self) -> Ordering { unreachable!() }
}
#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for NoRegs {
fn partial_cmp(&self, _: &Self) -> Option<Ordering> { unreachable!() }
}
impl Display for NoRegs {
fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { unreachable!() }
}
impl Register for NoRegs {
type Value = u8;
fn bytes(self) -> u16 { unreachable!() }
}
#[derive(Copy, Clone, Eq, PartialEq, Debug, Display)]
#[repr(i8)]
pub enum Status {
#[display("ok")]
Ok = 0,
#[display("fail")]
Fail = -1,
}
impl Status {
pub fn is_ok(self) -> bool { self == Status::Ok }
}
impl Not for Status {
type Output = Status;
fn not(self) -> Self::Output {
match self {
Status::Ok => Status::Fail,
Status::Fail => Status::Ok,
}
}
}
pub trait SiteId: Copy + Ord + Debug + Display + FromStr {}
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub struct Site<Id: SiteId> {
pub prog_id: Id,
pub offset: u16,
}
impl<Id: SiteId> Site<Id> {
#[inline]
pub fn new(prog_id: Id, offset: u16) -> Self { Self { prog_id, offset } }
}
impl<Id: SiteId> Display for Site<Id> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}@{:04}", self.prog_id, self.offset)
}
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct NoExt;
impl CoreExt for NoExt {
type Reg = NoRegs;
type Config = ();
fn with(_config: Self::Config) -> Self { NoExt }
fn get(&self, _reg: Self::Reg) -> Option<u8> { unreachable!() }
fn clr(&mut self, _reg: Self::Reg) { unreachable!() }
fn put(&mut self, _reg: Self::Reg, _val: Option<u8>) { unreachable!() }
fn reset(&mut self) {}
}