audb_core/tools/
validation.rs

1use anyhow::{anyhow, Result};
2use std::path::Path;
3
4pub fn validate_ip_address(ip: &str) -> Result<()> {
5    ip.parse::<std::net::IpAddr>()
6        .map(|_| ())
7        .map_err(|_| anyhow!("Invalid IP address format"))
8}
9
10pub fn validate_port(port: u16) -> Result<()> {
11    if port == 0 {
12        return Err(anyhow!("Port cannot be 0"));
13    }
14    Ok(())
15}
16
17pub fn validate_ssh_key_exists(path: &Path) -> Result<()> {
18    if !path.exists() {
19        return Err(anyhow!("SSH key file does not exist: {}", path.display()));
20    }
21    if !path.is_file() {
22        return Err(anyhow!("SSH key path is not a file: {}", path.display()));
23    }
24    Ok(())
25}
26
27pub fn validate_rpm_exists(path: &Path) -> Result<()> {
28    if !path.exists() {
29        return Err(anyhow!("RPM file does not exist: {}", path.display()));
30    }
31    if !path.is_file() {
32        return Err(anyhow!("RPM path is not a file: {}", path.display()));
33    }
34    if path.extension().and_then(|s| s.to_str()) != Some("rpm") {
35        return Err(anyhow!("File is not an RPM package: {}", path.display()));
36    }
37    Ok(())
38}