credence_lib/configuration/port/
host.rs

1use super::{super::error::*, acme::*, key::*};
2
3use {
4    compris::resolve::*,
5    kutil::{cli::depict::*, std::immutable::*},
6    std::path::*,
7};
8
9//
10// Host
11//
12
13/// Host.
14#[derive(Clone, Debug, Default, Depict, Resolve)]
15pub struct Host {
16    /// Name.
17    #[resolve(single)]
18    #[depict(style(string))]
19    pub name: ByteString,
20
21    /// Whether to redirect all requests to this port.
22    #[resolve(key = "redirect-to")]
23    #[depict(option, style(number))]
24    pub redirect_to: Option<u16>,
25
26    /// Optional key configuration.
27    #[resolve]
28    #[depict(option, as(depict))]
29    pub key: Option<Key>,
30
31    /// Optional ACME configuration.
32    #[resolve]
33    #[depict(option, as(depict))]
34    pub acme: Option<ACME>,
35}
36
37impl Host {
38    /// Validate.
39    pub fn validate<PathT>(&mut self, base_path: PathT) -> Result<(), ConfigurationError>
40    where
41        PathT: AsRef<Path>,
42    {
43        if self.key.is_some() && self.acme.is_some() {
44            return Err("host cannot have both `key` and `acme`".into());
45        }
46
47        let base_path = base_path.as_ref();
48
49        if let Some(key) = &mut self.key {
50            key.validate(base_path)?;
51        }
52
53        if let Some(acme) = &mut self.acme {
54            acme.validate(base_path)?;
55        }
56
57        Ok(())
58    }
59
60    /// Whether we have TLS.
61    pub fn has_tls(&self) -> bool {
62        self.key.is_some() || self.acme.is_some()
63    }
64}