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