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 std::error::Error as StdError;
use std::fmt;

/// Defines the strategy by which idle connections are taken from the pool.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ActivationOrder {
    /// First In - First Out
    ///
    /// Connections are taken in the same order they were
    /// added/returned to the pool
    FiFo,
    /// Last In - First Out
    ///
    /// The connections that were added/returned last will
    /// be taken from
    /// the pool first
    LiFo,
}

impl Default for ActivationOrder {
    fn default() -> Self {
        ActivationOrder::FiFo
    }
}

impl fmt::Display for ActivationOrder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ActivationOrder::FiFo => write!(f, "FiFo"),
            ActivationOrder::LiFo => write!(f, "LiFo"),
        }
    }
}

impl std::str::FromStr for ActivationOrder {
    type Err = ParseActivationOrderError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match &*s.to_lowercase() {
            "fifo" => Ok(ActivationOrder::FiFo),
            "lifo" => Ok(ActivationOrder::LiFo),
            invalid => Err(ParseActivationOrderError(format!(
                "'{}' is not a valid ActivationOrder. Only 'FiFo' and 'LiFo' are allowed.",
                invalid
            ))),
        }
    }
}

#[derive(Debug)]
pub struct ParseActivationOrderError(String);

impl fmt::Display for ParseActivationOrderError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Could not parse ActivationOrder. {}", self.0)
    }
}

impl StdError for ParseActivationOrderError {
    fn description(&self) -> &str {
        "parse activation order initialization failed"
    }

    fn cause(&self) -> Option<&dyn StdError> {
        None
    }
}