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
use crate::ResourceName;
use core::fmt::{Display, Formatter};
use minicbor::{Decode, Encode};
use ockam_core::compat::string::{String, ToString};
use ockam_core::compat::vec::Vec;
use serde::{Serialize, Serializer};
use strum::{AsRefStr, Display, EnumIter, EnumString, IntoEnumIterator};

#[derive(Clone, Debug, Encode, Decode, PartialEq)]
#[rustfmt::skip]
#[cbor(map)]
pub struct Resource {
    #[n(1)] pub resource_name: ResourceName,
    #[n(2)] pub resource_type: ResourceType,
}

impl Resource {
    pub fn new(resource_name: impl Into<ResourceName>, resource_type: ResourceType) -> Self {
        Self {
            resource_name: resource_name.into(),
            resource_type,
        }
    }
}

impl Display for Resource {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "(name: {}, type: {})",
            self.resource_name, self.resource_type
        )
    }
}

#[derive(Clone, Debug, Decode, Encode, PartialEq, Eq, EnumString, Display, EnumIter, AsRefStr)]
#[cbor(index_only)]
pub enum ResourceType {
    #[n(1)]
    #[strum(serialize = "tcp-inlet")]
    TcpInlet,
    #[n(2)]
    #[strum(serialize = "tcp-outlet")]
    TcpOutlet,
    #[n(3)]
    #[strum(serialize = "echoer")]
    Echoer,
}

impl ResourceType {
    /// Return a string with all valid values joined by a commas
    pub fn join_enum_values_as_string() -> String {
        Self::iter()
            .map(|v| v.to_string())
            .collect::<Vec<String>>()
            .join(", ")
    }
}

impl Serialize for ResourceType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_ref())
    }
}