prosa_utils/
config.rs

1//! Module for ProSA configuration object
2//!
3//! <svg width="40" height="40">
4#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/doc_assets/settings.svg"))]
5//! </svg>
6
7use std::path::PathBuf;
8
9use thiserror::Error;
10
11// Feature openssl or rusttls,...
12pub mod ssl;
13
14// Feature opentelemetry
15#[cfg(feature = "config-observability")]
16pub mod observability;
17
18// Feature tracing
19#[cfg(feature = "config-observability")]
20pub mod tracing;
21
22/// Error define for configuration object
23#[derive(Debug, Error)]
24pub enum ConfigError {
25    /// Error that indicate a wrong path format in filesystem
26    #[error("The config parameter {0} have an incorrect value `{1}`")]
27    WrongValue(String, String),
28    /// Error that indicate a wrong path format pattern in filesystem
29    #[error("The path `{0}` provided don't match the pattern `{1}`")]
30    WrongPathPattern(String, glob::PatternError),
31    /// Error that indicate a wrong path format in filesystem
32    #[error("The path `{0}` provided is not correct")]
33    WrongPath(PathBuf),
34    /// Error on a file read
35    #[error("The file `{0}` can't be read `{1}`")]
36    IoFile(String, std::io::Error),
37    #[cfg(feature = "config-openssl")]
38    /// SSL error
39    #[error("Openssl error `{0}`")]
40    OpenSsl(#[from] openssl::error::ErrorStack),
41}
42
43/// Method to get the country name from the OS
44pub fn os_country() -> Option<String> {
45    if let Some(lang) = option_env!("LANG") {
46        let language = if let Some(pos) = lang.find('.') {
47            &lang[..pos]
48        } else {
49            lang
50        };
51
52        if let Some(pos) = language.find('_') {
53            return Some(String::from(&language[pos + 1..]));
54        }
55    }
56
57    None
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_os_country() {
66        let country = os_country();
67        if let Some(cn) = country {
68            assert_eq!(2, cn.len());
69        }
70    }
71}