From 934b91aaa371890a5a0c53fe7e2b4ec6663a82a4 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 2 Jul 2024 11:49:55 +0200 Subject: [PATCH] Move None checks into CertPaths::load() --- src/lib.rs | 22 ++++++++++------------ src/unix.rs | 1 + 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b2737d4..78947c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -139,13 +139,7 @@ pub fn load_native_certs() -> Result>, Error> { /// subject to the rules outlined above for SSL_CERT_FILE. The directory is not /// scanned recursively and may be empty. fn load_certs_from_env() -> Result>>, Error> { - Ok(match CertPaths::from_env() { - CertPaths { - file: None, - dir: None, - } => None, - paths => Some(paths.load()?), - }) + CertPaths::from_env().load() } struct CertPaths { @@ -161,7 +155,11 @@ impl CertPaths { } } - fn load(&self) -> Result>, Error> { + fn load(&self) -> Result>>, Error> { + if self.file.is_none() && self.dir.is_none() { + return Ok(None); + } + let mut certs = match &self.file { Some(cert_file) => load_pem_certs(cert_file)?, None => Vec::new(), @@ -174,7 +172,7 @@ impl CertPaths { certs.sort_unstable_by(|a, b| a.cmp(b)); certs.dedup(); - Ok(certs) + Ok(Some(certs)) } } @@ -328,7 +326,7 @@ mod tests { } .load() .unwrap(); - assert_eq!(certs_from_file.len(), 2); + assert_eq!(certs_from_file.unwrap().len(), 2); let certs_from_dir = CertPaths { file: None, @@ -336,7 +334,7 @@ mod tests { } .load() .unwrap(); - assert_eq!(certs_from_dir.len(), 2); + assert_eq!(certs_from_dir.unwrap().len(), 2); let certs_from_both = CertPaths { file: Some(file_path), @@ -344,7 +342,7 @@ mod tests { } .load() .unwrap(); - assert_eq!(certs_from_both.len(), 2); + assert_eq!(certs_from_both.unwrap().len(), 2); } #[test] diff --git a/src/unix.rs b/src/unix.rs index dbaabfe..d33e67b 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -11,4 +11,5 @@ pub fn load_native_certs() -> Result>, Error> { dir: likely_locations.cert_dir, } .load() + .map(|certs| certs.unwrap_or_default()) }