rm-lisa 0.3.2

A logging library for rem-verse, with support for inputs, tasks, and more.
Documentation
//! A provider trait that makes it easy to test reading input.

use crate::{errors::LisaError, input::stdin::os_specific_apis::read_non_blocking_stdin};
use parking_lot::Mutex;
use std::io::{Error as IoError, Stdin};
use tokio::sync::mpsc::Receiver as BoundedReceiver;

/// A provider of a non-blocking read implementation.
pub trait ReadProvider: Send + Sync + Sized {
	/// Perform a non-blocking read.
	///
	/// ## Errors
	///
	/// If the provider cannot read from the input for any reason.
	fn non_blocking_read(&self) -> Result<String, LisaError>;
}

impl ReadProvider for Stdin {
	fn non_blocking_read(&self) -> Result<String, LisaError> {
		read_non_blocking_stdin()
	}
}

impl ReadProvider for Mutex<BoundedReceiver<String>> {
	fn non_blocking_read(&self) -> Result<String, LisaError> {
		self.lock()
			.try_recv()
			.map_err(|cause| LisaError::IOError(IoError::other(cause)))
	}
}