From 4effbd209edf96637d7da2b7d29ea1a6de3c637a Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 11 Mar 2026 17:38:08 +0800 Subject: Add config system and space macro with workspace dependencies --- systems/_config/src/rw.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 systems/_config/src/rw.rs (limited to 'systems/_config/src/rw.rs') diff --git a/systems/_config/src/rw.rs b/systems/_config/src/rw.rs new file mode 100644 index 0000000..36ea00c --- /dev/null +++ b/systems/_config/src/rw.rs @@ -0,0 +1,66 @@ +use crate::error::ConfigError; +use serde::{Deserialize, Serialize}; +use std::path::Path; + +pub async fn read_config(path: impl AsRef) -> Result +where + C: for<'a> Deserialize<'a>, +{ + let path_ref = path.as_ref(); + let ext = path_ref + .extension() + .and_then(|ext| ext.to_str()) + .unwrap_or("json"); + let format = ext_fmt(&ext.to_lowercase()); + + let content = std::fs::read_to_string(path_ref).map_err(ConfigError::ConfigReadFailed)?; + + match format { + Format::Yaml => { + serde_yaml::from_str(&content).map_err(|e| ConfigError::Other(e.to_string())) + } + Format::Toml => toml::from_str(&content).map_err(|e| ConfigError::Other(e.to_string())), + Format::Json => { + serde_json::from_str(&content).map_err(|e| ConfigError::Other(e.to_string())) + } + } +} + +pub async fn write_config(path: impl AsRef, config: &C) -> Result<(), ConfigError> +where + C: Serialize, +{ + let path_ref = path.as_ref(); + let ext = path_ref + .extension() + .and_then(|ext| ext.to_str()) + .unwrap_or("json"); + let format = ext_fmt(&ext.to_lowercase()); + + let content = match format { + Format::Yaml => { + serde_yaml::to_string(config).map_err(|e| ConfigError::Other(e.to_string()))? + } + Format::Toml => toml::to_string(config).map_err(|e| ConfigError::Other(e.to_string()))?, + Format::Json => { + serde_json::to_string_pretty(config).map_err(|e| ConfigError::Other(e.to_string()))? + } + }; + + std::fs::write(path_ref, content).map_err(ConfigError::ConfigWriteFailed) +} + +enum Format { + Yaml, + Toml, + Json, +} + +fn ext_fmt(ext: &str) -> Format { + match ext { + "yaml" | "yml" => Format::Yaml, + "toml" | "tml" => Format::Toml, + "json" => Format::Json, + _ => Format::Json, + } +} -- cgit