1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Coupling {
8 pub name: String,
10 pub source: String,
12 pub target: String,
14 #[serde(rename = "type")]
16 pub coupling_type: CouplingType,
17 pub force_transfer: ForceTransfer,
19 #[serde(default = "default_coupling_strength")]
21 pub strength: f64,
22}
23
24fn default_coupling_strength() -> f64 {
25 1.0
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
30#[serde(rename_all = "kebab-case")]
31pub enum CouplingType {
32 OneWay,
34 TwoWay,
36 OverlapRegion,
38 Interface,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
44#[serde(rename_all = "lowercase")]
45pub enum ForceTransfer {
46 Direct,
48 Lorentz,
50 Contact,
52 Penalty,
54 Lagrange,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct OverlapRegion {
61 #[serde(rename = "type")]
63 pub region_type: RegionType,
64 #[serde(flatten)]
66 pub params: std::collections::HashMap<String, serde_json::Value>,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
71#[serde(rename_all = "lowercase")]
72pub enum RegionType {
73 Aabb,
75 Sphere,
77 Cylinder,
79 Mesh,
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_coupling_serialization() {
89 let coupling = Coupling {
90 name: "rigid-em".to_string(),
91 source: "rigid_body".to_string(),
92 target: "em".to_string(),
93 coupling_type: CouplingType::TwoWay,
94 force_transfer: ForceTransfer::Lorentz,
95 strength: 1.0,
96 };
97
98 let json = serde_json::to_string(&coupling).unwrap();
99 let parsed: Coupling = serde_json::from_str(&json).unwrap();
100
101 assert_eq!(parsed.name, "rigid-em");
102 assert_eq!(parsed.coupling_type, CouplingType::TwoWay);
103 assert_eq!(parsed.force_transfer, ForceTransfer::Lorentz);
104 }
105
106 #[test]
107 fn test_coupling_type_serialization() {
108 let ct = CouplingType::OverlapRegion;
109 let json = serde_json::to_string(&ct).unwrap();
110 assert_eq!(json, "\"overlap-region\"");
111 }
112
113 #[test]
114 fn test_force_transfer_serialization() {
115 let ft = ForceTransfer::Penalty;
116 let json = serde_json::to_string(&ft).unwrap();
117 assert_eq!(json, "\"penalty\"");
118 }
119}