use amplify::num::{u1, u2, u24, u3, u4, u5, u6, u7};
use super::LibId;
use crate::data::Number;
use crate::isa::{Instr, InstructionSet};
use crate::reg::NumericRegister;
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display("attempt to read or write outside of code segment (i.e. at position > 2^16)")]
#[cfg_attr(feature = "std", derive(Error))]
pub struct CodeEofError;
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)]
#[cfg_attr(feature = "std", derive(Error))]
#[display(doc_comments)]
pub enum WriteError {
#[from(CodeEofError)]
CodeNotFittingSegment,
DataExceedsLimit(usize),
DataNotFittingSegment,
LibAbsent(LibId),
}
mod private {
use super::super::Cursor;
pub trait Sealed {}
impl<'a, T, D> Sealed for Cursor<'a, T, D>
where
T: AsRef<[u8]>,
D: AsRef<[u8]>,
Self: 'a,
{
}
}
pub trait Read: private::Sealed {
fn pos(&self) -> u16;
fn seek(&mut self, byte_pos: u16) -> Result<u16, CodeEofError>;
fn is_eof(&self) -> bool;
fn peek_u8(&self) -> Result<u8, CodeEofError>;
fn read_bool(&mut self) -> Result<bool, CodeEofError>;
fn read_u1(&mut self) -> Result<u1, CodeEofError>;
fn read_u2(&mut self) -> Result<u2, CodeEofError>;
fn read_u3(&mut self) -> Result<u3, CodeEofError>;
fn read_u4(&mut self) -> Result<u4, CodeEofError>;
fn read_u5(&mut self) -> Result<u5, CodeEofError>;
fn read_u6(&mut self) -> Result<u6, CodeEofError>;
fn read_u7(&mut self) -> Result<u7, CodeEofError>;
fn read_u8(&mut self) -> Result<u8, CodeEofError>;
fn read_i8(&mut self) -> Result<i8, CodeEofError>;
fn read_u16(&mut self) -> Result<u16, CodeEofError>;
fn read_i16(&mut self) -> Result<i16, CodeEofError>;
fn read_u24(&mut self) -> Result<u24, CodeEofError>;
fn read_lib(&mut self) -> Result<LibId, CodeEofError>;
fn read_data(&mut self) -> Result<(&[u8], bool), CodeEofError>;
fn read_number(&mut self, reg: impl NumericRegister) -> Result<Number, CodeEofError>;
}
pub trait Write: private::Sealed {
fn write_bool(&mut self, data: bool) -> Result<(), WriteError>;
fn write_u1(&mut self, data: impl Into<u1>) -> Result<(), WriteError>;
fn write_u2(&mut self, data: impl Into<u2>) -> Result<(), WriteError>;
fn write_u3(&mut self, data: impl Into<u3>) -> Result<(), WriteError>;
fn write_u4(&mut self, data: impl Into<u4>) -> Result<(), WriteError>;
fn write_u5(&mut self, data: impl Into<u5>) -> Result<(), WriteError>;
fn write_u6(&mut self, data: impl Into<u6>) -> Result<(), WriteError>;
fn write_u7(&mut self, data: impl Into<u7>) -> Result<(), WriteError>;
fn write_u8(&mut self, data: impl Into<u8>) -> Result<(), WriteError>;
fn write_i8(&mut self, data: impl Into<i8>) -> Result<(), WriteError>;
fn write_u16(&mut self, data: impl Into<u16>) -> Result<(), WriteError>;
fn write_i16(&mut self, data: impl Into<i16>) -> Result<(), WriteError>;
fn write_u24(&mut self, data: impl Into<u24>) -> Result<(), WriteError>;
fn write_lib(&mut self, data: LibId) -> Result<(), WriteError>;
fn write_data(&mut self, bytes: impl AsRef<[u8]>) -> Result<(), WriteError>;
fn write_number(&mut self, reg: impl NumericRegister, value: Number) -> Result<(), WriteError>;
fn edit<F, E, S>(&mut self, pos: u16, editor: F) -> Result<(), E>
where
F: FnOnce(&mut Instr<S>) -> Result<(), E>,
E: From<CodeEofError>,
S: InstructionSet;
}