use serde::Deserialize;
use std::net::{TcpListener, ToSocketAddrs};
use crate::error::{Error, ErrorKind};
#[derive(Debug, Deserialize, Clone)]
pub struct Dashboard {
#[serde(default = "Dashboard::default_enable")]
enable: bool,
#[serde(default = "Dashboard::default_address")]
address: String,
}
impl Dashboard {
const fn default_enable() -> bool {
true
}
fn default_address() -> String {
"127.0.0.1:18083".to_string()
}
#[must_use]
pub const fn enable(&self) -> bool {
self.enable
}
#[must_use]
pub fn address(&self) -> &str {
&self.address
}
pub fn validate(&self, bind_address: bool) -> Result<(), Error> {
if self.enable {
if bind_address {
let _socket = TcpListener::bind(&self.address).map_err(|err| {
Error::from_string(
ErrorKind::ConfigError,
format!(
"Failed to bind to address {} for dashboard, err: {:?}",
&self.address, err
),
)
})?;
} else {
let _addr = self.address.to_socket_addrs().map_err(|err| {
Error::from_string(
ErrorKind::ConfigError,
format!(
"Invalid socket address in config: {}, err: {:?}",
&self.address, err
),
)
})?;
}
}
Ok(())
}
}
impl Default for Dashboard {
fn default() -> Self {
Self {
enable: Self::default_enable(),
address: Self::default_address(),
}
}
}