use std::path::{Path, PathBuf}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use crate::stage0_config::PleaseConfigBuilder; #[derive(Debug, Clone, Serialize, Deserialize)] struct CuddleEmbeddedPleaseConfig { please: PleaseConfigBuilder, } impl From for PleaseConfigBuilder { fn from(value: CuddleEmbeddedPleaseConfig) -> Self { value.please } } #[derive(Debug, Clone, Serialize, Deserialize)] struct CuddlePleaseConfig { #[serde(flatten)] please: PleaseConfigBuilder, } impl From for PleaseConfigBuilder { fn from(value: CuddlePleaseConfig) -> Self { value.please } } const CUDDLE_FILE_NAME: &str = "cuddle"; const CUDDLE_CONFIG_FILE_NAME: &str = "cuddle.please"; const YAML_EXTENSION: &str = "yaml"; pub fn get_config_from_config_file(current_dir: &Path) -> PleaseConfigBuilder { let current_cuddle_path = current_dir .join(format!("{CUDDLE_FILE_NAME}.{YAML_EXTENSION}")); let current_cuddle_config_path = current_dir .join(format!("{CUDDLE_CONFIG_FILE_NAME}.{YAML_EXTENSION}")); let mut please_config = PleaseConfigBuilder::default(); if let Some(config) = get_config_from_file::(current_cuddle_path) { please_config = please_config.merge(&config).clone(); } if let Some(config) = get_config_from_file::(current_cuddle_config_path) { please_config = please_config.merge(&config).clone(); } please_config } pub fn get_config_from_file(current_cuddle_path: PathBuf) -> Option where T: DeserializeOwned, T: Into, { match std::fs::File::open(¤t_cuddle_path) { Ok(file) => match serde_yaml::from_reader::<_, T>(file) { Ok(config) => { return Some(config.into()); } Err(e) => { tracing::debug!( "{} doesn't contain a valid please config: {}", ¤t_cuddle_path.display(), e ); } }, Err(e) => { tracing::debug!( "did not find or was not allowed to read {}, error: {}", ¤t_cuddle_path.display(), e, ); } } None }