//! Component parameter serialization //! //! Provides types for extracting and serializing component-specific parameters. use serde::{Deserialize, Serialize}; use std::collections::HashMap; /// Serializable component parameters /// /// This type captures all component-specific configuration in a flexible format /// that can be serialized to JSON and later used to reconstruct components. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct ComponentParams { /// Component type (e.g., "Compressor", "Condenser", "ExpansionValve") pub component_type: String, /// Component-specific parameters as key-value pairs #[serde(flatten)] pub params: HashMap, } impl ComponentParams { /// Create a new ComponentParams for a given component type pub fn new(component_type: impl Into) -> Self { Self { component_type: component_type.into(), params: HashMap::new(), } } /// Add a parameter pub fn with_param( mut self, key: impl Into, value: impl Into, ) -> Self { self.params.insert(key.into(), value.into()); self } /// Get a parameter value pub fn get(&self, key: &str) -> Option<&serde_json::Value> { self.params.get(key) } /// Check if component is of a specific type pub fn is_type(&self, component_type: &str) -> bool { self.component_type == component_type } } #[cfg(test)] mod tests { use super::*; use serde_json::json; #[test] fn test_component_params_creation() { let params = ComponentParams::new("Compressor") .with_param("m1", 0.85) .with_param("m2", 2.5) .with_param("fluid", "R134a"); assert_eq!(params.component_type, "Compressor"); assert_eq!(params.get("m1"), Some(&json!(0.85))); assert_eq!(params.get("fluid"), Some(&json!("R134a"))); assert!(params.is_type("Compressor")); assert!(!params.is_type("Condenser")); } #[test] fn test_component_params_serialization() { let params = ComponentParams::new("TestComponent") .with_param("value1", 42) .with_param("value2", "test"); let json = serde_json::to_string(¶ms).unwrap(); let deserialized: ComponentParams = serde_json::from_str(&json).unwrap(); assert_eq!(params, deserialized); } }