trz_gateway_common/security_configuration/trusted_store/
load.rs

1use nameth::NamedEnumValues as _;
2use nameth::nameth;
3
4use super::cache::CachedTrustedStoreConfig;
5use super::native::NativeTrustedStoreConfig;
6use super::pem::PemTrustedStore;
7use super::pem::PemTrustedStoreError;
8use crate::unwrap_infallible::UnwrapInfallible as _;
9
10#[derive(Clone, Copy)]
11pub enum LoadTrustedStore<'t> {
12    Native,
13    PEM(&'t str),
14    File(&'t str),
15}
16
17impl LoadTrustedStore<'_> {
18    pub fn load(self) -> Result<CachedTrustedStoreConfig, LoadTrustedStoreError> {
19        match self {
20            LoadTrustedStore::Native => {
21                Ok(CachedTrustedStoreConfig::new(NativeTrustedStoreConfig).unwrap_infallible())
22            }
23            LoadTrustedStore::PEM(root_certificates_pem) => {
24                Ok(CachedTrustedStoreConfig::new(PemTrustedStore {
25                    root_certificates_pem: root_certificates_pem.to_owned(),
26                })?)
27            }
28            LoadTrustedStore::File(pem_file) => {
29                LoadTrustedStore::PEM(&std::fs::read_to_string(pem_file)?).load()
30            }
31        }
32    }
33}
34
35#[nameth]
36#[derive(thiserror::Error, Debug)]
37pub enum LoadTrustedStoreError {
38    #[error("[{n}] {0}", n = self.name())]
39    LoadPem(#[from] PemTrustedStoreError),
40
41    #[error("[{n}] {0}", n = self.name())]
42    LoadFile(#[from] std::io::Error),
43}