main
use std::net::IpAddr;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default)]
pub struct ApihostConfig {
pub enabled: Option<bool>,
pub bind_addr: Option<String>,
pub port: Option<u16>,
}
impl ApihostConfig {
pub fn enabled(&self) -> bool {
self.enabled.unwrap_or(true)
}
pub fn bind_addr(&self) -> &str {
self.bind_addr.as_deref().unwrap_or("0.0.0.0")
}
pub fn port(&self) -> u16 {
self.port.unwrap_or(3000)
}
pub fn is_valid(&self) -> Result<(), String> {
if !self.enabled() {
return Ok(());
}
let bind = self.bind_addr();
if bind.parse::<IpAddr>().is_err() {
return Err(format!("apihost.bind_addr is not a valid IP: {bind:?}"));
}
if self.port == Some(0) {
return Err("apihost.port must be greater than 0".to_string());
}
Ok(())
}
}