use crate::{Input, proto};
#[derive(Debug, Clone)]
pub enum Action {
Close,
Copy(String),
SetInput(Input),
DisplayError(String),
}
impl Action {
pub(crate) fn into_proto(self) -> proto::Action {
use proto::action::Action as PrAction;
let inner_action = match self {
Self::Close => PrAction::Close(()),
Self::Copy(str) => PrAction::Copy(str),
Self::SetInput(input) => PrAction::SetInput(input.into_proto()),
Self::DisplayError(err) => PrAction::DisplayError(err),
};
proto::Action {
action: Some(inner_action),
}
}
}
impl Action {
pub fn close() -> Self {
Self::Close
}
pub fn copy(str: impl Into<String>) -> Self {
Self::Copy(str.into())
}
pub fn set_input(input: impl Into<Input>) -> Self {
Self::SetInput(input.into())
}
pub fn display_error(err: impl std::fmt::Display) -> Self {
Self::DisplayError(err.to_string())
}
}
pub struct Actions {
pub list: Vec<Action>,
_priv: (),
}
impl<T: IntoIterator<Item = Action>> From<T> for Actions {
fn from(value: T) -> Self {
Self {
list: value.into_iter().collect(),
_priv: (),
}
}
}
impl From<Action> for Actions {
fn from(value: Action) -> Self {
Self::from(std::iter::once(value))
}
}
impl From<Input> for Actions {
fn from(value: Input) -> Self {
Self::from(Action::SetInput(value))
}
}