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 is_port_open(&self, _port: PortId) -> bool {
true
}
fn is_textual_port(&self, port: PortId) -> bool {
self.is_input_port(port) || self.is_output_port(port)
}
fn is_binary_port(&self, _port: PortId) -> bool {
false
}
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)
}
fn file_exists(&self, _path: &str) -> IoResult<bool> {
Err(IoErrorKind::Unsupported)
}
fn delete_file(&mut self, _path: &str) -> IoResult<()> {
Err(IoErrorKind::Unsupported)
}
fn read_file(&mut self, _path: &str) -> IoResult<&str> {
Err(IoErrorKind::Unsupported)
}
fn command_line_count(&self) -> IoResult<usize> {
Err(IoErrorKind::Unsupported)
}
fn command_line_arg(&self, _index: usize) -> IoResult<&str> {
Err(IoErrorKind::Unsupported)
}
fn get_environment_variable(&mut self, _name: &str) -> IoResult<Option<&str>> {
Err(IoErrorKind::Unsupported)
}
fn environment_variables_count(&mut self) -> IoResult<usize> {
Err(IoErrorKind::Unsupported)
}
fn environment_variable_at(&self, _index: usize) -> IoResult<(&str, &str)> {
Err(IoErrorKind::Unsupported)
}
fn exit_process(&mut self, _code: i32) -> IoResult<()> {
Err(IoErrorKind::Unsupported)
}
fn emergency_exit_process(&mut self, _code: i32) -> IoResult<()> {
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,
}