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
pub mod epoch;
pub mod core;
pub mod store;

// re-export k8-types crate
#[cfg(feature = "k8")]
pub use k8_types;

#[cfg(feature = "fixture")]
pub mod fixture {

    use std::fmt::Display;

    use serde::{Serialize, Deserialize};

    use crate::core::{Spec, Status, MetadataItem, MetadataContext, MetadataRevExtension};
    use crate::store::MetadataStoreObject;
    use crate::epoch::DualEpochMap;

    // define test spec and status
    #[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
    pub struct TestSpec {
        pub replica: u16,
    }

    impl Spec for TestSpec {
        const LABEL: &'static str = "Test";
        type IndexKey = String;
        type Owner = Self;
        type Status = TestStatus;
    }

    #[derive(Debug, Default, Clone, Eq, PartialEq, Serialize, Deserialize)]
    pub struct TestStatus {
        pub up: bool,
    }

    impl Status for TestStatus {}

    impl Display for TestStatus {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            write!(f, "{}", self.up)
        }
    }

    pub type DefaultTest = MetadataStoreObject<TestSpec, TestMeta>;

    pub type TestEpochMap = DualEpochMap<String, DefaultTest>;

    #[derive(Debug, Default, Eq, PartialEq, Clone)]
    pub struct TestMeta {
        pub rev: u32,
        pub comment: String,
    }

    impl MetadataItem for TestMeta {
        type UId = u32;

        fn uid(&self) -> &Self::UId {
            &self.rev
        }

        fn is_newer(&self, another: &Self) -> bool {
            self.rev >= another.rev
        }
    }

    impl MetadataRevExtension for TestMeta {
        fn next_rev(&self) -> Self {
            Self {
                rev: self.rev + 1,
                comment: self.comment.clone(),
            }
        }
    }

    impl TestMeta {
        pub fn new(rev: u32) -> Self {
            Self {
                rev,
                comment: "new".to_owned(),
            }
        }
    }

    impl From<u32> for MetadataContext<TestMeta> {
        fn from(val: u32) -> MetadataContext<TestMeta> {
            TestMeta::new(val).into()
        }
    }
}