[Feedback] Add comment to get_last_path
@@ -70,8 +70,9 @@ pub fn generate_self_signed_certificate_and_key(subject_alternate_name: DNSNameR
Ok(())
}
-fn get_latest_path(path: impl AsRef<Path>, pattern: &str) -> Result<PathBuf, Error> {
- let pattern = path.as_ref().join(pattern);
+/// Search for files in `search_directory` that match `file_pattern` and return the last match
+fn get_last_path(search_directory: impl AsRef<Path>, file_pattern: &str) -> Result<PathBuf, Error> {
+ let pattern = search_directory.as_ref().join(file_pattern);
let pattern = pattern.to_string_lossy();
glob::glob(&pattern).expect("valid pattern")
.filter_map(std::result::Result::ok)
@@ -81,17 +82,17 @@ fn get_latest_path(path: impl AsRef<Path>, pattern: &str) -> Result<PathBuf, Err
}
pub fn latest_certificates(path: impl AsRef<Path>) -> Result<Vec<Certificate>, Error> {
- let path = get_latest_path(path, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION))?;
+ let path = get_last_path(path, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION))?;
Ok(rustls_wrapper::certificates_from_file(&path)?)
}
pub fn latest_private_key(path: impl AsRef<Path>) -> Result<PrivateKey, Error> {
- let path = get_latest_path(path, &format!("{}-*.{}", NAME_PREFIX, KEY_EXTENSION))?;
+ let path = get_last_path(path, &format!("{}-*.{}", NAME_PREFIX, KEY_EXTENSION))?;
Ok(rustls_wrapper::private_key_from_file(&path)?)
}
pub fn latest_root_certificate_store(path: impl AsRef<Path>) -> Result<RootCertStore, Error> {
- let path = get_latest_path(path, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION))?;
+ let path = get_last_path(path, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION))?;
Ok(rustls_wrapper::root_certificate_store_from_file(&path)?)
}
@@ -110,7 +111,7 @@ mod tests {
generate_self_signed_certificate_and_key(DNSNameRef::try_from_ascii_str("a_test_domain").unwrap(), &tmpdir).unwrap();
assert_eq!(fs::read_dir(&tmpdir).unwrap().count(), 2);
let first_path =
- get_latest_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
+ get_last_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
let certificates = latest_certificates(&tmpdir).unwrap();
assert_eq!(certificates.len(), 1);
latest_private_key(&tmpdir).unwrap();
@@ -124,7 +125,7 @@ mod tests {
generate_self_signed_certificate_and_key(DNSNameRef::try_from_ascii_str("another_domain").unwrap(), &tmpdir).unwrap();
assert_eq!(fs::read_dir(&tmpdir).unwrap().count(), 4);
let second_path =
- get_latest_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
+ get_last_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
let certificates = latest_certificates(&tmpdir).unwrap();
assert_eq!(certificates.len(), 1);
latest_private_key(&tmpdir).unwrap();
