pub mod event; pub mod im; pub mod store; pub mod task; pub mod terminal; pub mod ui; use std::any::Any; use std::fmt::Debug; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; use serde::ser::{Serialize, SerializeStruct, Serializer}; use anyhow::Result; use store::State; use task::Interrupted; use ui::widget::Widget; use ui::Frontend; /// An optional return value. #[derive(Clone, Debug)] pub struct Exit { pub value: Option, } /// The output that is returned by all selection interfaces. #[derive(Clone, Default, Debug, Eq, PartialEq)] pub struct Selection where I: ToString, { pub operation: Option, pub ids: Vec, pub args: Vec, } impl Selection where I: ToString, { pub fn with_operation(mut self, operation: String) -> Self { self.operation = Some(operation); self } pub fn with_id(mut self, id: I) -> Self { self.ids.push(id); self } pub fn with_args(mut self, arg: String) -> Self { self.args.push(arg); self } } impl Serialize for Selection where I: ToString, { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut state = serializer.serialize_struct("", 3)?; state.serialize_field("operation", &self.operation)?; state.serialize_field( "ids", &self.ids.iter().map(|id| id.to_string()).collect::>(), )?; state.serialize_field("args", &self.args)?; state.end() } } /// Provide implementations for conversions to and from `Box`. pub trait BoxedAny { fn from_boxed_any(any: Box) -> Option where Self: Sized + Clone + 'static; fn to_boxed_any(self) -> Box where Self: Sized + Clone + 'static; } impl BoxedAny for T where T: Sized + Clone + 'static, { fn from_boxed_any(any: Box) -> Option where Self: Sized + Clone + 'static, { any.downcast::().ok().map(|b| *b) } fn to_boxed_any(self) -> Box where Self: Sized + Clone + 'static, { Box::new(self) } } /// A 'PageStack' for applications. Page identifier can be pushed to and /// popped from the stack. #[derive(Clone, Default, Debug)] pub struct PageStack { pages: Vec, } impl PageStack { pub fn new(pages: Vec) -> Self { Self { pages } } pub fn push(&mut self, page: T) { self.pages.push(page); } pub fn pop(&mut self) -> Option { self.pages.pop() } pub fn peek(&self) -> Result<&T> { match self.pages.last() { Some(page) => Ok(page), None => Err(anyhow::anyhow!( "Could not peek active page. Page stack is empty." )), } } } /// A multi-producer, single-consumer message channel. pub struct Channel { pub tx: UnboundedSender, pub rx: UnboundedReceiver, } impl Default for Channel { fn default() -> Self { let (tx, rx) = mpsc::unbounded_channel(); Self { tx: tx.clone(), rx } } } /// Initialize a `Store` with the `State` given and a `Frontend` with the `Widget` given, /// and run their main loops concurrently. Connect them to the `Channel` and also to /// an interrupt broadcast channel also initialized in this function. pub async fn run(channel: Channel, state: S, root: Widget) -> Result> where S: State + Clone + Debug + Send + Sync + 'static, M: 'static, P: Clone + Debug + Send + Sync + 'static, { let (terminator, mut interrupt_rx) = task::create_termination(); let (store, state_rx) = store::Store::::new(); let frontend = Frontend::default(); tokio::try_join!( store.main_loop(state, terminator, channel.rx, interrupt_rx.resubscribe()), frontend.main_loop(root, state_rx, interrupt_rx.resubscribe()), )?; if let Ok(reason) = interrupt_rx.recv().await { match reason { Interrupted::User { payload } => Ok(payload), Interrupted::OsSignal => anyhow::bail!("exited because of an os sig int"), } } else { anyhow::bail!("exited because of an unexpected error"); } }