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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
mod spec;
mod status;

pub use self::spec::*;
pub use self::status::*;
pub use custom_metadata::CustomSpuKey;

#[cfg(feature = "k8")]
mod k8;

mod metadata {

    use crate::core::{Spec, Status};
    use crate::spg::SpuGroupSpec;
    use crate::extended::{ObjectType, SpecExt};

    use super::*;

    impl Spec for SpuSpec {
        const LABEL: &'static str = "SPU";
        type IndexKey = String;
        type Owner = SpuGroupSpec;
        type Status = SpuStatus;
    }

    impl SpecExt for SpuSpec {
        const OBJECT_TYPE: ObjectType = ObjectType::Spu;
    }

    impl Status for SpuStatus {}

    #[cfg(feature = "k8")]
    mod extended {

        use crate::store::k8::{K8ExtendedSpec, K8ConvertError, K8MetaItem};
        use crate::store::MetadataStoreObject;
        use crate::k8_types::K8Obj;
        use crate::store::k8::default_convert_from_k8;

        use super::SpuSpec;

        impl K8ExtendedSpec for SpuSpec {
            type K8Spec = Self;

            fn convert_from_k8(
                k8_obj: K8Obj<Self::K8Spec>,
                multi_namespace_context: bool,
            ) -> Result<MetadataStoreObject<Self, K8MetaItem>, K8ConvertError<Self::K8Spec>>
            {
                default_convert_from_k8(k8_obj, multi_namespace_context)
            }

            fn convert_status_from_k8(status: Self::Status) -> Self::Status {
                status
            }

            fn into_k8(self) -> Self::K8Spec {
                self
            }
        }
    }
}

mod custom_metadata {

    use std::io::Error;
    use std::io::ErrorKind;
    use std::fmt;

    use tracing::trace;

    use fluvio_protocol::Encoder;
    use fluvio_protocol::Decoder;
    use fluvio_protocol::Version;
    use fluvio_protocol::bytes::{Buf, BufMut};

    use crate::core::{Spec, Removable, Creatable};
    use crate::extended::{ObjectType, SpecExt};

    use super::*;

    /// this is not real spec but is there to allow passing of parameters
    impl Spec for CustomSpuSpec {
        const LABEL: &'static str = "CustomSpu";
        type IndexKey = String;
        type Status = SpuStatus;
        type Owner = SpuSpec;
    }

    impl SpecExt for CustomSpuSpec {
        const OBJECT_TYPE: ObjectType = ObjectType::CustomSpu;
    }

    impl Removable for CustomSpuSpec {
        type DeleteKey = CustomSpuKey;
    }

    impl Creatable for CustomSpuSpec {}

    // This can be auto generated by enum derive later
    #[derive(Debug)]
    pub enum CustomSpuKey {
        Name(String),
        Id(i32),
    }

    impl fmt::Display for CustomSpuKey {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            match self {
                Self::Name(name) => write!(f, "{name}"),
                Self::Id(id) => write!(f, "{id}"),
            }
        }
    }

    impl From<&CustomSpuKey> for String {
        fn from(key: &CustomSpuKey) -> Self {
            match key {
                CustomSpuKey::Name(name) => name.to_owned(),
                CustomSpuKey::Id(id) => id.to_string(),
            }
        }
    }

    impl CustomSpuKey {
        fn type_string(&self) -> &'static str {
            match self {
                Self::Name(_) => "Name",
                Self::Id(_) => "Id",
            }
        }
    }

    impl Default for CustomSpuKey {
        fn default() -> Self {
            Self::Id(0)
        }
    }

    impl Encoder for CustomSpuKey {
        fn write_size(&self, version: Version) -> usize {
            let type_size = self.type_string().to_owned().write_size(version);

            type_size
                + match self {
                    Self::Name(s) => s.write_size(version),
                    Self::Id(s) => s.write_size(version),
                }
        }

        // encode match
        fn encode<T>(&self, dest: &mut T, version: Version) -> Result<(), Error>
        where
            T: BufMut,
        {
            self.type_string().to_owned().encode(dest, version)?;

            match self {
                Self::Name(s) => s.encode(dest, version)?,
                Self::Id(s) => s.encode(dest, version)?,
            }

            Ok(())
        }
    }

    impl Decoder for CustomSpuKey {
        fn decode<T>(&mut self, src: &mut T, version: Version) -> Result<(), Error>
        where
            T: Buf,
        {
            let mut typ = "".to_owned();
            typ.decode(src, version)?;
            trace!("decoded type: {}", typ);

            match typ.as_ref() {
                "Name" => {
                    let mut response = String::default();
                    response.decode(src, version)?;
                    *self = Self::Name(response);
                    Ok(())
                }

                "Id" => {
                    let mut response: i32 = 9;
                    response.decode(src, version)?;
                    *self = Self::Id(response);
                    Ok(())
                }

                // Unexpected type
                _ => Err(Error::new(
                    ErrorKind::InvalidData,
                    format!("invalid spec type {typ}"),
                )),
            }
        }
    }
}