Skip to main content

CliMessageChannel

Struct CliMessageChannel 

Source
pub struct CliMessageChannel { /* private fields */ }
Expand description

A command-line message channel: prints messages to the terminal and reads one line from stdin as the reply.

Binds to standard input / standard output by default; alternatively, inject custom read/write sources with CliMessageChannel::with_io (files, network, in-memory buffers — common in tests).

Messages travel line by line: each message gets its own line on output (a newline is appended automatically); ask reads one line from the input, trims leading and trailing whitespace, and returns it as the reply.

All operations are serialized through an internal mutex: ask presents only one request at a time, and other ask / notify calls queue while it waits for the reply; input is read asynchronously, yielding while waiting, so other tasks are not blocked on a single-threaded runtime.

§Example

use molo::{CliMessageChannel, MessageChannel};
use tokio::io::{AsyncWriteExt, BufReader};

// Inject in-memory read/write sources instead of touching a real terminal.
let (mut seed, rx) = tokio::io::duplex(64);
seed.write_all("yes\n".as_bytes()).await?;
let channel = CliMessageChannel::with_io(
    Box::new(BufReader::new(rx)),
    Box::new(tokio::io::sink()),
);

let answer = channel.ask("continue? [y/n]").await?;
assert_eq!(answer, "yes");

§Notes

  • when input reaches end-of-file (Ctrl-D / EOF), ask returns ChannelError::Closed;
  • replies are read line by line and trimmed: an empty line still counts as a valid reply (an empty string).

Implementations§

Source§

impl CliMessageChannel

Source

pub fn new() -> Self

Binds to standard input / standard output.

Source

pub fn with_io( reader: Box<dyn AsyncBufRead + Unpin + Send>, writer: Box<dyn AsyncWrite + Unpin + Send>, ) -> Self

Binds custom read/write sources.

reader supplies the reply input (read line by line), and writer receives the output messages (written line by line and flushed). Tests can inject an in-memory buffer, or the channel can be attached to a file or the network.

Trait Implementations§

Source§

impl Debug for CliMessageChannel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CliMessageChannel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl MessageChannel for CliMessageChannel

Source§

fn ask<'life0, 'life1, 'async_trait>( &'life0 self, message: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, ChannelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Sends a question and waits for a reply (request-response). Read more
Source§

fn notify<'life0, 'life1, 'async_trait>( &'life0 self, message: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<(), ChannelError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

A one-way notification that does not wait for a reply (broadcast to all receivers).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more