use std::hash::Hash;
use serde::{Deserialize, Serialize};
use crate::agent::{AgentDescriptor, AgentName};
use crate::error::Error;
use crate::protocol::ProtocolBehavior;
use crate::stream::Stream;
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq, Hash, Default)]
pub struct PutOptions {
options: Vec<(String, String)>,
}
impl PutOptions {
pub fn new(options: Vec<(String, String)>) -> Self {
Self { options }
}
}
impl PutOptions {
pub fn get_option(&self, key: &str) -> Option<&str> {
self.options
.iter()
.find(|(found_key, _value)| -> bool { found_key == key })
.map(|(_key, value)| value.as_str())
}
}
impl<S> From<Vec<(S, S)>> for PutOptions
where
S: Into<String>,
{
fn from(value: Vec<(S, S)>) -> Self {
Self {
options: value
.into_iter()
.map(|(key, value)| (key.into(), value.into()))
.collect(),
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq, Hash, Default)]
pub struct PutDescriptor {
pub factory: String,
pub options: PutOptions,
}
impl PutDescriptor {
pub fn new(factory: impl Into<String>, options: impl Into<PutOptions>) -> Self {
Self {
factory: factory.into(),
options: options.into(),
}
}
}
impl<S> From<S> for PutDescriptor
where
S: Into<String>,
{
fn from(name: S) -> Self {
PutDescriptor::new(name, PutOptions::default())
}
}
pub trait Put<PB: ProtocolBehavior>: Stream<PB> + 'static {
fn progress(&mut self) -> Result<(), Error>;
fn reset(&mut self, new_name: AgentName) -> Result<(), Error>;
fn descriptor(&self) -> &AgentDescriptor;
fn describe_state(&self) -> &str;
fn is_state_successful(&self) -> bool;
fn shutdown(&mut self) -> String;
fn version() -> String
where
Self: Sized;
}