fluvio_sc_schema/objects/
list.rs

1use std::fmt::Debug;
2use std::marker::PhantomData;
3
4use anyhow::Result;
5
6use fluvio_controlplane_metadata::store::KeyFilter;
7use fluvio_protocol::{Encoder, Decoder, Version};
8use fluvio_protocol::api::Request;
9
10use crate::{AdminPublicApiKey, AdminSpec, TryEncodableFrom};
11use super::{COMMON_VERSION, Metadata, TypeBuffer};
12use super::classic::{ClassicObjectApiEnum, ClassicDecoding};
13
14/// Filter for List
15#[derive(Debug, Encoder, Decoder, Default)]
16pub struct ListFilter {
17    pub name: String,
18}
19
20impl From<String> for ListFilter {
21    fn from(name: String) -> Self {
22        Self { name }
23    }
24}
25
26#[derive(Debug, Encoder, Decoder, Default)]
27pub struct ListFilters {
28    filters: Vec<ListFilter>,
29}
30
31impl From<Vec<ListFilter>> for ListFilters {
32    fn from(filters: Vec<ListFilter>) -> Self {
33        Self { filters }
34    }
35}
36
37impl From<&str> for ListFilters {
38    fn from(name: &str) -> Self {
39        Self {
40            filters: vec![ListFilter {
41                name: name.to_owned(),
42            }],
43        }
44    }
45}
46
47impl From<ListFilters> for Vec<ListFilter> {
48    fn from(filter: ListFilters) -> Self {
49        filter.filters
50    }
51}
52
53impl KeyFilter<str> for ListFilters {
54    fn filter(&self, value: &str) -> bool {
55        if self.filters.is_empty() {
56            return true;
57        }
58        self.filters
59            .iter()
60            .filter(|key| key.name.filter(value))
61            .count()
62            > 0
63    }
64}
65
66#[derive(Debug, Default, Encoder, Decoder)]
67pub struct ListRequest<S> {
68    pub name_filters: ListFilters,
69    #[fluvio(min_version = 10)]
70    pub summary: bool, // if true, only return summary
71    #[fluvio(min_version = 13)]
72    pub system: bool, // if true, only return system specs
73    data: PhantomData<S>, // satisfy generic
74}
75
76impl<S> ListRequest<S> {
77    pub fn new(name_filters: impl Into<ListFilters>, summary: bool) -> Self {
78        Self {
79            name_filters: name_filters.into(),
80            summary,
81            system: false,
82            data: PhantomData,
83        }
84    }
85
86    pub fn system(mut self, system: bool) -> Self {
87        self.system = system;
88        self
89    }
90}
91
92#[derive(Debug, Default, Encoder)]
93pub struct ObjectApiListRequest(TypeBuffer);
94
95impl<S> TryEncodableFrom<ListRequest<S>> for ObjectApiListRequest
96where
97    ListRequest<S>: Encoder + Decoder + Debug,
98    S: AdminSpec,
99{
100    fn try_encode_from(input: ListRequest<S>, version: Version) -> Result<Self> {
101        Ok(Self(TypeBuffer::encode::<S, _>(input, version)?))
102    }
103
104    fn downcast(&self) -> Result<Option<ListRequest<S>>> {
105        self.0.downcast::<S, _>()
106    }
107}
108
109impl Request for ObjectApiListRequest {
110    const API_KEY: u16 = AdminPublicApiKey::List as u16;
111    const MIN_API_VERSION: i16 = 15;
112    const DEFAULT_API_VERSION: i16 = COMMON_VERSION;
113    type Response = ObjectApiListResponse;
114}
115
116#[derive(Debug, Default, Encoder)]
117pub struct ObjectApiListResponse(TypeBuffer);
118
119impl<S> TryEncodableFrom<ListResponse<S>> for ObjectApiListResponse
120where
121    S: AdminSpec,
122    S::Status: Encoder + Decoder + Debug,
123{
124    fn try_encode_from(input: ListResponse<S>, version: Version) -> Result<ObjectApiListResponse> {
125        Ok(ObjectApiListResponse(TypeBuffer::encode::<S, _>(
126            input, version,
127        )?))
128    }
129
130    fn downcast(&self) -> Result<Option<ListResponse<S>>> {
131        self.0.downcast::<S, _>()
132    }
133}
134
135#[derive(Debug, Default, Encoder, Decoder)]
136pub struct ListResponse<S: AdminSpec>
137where
138    S::Status: Encoder + Decoder + Debug,
139{
140    inner: Vec<Metadata<S>>,
141}
142
143impl<S> ListResponse<S>
144where
145    S: AdminSpec,
146    S::Status: Encoder + Decoder + Debug,
147{
148    pub fn new(inner: Vec<Metadata<S>>) -> Self {
149        Self { inner }
150    }
151
152    pub fn inner(self) -> Vec<Metadata<S>> {
153        self.inner
154    }
155}
156
157// for supporting classic, this should go away after we remove classic
158ClassicObjectApiEnum!(ListRequest);
159ClassicObjectApiEnum!(ListResponse);
160ClassicDecoding!(ListRequest);
161ClassicDecoding!(ListResponse);