diff options
author | Irene Knapp <ireneista@irenes.space> | 2024-03-12 22:06:24 -0700 |
---|---|---|
committer | Irene Knapp <ireneista@irenes.space> | 2024-03-12 22:06:24 -0700 |
commit | f25a79763b9ae493598230a109eb0788def17199 (patch) | |
tree | dfcee3742e7086985c7392944b0b98308c89b2c7 /src/terminal | |
parent | 7be9acd0bb08901c9fdfa45b694b7d3d5a594e70 (diff) |
Change-Id: I3478f222ee4b24d9d1796f0f58986186119bc2f7
Diffstat (limited to 'src/terminal')
-rw-r--r-- | src/terminal/decoding.rs | 117 | ||||
-rw-r--r-- | src/terminal/error.rs | 45 | ||||
-rw-r--r-- | src/terminal/prelude.rs | 4 |
3 files changed, 0 insertions, 166 deletions
diff --git a/src/terminal/decoding.rs b/src/terminal/decoding.rs deleted file mode 100644 index 018730e..0000000 --- a/src/terminal/decoding.rs +++ /dev/null @@ -1,117 +0,0 @@ -#![forbid(unsafe_code)] -use crate::terminal::prelude::*; - -use crate::terminal::error; - -use pin_project::pin_project; -use pin_utils::pin_mut; -use std::future::Future; -use std::pin::Pin; -use std::str; -use std::sync::Arc; -use std::task::{Context, Poll}; -use tokio::io::{AsyncBufRead, AsyncRead, BufReader}; -use tokio::sync::Mutex; - - -#[pin_project] -pub struct CharBufReader<R: AsyncRead + Unpin> { - #[pin] byte_reader: BufReader<R>, - #[pin] char_buffer: String, -} - - -#[pin_project] -struct FillBufFuture<R: AsyncRead + Unpin> { - char_reader: Arc<Mutex<CharBufReader<R>>>, -} - - -impl<R: AsyncRead + Unpin> Future for FillBufFuture<R> { - type Output = Result<String>; - - fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) - -> Poll<Self::Output> - { - let future = self.project(); - let char_reader: &mut Arc<Mutex<CharBufReader<R>>> = future.char_reader; - let char_reader_future = char_reader.lock(); - pin_mut!(char_reader_future); - match char_reader_future.poll(context) { - Poll::Ready(mut char_reader) => { - let char_reader = &mut *char_reader; - let mut byte_reader: Pin<&mut BufReader<R>> = Pin::new(&mut char_reader.byte_reader); - - loop { - match byte_reader.as_mut().poll_fill_buf(context).map_err(error::input)? - { - Poll::Ready(byte_buffer) => { - match str::from_utf8(&byte_buffer) { - Err(error) => { - let n_valid = error.valid_up_to(); - if n_valid == 0 { - byte_reader.as_mut().consume(1); - } else { - match str::from_utf8(&byte_buffer[..n_valid]) { - Err(_) => { - byte_reader.as_mut().consume(1); - }, - Ok(chars) => { - char_reader.char_buffer.push_str(chars); - - byte_reader.as_mut().consume(n_valid); - - break; - }, - } - } - } - Ok(chars) => { - char_reader.char_buffer.push_str(chars); - - let n_to_consume = byte_buffer.len(); - byte_reader.as_mut().consume(n_to_consume); - - break; - } - } - } - Poll::Pending => { - return Poll::Pending; - } - } - } - - return Poll::Ready(Ok(char_reader.char_buffer.to_string())); - } - Poll::Pending => { - return Poll::Pending; - } - } - } -} - - -impl<R: AsyncRead + Unpin> CharBufReader<R> { - pub fn new(input_stream: R) -> CharBufReader<R> { - let byte_reader = BufReader::new(input_stream); - - CharBufReader { - byte_reader: byte_reader, - char_buffer: String::new(), - } - } - - pub fn fill_buf(reader: Arc<Mutex<Self>>) - -> impl Future<Output = Result<String>> - { - FillBufFuture { - char_reader: reader, - } - } - - pub fn consume(&mut self, amount: usize) { - self.char_buffer.replace_range(..amount, ""); - } -} - diff --git a/src/terminal/error.rs b/src/terminal/error.rs deleted file mode 100644 index 795f973..0000000 --- a/src/terminal/error.rs +++ /dev/null @@ -1,45 +0,0 @@ -#![forbid(unsafe_code)] - -pub type Result<T> = std::result::Result<T, TerminalError>; - - -#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)] -pub enum TerminalError { - Input(String), - ModeSetting(String), - Internal(String), -} - -impl std::error::Error for TerminalError { } - -impl std::fmt::Display for TerminalError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - TerminalError::Input(s) => - f.write_fmt(format_args!( - "Can't read terminal input: {}", s)), - TerminalError::ModeSetting(s) => - f.write_fmt(format_args!( - "Can't set terminal mode: {}", s)), - TerminalError::Internal(s) => - f.write_fmt(format_args!( - "Internal error regarding the terminal: {}", s)), - } - } -} - - -pub fn input(e: impl std::error::Error) -> TerminalError { - TerminalError::ModeSetting(format!("{}", e)) -} - - -pub fn mode_setting(e: impl std::error::Error) -> TerminalError { - TerminalError::ModeSetting(format!("{}", e)) -} - - -pub fn internal(e: impl std::error::Error) -> TerminalError { - TerminalError::Internal(format!("{}", e)) -} - diff --git a/src/terminal/prelude.rs b/src/terminal/prelude.rs deleted file mode 100644 index bada817..0000000 --- a/src/terminal/prelude.rs +++ /dev/null @@ -1,4 +0,0 @@ -#![forbid(unsafe_code)] - -pub use crate::terminal::error::{Result, TerminalError}; - |