use grift_arena::ArenaIndex;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PortId(pub usize);
impl PortId {
pub const STDIN: PortId = PortId(0);
pub const STDOUT: PortId = PortId(1);
pub const STDERR: PortId = PortId(2);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IoErrorKind {
Eof,
InvalidPort,
WriteFailed,
ReadFailed,
PortClosed,
Unsupported,
}
pub type IoResult<T> = Result<T, IoErrorKind>;
pub trait IoProvider {
fn read_char(&mut self, port: PortId) -> IoResult<char>;
fn peek_char(&mut self, port: PortId) -> IoResult<char>;
fn char_ready(&mut self, port: PortId) -> IoResult<bool>;
fn write_char(&mut self, port: PortId, c: char) -> IoResult<()>;
fn write_str(&mut self, port: PortId, s: &str) -> IoResult<()>;
fn flush(&mut self, port: PortId) -> IoResult<()>;
fn close_port(&mut self, port: PortId) -> IoResult<()>;
fn is_input_port(&self, port: PortId) -> bool;
fn is_output_port(&self, port: PortId) -> bool;
fn open_input_string(&mut self, _s: &str) -> IoResult<PortId> {
Err(IoErrorKind::Unsupported)
}
fn open_output_string(&mut self) -> IoResult<PortId> {
Err(IoErrorKind::Unsupported)
}
fn get_output_string(&self, _port: PortId) -> IoResult<&str> {
Err(IoErrorKind::Unsupported)
}
}
pub struct NullIoProvider;
impl IoProvider for NullIoProvider {
fn read_char(&mut self, _port: PortId) -> IoResult<char> {
Err(IoErrorKind::Unsupported)
}
fn peek_char(&mut self, _port: PortId) -> IoResult<char> {
Err(IoErrorKind::Unsupported)
}
fn char_ready(&mut self, _port: PortId) -> IoResult<bool> {
Err(IoErrorKind::Unsupported)
}
fn write_char(&mut self, _port: PortId, _c: char) -> IoResult<()> {
Ok(())
}
fn write_str(&mut self, _port: PortId, _s: &str) -> IoResult<()> {
Ok(())
}
fn flush(&mut self, _port: PortId) -> IoResult<()> {
Ok(())
}
fn close_port(&mut self, _port: PortId) -> IoResult<()> {
Ok(())
}
fn is_input_port(&self, _port: PortId) -> bool {
false
}
fn is_output_port(&self, _port: PortId) -> bool {
false
}
}
#[derive(Debug, Clone, Copy)]
pub struct DisplayPort {
pub value: ArenaIndex,
pub port: PortId,
}