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
#![allow(clippy::assign_op_pattern)]

use std::fmt::Debug;
use std::marker::PhantomData;

use anyhow::Result;

use fluvio_controlplane_metadata::message::Message;
use fluvio_protocol::{Encoder, Decoder, Version};
use fluvio_protocol::api::Request;
use fluvio_controlplane_metadata::store::Epoch;

use crate::{AdminPublicApiKey, AdminSpec, TryEncodableFrom};
use crate::core::Spec;

use super::classic::{ClassicObjectApiEnum, ClassicDecoding};
use super::{Metadata, COMMON_VERSION, TypeBuffer};

/// Watch resources
/// Argument epoch is not being used, it is always 0
#[derive(Debug, Encoder, Default, Decoder)]
pub struct WatchRequest<S: AdminSpec> {
    epoch: Epoch,
    #[fluvio(min_version = 10)]
    pub summary: bool, // if true, only return summary
    data: PhantomData<S>,
}

impl<S> WatchRequest<S>
where
    S: AdminSpec,
{
    pub fn summary() -> Self {
        Self {
            summary: true,
            ..Default::default()
        }
    }
}

#[derive(Debug, Default, Encoder)]
pub struct ObjectApiWatchRequest(TypeBuffer);

impl<S> TryEncodableFrom<WatchRequest<S>> for ObjectApiWatchRequest
where
    S: AdminSpec,
{
    fn try_encode_from(input: WatchRequest<S>, version: Version) -> Result<Self> {
        Ok(Self(TypeBuffer::encode::<S, _>(input, version)?))
    }

    fn downcast(&self) -> Result<Option<WatchRequest<S>>> {
        self.0.downcast::<S, _>()
    }
}

impl Request for ObjectApiWatchRequest {
    const API_KEY: u16 = AdminPublicApiKey::Watch as u16;
    const DEFAULT_API_VERSION: i16 = COMMON_VERSION;
    type Response = ObjectApiWatchResponse;
}

#[derive(Debug, Default, Encoder)]
pub struct ObjectApiWatchResponse(TypeBuffer);

impl<S> TryEncodableFrom<WatchResponse<S>> for ObjectApiWatchResponse
where
    S: AdminSpec,
    S::Status: Encoder + Decoder,
{
    fn try_encode_from(input: WatchResponse<S>, version: Version) -> Result<Self> {
        Ok(Self(TypeBuffer::encode::<S, _>(input, version)?))
    }

    fn downcast(&self) -> Result<Option<WatchResponse<S>>> {
        self.0.downcast::<S, _>()
    }
}

#[derive(Debug, Default, Encoder, Decoder, Clone)]
pub struct WatchResponse<S: AdminSpec>
where
    S::Status: Encoder + Decoder,
{
    inner: MetadataUpdate<S>,
}

impl<S> WatchResponse<S>
where
    S: AdminSpec,
    S::Status: Encoder + Decoder,
{
    pub fn new(inner: MetadataUpdate<S>) -> Self {
        Self { inner }
    }

    pub fn inner(self) -> MetadataUpdate<S> {
        self.inner
    }
}

/// updates on metadata
#[derive(Encoder, Decoder, Default, Clone, Debug)]
pub struct MetadataUpdate<S>
where
    S: Spec + Encoder + Decoder,
    S::Status: Encoder + Decoder + Debug,
{
    pub epoch: Epoch,
    pub changes: Vec<Message<Metadata<S>>>,
    pub all: Vec<Metadata<S>>,
}

impl<S> MetadataUpdate<S>
where
    S: Spec + Encoder + Decoder,
    S::Status: Encoder + Decoder + Debug,
{
    pub fn with_changes(epoch: i64, changes: Vec<Message<Metadata<S>>>) -> Self {
        Self {
            epoch,
            changes,
            all: vec![],
        }
    }

    pub fn with_all(epoch: i64, all: Vec<Metadata<S>>) -> Self {
        Self {
            epoch,
            changes: vec![],
            all,
        }
    }
}

// for supporting classic, this should go away after we remove classic
ClassicObjectApiEnum!(WatchRequest);
ClassicObjectApiEnum!(WatchResponse);
ClassicDecoding!(WatchRequest);
ClassicDecoding!(WatchResponse);