feat: validate configuration

This commit is contained in:
Marc Plano-Lesay 2025-05-21 22:02:42 +10:00
parent ee7d7971be
commit 48c670f455
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
33 changed files with 1076 additions and 165 deletions

View file

@ -0,0 +1,48 @@
use crate::actions::bitmagnet::BitmagnetConfig;
use crate::config::Validate;
use crate::errors::{self};
use color_eyre::eyre::Result;
use url::Url;
impl Validate for BitmagnetConfig {
fn validate(&self) -> Result<()> {
Url::parse(&self.host.to_string())
.map_err(|e| errors::config_error(e, &format!("Invalid URL format: {}", self.host)))?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::types::Host;
fn create_valid_config() -> Result<BitmagnetConfig> {
Ok(BitmagnetConfig {
enable: true,
host: Host::try_new("http://localhost:8080")?,
})
}
#[test]
fn test_valid_config() -> Result<()> {
let config = create_valid_config()?;
assert!(config.validate().is_ok());
Ok(())
}
#[test]
fn test_invalid_url() -> Result<()> {
let config = BitmagnetConfig {
enable: true,
host: Host::try_new("not a url")?,
};
assert!(config.validate().is_err());
Ok(())
}
}