tansu_cli/
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::{collections::HashMap, env::vars, fmt, result, str::FromStr};
16
17mod cli;
18
19pub use cli::Cli;
20use regex::{Regex, Replacer};
21
22#[derive(thiserror::Error, Debug)]
23pub enum Error {
24    Box(#[from] Box<dyn std::error::Error + Send + Sync>),
25    Cat(Box<tansu_cat::Error>),
26    DotEnv(#[from] dotenv::Error),
27    Generate(#[from] tansu_generator::Error),
28    Proxy(#[from] tansu_proxy::Error),
29    Regex(#[from] regex::Error),
30    Schema(Box<tansu_schema::Error>),
31    Server(Box<tansu_broker::Error>),
32    Topic(#[from] tansu_topic::Error),
33    Url(#[from] url::ParseError),
34}
35
36impl From<tansu_cat::Error> for Error {
37    fn from(value: tansu_cat::Error) -> Self {
38        Self::Cat(Box::new(value))
39    }
40}
41
42impl From<tansu_schema::Error> for Error {
43    fn from(value: tansu_schema::Error) -> Self {
44        Self::Schema(Box::new(value))
45    }
46}
47
48impl From<tansu_broker::Error> for Error {
49    fn from(value: tansu_broker::Error) -> Self {
50        Self::Server(Box::new(value))
51    }
52}
53
54impl fmt::Display for Error {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        write!(f, "{self:?}")
57    }
58}
59
60pub type Result<T, E = Error> = result::Result<T, E>;
61
62#[derive(Clone, Debug)]
63pub struct VarRep(HashMap<String, String>);
64
65impl From<HashMap<String, String>> for VarRep {
66    fn from(value: HashMap<String, String>) -> Self {
67        Self(value)
68    }
69}
70
71impl VarRep {
72    fn replace(&self, haystack: &str) -> Result<String> {
73        Regex::new(r"\$\{(?<var>[^\}]+)\}")
74            .map(|re| re.replace(haystack, self).into_owned())
75            .map_err(Into::into)
76    }
77}
78
79impl Replacer for &VarRep {
80    fn replace_append(&mut self, caps: &regex::Captures<'_>, dst: &mut String) {
81        if let Some(variable) = caps.name("var")
82            && let Some(value) = self.0.get(variable.as_str())
83        {
84            dst.push_str(value);
85        }
86    }
87}
88
89#[derive(Clone, Debug)]
90pub struct EnvVarExp<T>(T);
91
92impl<T> EnvVarExp<T> {
93    pub fn into_inner(self) -> T {
94        self.0
95    }
96}
97
98impl<T> FromStr for EnvVarExp<T>
99where
100    T: FromStr,
101    Error: From<<T as FromStr>::Err>,
102{
103    type Err = Error;
104
105    fn from_str(s: &str) -> Result<Self, Self::Err> {
106        VarRep::from(vars().collect::<HashMap<_, _>>())
107            .replace(s)
108            .and_then(|s| T::from_str(&s).map_err(Into::into))
109            .map(|t| Self(t))
110    }
111}