use super::{ClientError, Reply, Result};
use crate::proto::ReachedConsole;
use crate::wire::{Reader, Writer};
pub struct HeldConsole {
pub console: ReachedConsole,
pub(super) reader: Reader,
pub(super) _writer: Writer,
}
impl HeldConsole {
pub async fn closed(&mut self) -> String {
loop {
match self.reader.next::<Reply>().await {
Ok(Some(Reply::ConsoleClosed { reason, .. })) => return reason,
Ok(Some(_)) => {}
Ok(None) => return "the runner service closed the connection".to_owned(),
Err(e) => return format!("the runner service's connection broke: {e}"),
}
}
}
}
impl std::fmt::Debug for HeldConsole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HeldConsole").field("console", &self.console).finish_non_exhaustive()
}
}
pub(super) fn of_reply(reply: Option<Reply>) -> Result<ReachedConsole> {
match reply {
Some(Reply::Console { console, .. }) => Ok(*console),
Some(Reply::Error { report, .. }) => Err(ClientError::Refused(Box::new(report))),
Some(other) => Err(super::unexpected(&other)),
None => Err(ClientError::closed()),
}
}