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

@ -23,15 +23,15 @@ impl TransmissionClient {
let url = Url::parse(&url_str).wrap_err_with(|| format!("Invalid URL: {}", url_str))?;
let auth = BasicAuth {
user: config.username.clone(),
password: config.password.clone(),
user: config.username.to_string(),
password: config.password.to_string(),
};
let client = TransClient::with_auth(url, auth);
Ok(TransmissionClient {
client,
download_dir: config.download_dir.clone(),
download_dir: config.download_dir.to_string(),
})
}

View file

@ -1,15 +1,16 @@
use crate::app::Enableable;
use crate::config::types::{AbsoluteDownloadDir, Host, Password, Port, Username};
use serde::{Deserialize, Serialize};
/// Configuration for the Transmission action
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransmissionConfig {
pub enable: bool,
pub host: String,
pub username: String,
pub password: String,
pub port: u16,
pub download_dir: String,
pub host: Host,
pub username: Username,
pub password: Password,
pub port: Port,
pub download_dir: AbsoluteDownloadDir,
}
impl Enableable for TransmissionConfig {

View file

@ -1,6 +1,7 @@
pub mod action;
pub mod client;
pub mod config;
pub mod validation;
pub use action::TransmissionAction;
pub use config::TransmissionConfig;

View file

@ -0,0 +1,33 @@
use crate::actions::transmission::TransmissionConfig;
use crate::config::Validate;
use color_eyre::eyre::Result;
impl Validate for TransmissionConfig {
fn validate(&self) -> Result<()> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::types::{AbsoluteDownloadDir, Host, Password, Port, Username};
fn create_valid_config() -> Result<TransmissionConfig> {
Ok(TransmissionConfig {
enable: true,
host: Host::try_new("localhost")?,
username: Username::try_new("user")?,
password: Password::try_new("pass")?,
port: Port::try_new(9091)?,
download_dir: AbsoluteDownloadDir::try_new("/downloads")?,
})
}
#[test]
fn test_valid_config() -> Result<()> {
let config = create_valid_config()?;
assert!(config.validate().is_ok());
Ok(())
}
}