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
use std::fmt;

use super::*;

pub trait TolerationExt {
    fn no_schedule(key: impl ToString) -> TolerationBuilder;
    fn prefer_no_schedule(key: impl ToString) -> TolerationBuilder;
    fn no_execute(key: impl ToString) -> TolerationBuilder;
    fn toleration_seconds(self, toleration_seconds: i64) -> Self;
}

impl TolerationExt for corev1::Toleration {
    fn no_schedule(key: impl ToString) -> TolerationBuilder {
        let effect = Some(Effect::NoSchedule);
        let key = Some(key.to_string());
        TolerationBuilder { effect, key }
    }

    fn prefer_no_schedule(key: impl ToString) -> TolerationBuilder {
        let effect = Some(Effect::PreferNoSchedule);
        let key = Some(key.to_string());
        TolerationBuilder { effect, key }
    }

    fn no_execute(key: impl ToString) -> TolerationBuilder {
        let effect = Some(Effect::NoExecute);
        let key = Some(key.to_string());
        TolerationBuilder { effect, key }
    }

    fn toleration_seconds(self, toleration_seconds: i64) -> Self {
        Self {
            toleration_seconds: Some(toleration_seconds),
            ..self
        }
    }
}

#[derive(Debug)]
#[must_use]
pub struct TolerationBuilder {
    effect: Option<Effect>,
    key: Option<String>,
}

impl TolerationBuilder {
    pub fn equal(self, value: impl ToString) -> corev1::Toleration {
        corev1::Toleration {
            effect: self.effect.map(|effect| effect.to_string()),
            key: self.key,
            operator: Some(Operator::Equal.to_string()),
            toleration_seconds: None,
            value: Some(value.to_string()),
        }
    }

    pub fn exists(self) -> corev1::Toleration {
        corev1::Toleration {
            effect: self.effect.map(|effect| effect.as_str().into()),
            key: self.key,
            operator: Some(Operator::Exists.to_string()),
            toleration_seconds: None,
            value: None,
        }
    }
}

#[derive(Debug)]
enum Effect {
    NoSchedule,
    PreferNoSchedule,
    NoExecute,
}

impl Effect {
    fn as_str(&self) -> &'static str {
        match self {
            Self::NoSchedule => "NoSchedule",
            Self::PreferNoSchedule => "PreferNoSchedule",
            Self::NoExecute => "NoExecute",
        }
    }
}

impl fmt::Display for Effect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

#[derive(Debug)]
enum Operator {
    Equal,
    Exists,
}

impl Operator {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Equal => "Equal",
            Self::Exists => "Exists",
        }
    }
}

impl fmt::Display for Operator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}