pass_it_on/configuration/
server_configuration_file.rs

1use crate::configuration::{collect_endpoints, collect_interfaces, ServerConfiguration};
2use crate::endpoints::{Endpoint, EndpointConfig};
3use crate::interfaces::{Interface, InterfaceConfig};
4use crate::Error;
5use serde::Deserialize;
6
7/// Server configuration parsed from TOML that handles any [`InterfaceConfig`][`crate::interfaces::InterfaceConfig`]
8/// and [`EndpointConfig`][`crate::endpoints::EndpointConfig`].
9#[derive(Deserialize, Debug)]
10pub(super) struct ServerConfigFileParser {
11    server: ServerConfigFile,
12}
13
14/// Serde compatible representation of [`ServerConfiguration`]
15#[derive(Deserialize, Debug)]
16pub struct ServerConfigFile {
17    key: String,
18    interface: Vec<Box<dyn InterfaceConfig>>,
19    endpoint: Vec<Box<dyn EndpointConfig>>,
20}
21
22impl ServerConfigFileParser {
23    /// Parse [`ServerConfiguration`] from provided TOML
24    pub fn from(string: &str) -> Result<ServerConfiguration, Error> {
25        let parsed: ServerConfigFileParser = toml::from_str(string)?;
26        parsed.server.try_into()
27    }
28}
29
30impl TryFrom<ServerConfigFile> for ServerConfiguration {
31    type Error = Error;
32
33    fn try_from(value: ServerConfigFile) -> Result<Self, Self::Error> {
34        let interfaces: Vec<Box<dyn Interface + Send>> = collect_interfaces(value.interface)?;
35        let endpoints: Vec<Box<dyn Endpoint + Send>> = collect_endpoints(value.endpoint)?;
36
37        ServerConfiguration::new(value.key.as_str(), interfaces, endpoints)
38    }
39}