feat: validate configuration
This commit is contained in:
parent
ee7d7971be
commit
48c670f455
33 changed files with 1076 additions and 165 deletions
48
src/actions/bitmagnet/validation.rs
Normal file
48
src/actions/bitmagnet/validation.rs
Normal 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(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue