start to implement the configuration file and mrproxy integration

This commit is contained in:
2025-12-20 03:56:46 +00:00
parent 7cabefc76e
commit 5045c05f9f
11 changed files with 323 additions and 15 deletions

102
src/config/mod.rs Normal file
View File

@@ -0,0 +1,102 @@
use std::{error::Error, fs::read_to_string, io};
use toml::Table;
pub const CONFIG_PATH: &str = "config/config.tom";
enum DockerConnectionKind {
HTTP,
HTTP_DEFAULT,
UNIX,
UNIX_DEFAULT,
}
pub enum MrproxyConnectionKind {
TCP,
UNIX,
}
pub struct DockerConnectionConfig {
connection_kind: DockerConnectionKind,
connection_string: Option<String>,
}
pub struct MrproxyConnectionData {
pub connection_kind: MrproxyConnectionKind,
pub connection_string: String,
}
impl DockerConnectionConfig {
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
let connection_kind_string = stored_file["docker"]["connection"].as_str().ok_or(
generate_toml_parser_error_in_field("docker connection kind"),
)?;
let connection_kind = match connection_kind_string {
"http" => DockerConnectionKind::HTTP,
"http_default" => DockerConnectionKind::HTTP_DEFAULT,
"unix" => DockerConnectionKind::UNIX,
"unix_default" => DockerConnectionKind::UNIX_DEFAULT,
_ => {
return Err(Box::new(generate_toml_parser_error_in_field(
"docker connection kind",
)));
}
};
let connection_string = match connection_kind {
DockerConnectionKind::HTTP | DockerConnectionKind::UNIX => Some(
stored_file["docker"]["string"]
.as_str()
.ok_or(generate_toml_parser_error_in_field(
"docker connection string",
))?
.to_string(),
),
_ => None,
};
Ok(Self {
connection_kind,
connection_string,
})
}
}
impl MrproxyConnectionData {
pub fn get_config(file_name: &str) -> Result<Self, Box<dyn Error>> {
let stored_file = read_to_string(file_name)?.parse::<Table>()?;
let connection_kind_string = stored_file["mrproxy"]["connection"].as_str().ok_or(
generate_toml_parser_error_in_field("mrproxy connection kind"),
)?;
let connection_kind = match connection_kind_string {
"tcp" => MrproxyConnectionKind::TCP,
"unix" => MrproxyConnectionKind::UNIX,
_ => {
return Err(Box::new(generate_toml_parser_error_in_field(
"mrproxy connection kind",
)));
}
};
let connection_string = stored_file["mrproxy"]["string"]
.as_str()
.ok_or(generate_toml_parser_error_in_field(
"mrproxy connection string",
))?
.to_string();
Ok(Self {
connection_kind,
connection_string,
})
}
}
fn generate_toml_parser_error_in_field(field: &str) -> io::Error {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Invalid format for config.toml in {} field", field),
)
}