Skip to content

Commit

Permalink
Move None checks into CertPaths::load()
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Jul 3, 2024
1 parent f9bf59f commit 934b91a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,7 @@ pub fn load_native_certs() -> Result<Vec<CertificateDer<'static>>, 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<Option<Vec<CertificateDer<'static>>>, Error> {
Ok(match CertPaths::from_env() {
CertPaths {
file: None,
dir: None,
} => None,
paths => Some(paths.load()?),
})
CertPaths::from_env().load()
}

struct CertPaths {
Expand All @@ -161,7 +155,11 @@ impl CertPaths {
}
}

fn load(&self) -> Result<Vec<CertificateDer<'static>>, Error> {
fn load(&self) -> Result<Option<Vec<CertificateDer<'static>>>, 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(),
Expand All @@ -174,7 +172,7 @@ impl CertPaths {
certs.sort_unstable_by(|a, b| a.cmp(b));
certs.dedup();

Ok(certs)
Ok(Some(certs))
}
}

Expand Down Expand Up @@ -328,23 +326,23 @@ 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,
dir: Some(dir_path.clone()),
}
.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),
dir: Some(dir_path),
}
.load()
.unwrap();
assert_eq!(certs_from_both.len(), 2);
assert_eq!(certs_from_both.unwrap().len(), 2);
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pub fn load_native_certs() -> Result<Vec<CertificateDer<'static>>, Error> {
dir: likely_locations.cert_dir,
}
.load()
.map(|certs| certs.unwrap_or_default())
}

0 comments on commit 934b91a

Please sign in to comment.