credence_lib/configuration/port/
key.rs

1use super::{super::error::*, loadable_bytes::*};
2
3use {
4    compris::resolve::*,
5    kutil::{cli::depict::*, std::immutable::*},
6    std::path::*,
7};
8
9//
10// Key
11//
12
13/// TLS key.
14#[derive(Clone, Debug, Default, Depict, Resolve)]
15pub struct Key {
16    /// Certificates PEM.
17    #[resolve(required)]
18    #[depict(as(display), style(symbol))]
19    pub certificates: LoadableBytes,
20
21    /// Private key PEM.
22    #[resolve(required, key = "private-key")]
23    #[depict(as(display), style(symbol))]
24    pub private_key: LoadableBytes,
25}
26
27impl Key {
28    /// Validate.
29    pub fn validate<PathT>(&mut self, base_path: PathT) -> Result<(), ConfigurationError>
30    where
31        PathT: AsRef<Path>,
32    {
33        if let LoadableBytes::Path(path) = &mut self.certificates
34            && !path.is_absolute()
35        {
36            *path = base_path.as_ref().join(&path);
37        }
38
39        if let LoadableBytes::Path(path) = &mut self.private_key
40            && !path.is_absolute()
41        {
42            *path = base_path.as_ref().join(&path);
43        }
44
45        Ok(())
46    }
47
48    /// Ensures both certificates and private key are loaded.
49    pub fn to_bytes(&self) -> Result<(Bytes, Bytes), ConfigurationError> {
50        Ok((self.certificates.to_bytes()?, self.private_key.to_bytes()?))
51    }
52}