solverforge_core/domain/
constraint_config.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
4pub struct ConstraintWeight {
5    pub constraint_name: String,
6    #[serde(default)]
7    pub constraint_package: Option<String>,
8}
9
10impl ConstraintWeight {
11    pub fn new(constraint_name: impl Into<String>) -> Self {
12        Self {
13            constraint_name: constraint_name.into(),
14            constraint_package: None,
15        }
16    }
17
18    pub fn with_package(constraint_name: impl Into<String>, package: impl Into<String>) -> Self {
19        Self {
20            constraint_name: constraint_name.into(),
21            constraint_package: Some(package.into()),
22        }
23    }
24
25    pub fn full_name(&self) -> String {
26        match &self.constraint_package {
27            Some(pkg) => format!("{}/{}", pkg, self.constraint_name),
28            None => self.constraint_name.clone(),
29        }
30    }
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
34pub struct ConstraintConfiguration;
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
37pub struct DeepPlanningClone;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_constraint_weight_new() {
45        let weight = ConstraintWeight::new("Room conflict");
46        assert_eq!(weight.constraint_name, "Room conflict");
47        assert!(weight.constraint_package.is_none());
48    }
49
50    #[test]
51    fn test_constraint_weight_with_package() {
52        let weight = ConstraintWeight::with_package("Room conflict", "timetabling");
53        assert_eq!(weight.constraint_name, "Room conflict");
54        assert_eq!(weight.constraint_package, Some("timetabling".to_string()));
55    }
56
57    #[test]
58    fn test_full_name() {
59        let weight1 = ConstraintWeight::new("Room conflict");
60        assert_eq!(weight1.full_name(), "Room conflict");
61
62        let weight2 = ConstraintWeight::with_package("Room conflict", "timetabling");
63        assert_eq!(weight2.full_name(), "timetabling/Room conflict");
64    }
65
66    #[test]
67    fn test_constraint_weight_json() {
68        let weight = ConstraintWeight::with_package("Room conflict", "timetabling");
69        let json = serde_json::to_string(&weight).unwrap();
70        assert!(json.contains("\"constraint_name\":\"Room conflict\""));
71        assert!(json.contains("\"constraint_package\":\"timetabling\""));
72
73        let parsed: ConstraintWeight = serde_json::from_str(&json).unwrap();
74        assert_eq!(parsed, weight);
75    }
76
77    #[test]
78    fn test_constraint_weight_json_without_package() {
79        let json = r#"{"constraint_name":"Room conflict"}"#;
80        let parsed: ConstraintWeight = serde_json::from_str(json).unwrap();
81        assert_eq!(parsed.constraint_name, "Room conflict");
82        assert!(parsed.constraint_package.is_none());
83    }
84
85    #[test]
86    fn test_constraint_configuration() {
87        let config = ConstraintConfiguration;
88        let json = serde_json::to_string(&config).unwrap();
89        let parsed: ConstraintConfiguration = serde_json::from_str(&json).unwrap();
90        assert_eq!(parsed, config);
91    }
92
93    #[test]
94    fn test_deep_planning_clone() {
95        let clone = DeepPlanningClone;
96        let json = serde_json::to_string(&clone).unwrap();
97        let parsed: DeepPlanningClone = serde_json::from_str(&json).unwrap();
98        assert_eq!(parsed, clone);
99    }
100}