1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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())
    }
}

/// Generic trait used to define the interface with a concrete library
/// implementing the protocol.
pub trait Put<PB: ProtocolBehavior>: Stream<PB> + 'static {
    /// Process incoming buffer, internal progress, can fill in the output buffer
    fn progress(&mut self) -> Result<(), Error>;

    /// In-place reset of the state
    fn reset(&mut self, new_name: AgentName) -> Result<(), Error>;

    fn descriptor(&self) -> &AgentDescriptor;

    /// Returns a textual representation of the state in which self is
    fn describe_state(&self) -> &str;

    /// Checks whether the Put is in a good state
    fn is_state_successful(&self) -> bool;

    /// Shut down the PUT by consuming it and returning a string that summarizes the execution.
    fn shutdown(&mut self) -> String;

    /// Returns a textual representation of the version of the PUT used by self
    fn version() -> String
    where
        Self: Sized;
}