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
23mod create;
24mod delete;
25
26pub type Result<T, E = Error> = result::Result<T, E>;
27
28#[derive(thiserror::Error, Debug)]
29pub enum Error {
30    Api(ErrorCode),
31    Client(#[from] tansu_client::Error),
32    Io(Arc<io::Error>),
33    Protocol(#[from] tansu_sans_io::Error),
34}
35
36impl From<io::Error> for Error {
37    fn from(value: io::Error) -> Self {
38        Self::Io(Arc::new(value))
39    }
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "{self:?}")
45    }
46}
47
48#[derive(Clone, Debug, Eq, PartialEq)]
49pub enum Topic {
50    Create(create::Configuration),
51    Delete(delete::Configuration),
52}
53
54impl Topic {
55    pub fn create() -> create::Builder<PhantomData<Url>, PhantomData<String>, PhantomData<i32>> {
56        create::Builder::default()
57    }
58
59    pub fn delete() -> delete::Builder<PhantomData<Url>, PhantomData<String>> {
60        delete::Builder::default()
61    }
62
63    pub async fn main(self) -> Result<ErrorCode> {
64        match self {
65            Self::Create(configuration) => Create::try_from(configuration)?.main().await,
66            Self::Delete(configuration) => Delete::try_from(configuration)?.main().await,
67        }
68    }
69}