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