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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
mod error;
mod options;
mod roles;
mod value;

use serde;
use std::fmt;

pub use messages::types::error::*;
pub use messages::types::options::*;
pub use messages::types::roles::*;
pub use messages::types::value::*;

fn is_not(b: &bool) -> bool {
    !*b
}

/**************************
         Structs
**************************/

/// The policies that can be used for matching a uri pattern.
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum MatchingPolicy {
    /// The given pattern matches any URI that has it as a prefix
    Prefix,
    /// The given pattern contains at least one 'wildcard' segment which can match any segment at the same location
    Wildcard,
    /// The given pattern only matches URIs that are identical.
    Strict,
}

/// The policies that dictate how invocations are distributed amongst shared registrations
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum InvocationPolicy {
    // Only one reigistration per uri (the default)
    Single,
    // Callee selcted sequentially from the list of registrants
    RoundRobin,
    // Callee selcted randomly from the list of registrants
    Random,
    // First callee (in orer of registration) is called
    First,
    // Last callee (in order of registration( is called
    Last,
}

/**************************
        Visitors
**************************/

struct MatchingPolicyVisitor;
struct InvocationPolicyVisitor;

impl MatchingPolicy {
    #[inline]
    fn is_strict(&self) -> bool {
        self == &MatchingPolicy::Strict
    }
}

impl InvocationPolicy {
    #[inline]
    fn is_single(&self) -> bool {
        self == &InvocationPolicy::Single
    }
}

impl Default for MatchingPolicy {
    #[inline]
    fn default() -> MatchingPolicy {
        MatchingPolicy::Strict
    }
}

impl Default for InvocationPolicy {
    #[inline]
    fn default() -> InvocationPolicy {
        InvocationPolicy::Single
    }
}

/*-------------------------
       MatchingPolicy
-------------------------*/

impl serde::Serialize for MatchingPolicy {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let ser_str = match *self {
            MatchingPolicy::Prefix => "prefix",
            MatchingPolicy::Wildcard => "wildcard",
            MatchingPolicy::Strict => "",
        };
        serializer.serialize_str(ser_str)
    }
}

impl<'de> serde::Deserialize<'de> for MatchingPolicy {
    fn deserialize<D>(deserializer: D) -> Result<MatchingPolicy, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(MatchingPolicyVisitor)
    }
}

impl<'de> serde::de::Visitor<'de> for MatchingPolicyVisitor {
    type Value = MatchingPolicy;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("matching policy for registration")
    }

    #[inline]
    fn visit_str<E>(self, value: &str) -> Result<MatchingPolicy, E>
    where
        E: serde::de::Error,
    {
        match value {
            "prefix" => Ok(MatchingPolicy::Prefix),
            "wildcard" => Ok(MatchingPolicy::Wildcard),
            x => Err(serde::de::Error::custom(format!(
                "Invalid matching policy: {}",
                x
            ))),
        }
    }
}

impl serde::Serialize for InvocationPolicy {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        let ser_str = match *self {
            InvocationPolicy::Single => "single",
            InvocationPolicy::RoundRobin => "roundrobin",
            InvocationPolicy::Random => "random",
            InvocationPolicy::First => "first",
            InvocationPolicy::Last => "last",
        };
        serializer.serialize_str(ser_str)
    }
}

impl<'de> serde::Deserialize<'de> for InvocationPolicy {
    fn deserialize<D>(deserializer: D) -> Result<InvocationPolicy, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_str(InvocationPolicyVisitor)
    }
}

impl<'de> serde::de::Visitor<'de> for InvocationPolicyVisitor {
    type Value = InvocationPolicy;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("invocation policy for a procedure")
    }

    #[inline]
    fn visit_str<E>(self, value: &str) -> Result<InvocationPolicy, E>
    where
        E: serde::de::Error,
    {
        match value {
            "single" => Ok(InvocationPolicy::Single),
            "roundrobin" => Ok(InvocationPolicy::RoundRobin),
            "random" => Ok(InvocationPolicy::Random),
            "first" => Ok(InvocationPolicy::First),
            "last" => Ok(InvocationPolicy::Last),
            x => Err(serde::de::Error::custom(format!(
                "Invalid invocation policy: {}",
                x
            ))),
        }
    }
}