odilia_common/settings/
mod.rs

1mod log;
2mod speech;
3use log::LogSettings;
4use speech::SpeechSettings;
5
6use serde::{Deserialize, Serialize};
7use tini::Ini;
8
9use crate::errors::ConfigError;
10
11///type representing a *read-only* view of the odilia screenreader configuration
12/// this type should only be obtained as a result of parsing odilia's configuration files, as it containes types for each section responsible for controlling various parts of the screenreader
13/// the only way this config should change is if the configuration file changes, in which case the entire view will be replaced to reflect the fact
14#[derive(Debug, Serialize, Deserialize)]
15pub struct ApplicationConfig {
16	speech: SpeechSettings,
17	log: LogSettings,
18}
19
20impl ApplicationConfig {
21	/// Opens a new config file with a certain path.
22	///
23	/// # Errors
24	///
25	/// This can return `Err(_)` if the path doesn't exist, or if not all the key/value pairs are defined.
26	pub fn new(path: &str) -> Result<Self, ConfigError> {
27		let ini = Ini::from_file(path)?;
28		let rate: i32 = ini.get("speech", "rate").ok_or(ConfigError::ValueNotFound)?;
29		let level: String = ini.get("log", "level").ok_or(ConfigError::ValueNotFound)?;
30		let speech = SpeechSettings::new(rate);
31		let log = LogSettings::new(level);
32		Ok(Self { speech, log })
33	}
34
35	#[must_use]
36	pub fn log(&self) -> &LogSettings {
37		&self.log
38	}
39
40	#[must_use]
41	pub fn speech(&self) -> &SpeechSettings {
42		&self.speech
43	}
44}