Skip to main content

tansu_topic/
lib.rs

1// Copyright ⓒ 2024-2025 Peter Morgan <peter.james.morgan@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{fmt, io, result};
16
17use create::Create;
18use delete::Delete;
19use std::{marker::PhantomData, sync::Arc};
20use tansu_sans_io::ErrorCode;
21use url::Url;
22
23use crate::list::List;
24
25mod create;
26mod delete;
27mod list;
28
29pub type Result<T, E = Error> = result::Result<T, E>;
30
31#[derive(thiserror::Error, Debug)]
32pub enum Error {
33    Api(ErrorCode),
34    Client(#[from] tansu_client::Error),
35    Io(Arc<io::Error>),
36    Protocol(#[from] tansu_sans_io::Error),
37    SerdeJson(Arc<serde_json::Error>),
38}
39
40impl From<io::Error> for Error {
41    fn from(value: io::Error) -> Self {
42        Self::Io(Arc::new(value))
43    }
44}
45
46impl From<serde_json::Error> for Error {
47    fn from(value: serde_json::Error) -> Self {
48        Self::SerdeJson(Arc::new(value))
49    }
50}
51
52impl fmt::Display for Error {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{self:?}")
55    }
56}
57
58#[derive(Clone, Debug, Eq, PartialEq)]
59pub enum Topic {
60    Create(create::Configuration),
61    Delete(delete::Configuration),
62    List(list::Configuration),
63}
64
65impl Topic {
66    pub fn create() -> create::Builder<PhantomData<Url>, PhantomData<String>, PhantomData<i32>> {
67        create::Builder::default()
68    }
69
70    pub fn delete() -> delete::Builder<PhantomData<Url>, PhantomData<String>> {
71        delete::Builder::default()
72    }
73
74    pub fn list() -> list::Builder<PhantomData<Url>> {
75        list::Builder::default()
76    }
77
78    pub async fn main(self) -> Result<ErrorCode> {
79        match self {
80            Self::Create(configuration) => Create::try_from(configuration)?.main().await,
81            Self::Delete(configuration) => Delete::try_from(configuration)?.main().await,
82            Self::List(configuration) => List::try_from(configuration)?.main().await,
83        }
84    }
85}