//! # Configuration Module //! //! This module defines the `Configuration` struct, which is responsible for //! loading and managing the application's configuration settings from a TOML file. //! The configuration includes parameters such as the PAM service name, //! commands for login and user sessions, and the user to run login sessions as. //! The `Configuration` struct provides methods to access these settings and implements //! the `Display` trait for easy debugging and logging. use crate::APPLICATION_NAME; use core::fmt; use glib::g_info; use std::path::PathBuf; use toml::Value; /// Configuration /// /// Represents the application's configuration settings loaded from a TOML file. pub struct Configuration { /// The name of the PAM service to use (default: "diya"). pam_service: String, /// The command to execute for login sessions (required). login_session_command: String, /// The user to run login sessions as (default: "root"). login_session_user: String, // optional (default root) /// The command to execute for user sessions (required). user_session_command: String, } impl Configuration { /// Load configuration from a TOML file. /// /// # Arguments //// /// * `file` - A `PathBuf` pointing to the configuration file. /// /// # Returns /// * `Ok(Configuration)` if the configuration was successfully loaded and parsed. /// * `Err(Box)` if there was an error reading the file or /// parsing the TOML content. pub fn from(file: &PathBuf) -> Result> { g_info!( APPLICATION_NAME, "Loading configuration from: {}", file.display() ); let content = std::fs::read_to_string(file)?; let content = content.parse::()?; let table = content.as_table().ok_or("Invalid configuration format")?; Ok(Configuration { pam_service: table .get("pam_service") .and_then(Value::as_str) .unwrap_or("diya") .to_string(), login_session_command: table .get("login_session_command") .and_then(Value::as_str) .ok_or("Missing login session command")? .to_string(), login_session_user: table .get("login_session_user") .and_then(Value::as_str) .unwrap_or("root") .to_string(), user_session_command: table .get("user_session_command") .and_then(Value::as_str) .ok_or("Missing user session command")? .to_string(), }) } /// Get the PAM service name. /// /// # Returns /// * A string slice representing the PAM service name. /// Defaults to "diya" if not specified in the configuration. pub fn pam_service(&self) -> &str { &self.pam_service } /// Get the login session command. /// /// # Returns /// * A string slice representing the login session command. pub fn login_session_command(&self) -> &str { &self.login_session_command } /// Get the login session user. /// /// # Returns /// * A string slice representing the user to run login sessions as. /// * Defaults to "root" if not specified in the configuration. pub fn login_session_user(&self) -> &str { &self.login_session_user } /// Get the user session command. /// /// # Returns /// * A string slice representing the user session command. pub fn user_session_command(&self) -> &str { &self.user_session_command } } impl fmt::Display for Configuration { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "Configuration:")?; writeln!(f, " - PAM Service: {}", self.pam_service)?; writeln!( f, " - Login Session Command: {}", self.login_session_command )?; writeln!(f, " - Login Session User: {}", self.login_session_user)?; writeln!(f, " - User Session Command: {}", self.user_session_command)?; Ok(()) } }