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