1use axum_server::tls_rustls::RustlsConfig;
6use std::io;
7use std::path::{Path, PathBuf};
8
9#[derive(Debug, thiserror::Error)]
11pub enum TlsError {
12 #[error("IO error: {0}")]
13 Io(#[from] io::Error),
14
15 #[error("Failed to load certificate: {0}")]
16 CertificateError(String),
17
18 #[error("Failed to load private key: {0}")]
19 PrivateKeyError(String),
20
21 #[error("TLS configuration error: {0}")]
22 ConfigError(String),
23}
24
25pub type TlsResult<T> = Result<T, TlsError>;
26
27#[derive(Debug, Clone)]
29pub struct TlsConfig {
30 pub cert_path: PathBuf,
32 pub key_path: PathBuf,
34}
35
36impl TlsConfig {
37 pub fn new(cert_path: impl AsRef<Path>, key_path: impl AsRef<Path>) -> Self {
39 Self {
40 cert_path: cert_path.as_ref().to_path_buf(),
41 key_path: key_path.as_ref().to_path_buf(),
42 }
43 }
44
45 pub async fn build_server_config(&self) -> TlsResult<RustlsConfig> {
49 RustlsConfig::from_pem_file(&self.cert_path, &self.key_path)
50 .await
51 .map_err(|e| TlsError::ConfigError(format!("Failed to load TLS configuration: {}", e)))
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn test_tls_config_creation() {
61 let config = TlsConfig::new("cert.pem", "key.pem");
62 assert_eq!(config.cert_path, PathBuf::from("cert.pem"));
63 assert_eq!(config.key_path, PathBuf::from("key.pem"));
64 }
65
66 }