plansolve 0.26.0

Official Rust client library for the PlanSolve optimization API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use serde::{Deserialize, Serialize};

/// Request model for starting a shift optimization.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub contracts: Vec<Contract>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shifts: Vec<ShiftAssignment>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub employees: Vec<ShiftEmployee>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weights: Option<Weights>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fairness: Option<Fairness>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub day_off_requests: Vec<DayOffRequest>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shift_off_requests: Vec<ShiftOffRequest>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hook: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub constraint_weight_overrides: Option<ConstraintWeightOverrides>,
}

/// A time window for shift scheduling.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Shift {
    pub id: String,
    pub min_start_time: String,
    pub max_end_time: String,
}

/// A shift assignment to be scheduled.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftAssignment {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skills: Vec<String>,
    #[serde(default)]
    pub cost: f64,
    #[serde(default)]
    pub value: i64,
    #[serde(default)]
    pub priority: i64,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub desired_skills: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    #[serde(default)]
    pub pinned_by_user: bool,
}

/// An employee in the shift optimization.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftEmployee {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub contract: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub skills: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub last_rest_date: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub availability: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub preference: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub period_rules: Vec<PeriodRule>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unavailable_dates: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tags: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum_minutes_per_week: Option<i64>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shifts: Vec<ShiftAssignment>,
}

/// A shift employee contract.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Contract {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min: Option<String>,
    #[serde(default)]
    pub max_consecutive_work_days: i64,
    #[serde(default)]
    pub max_shifts_day: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_rest_between_shifts_same_day: Option<String>,
    #[serde(default)]
    pub max_working_days: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub latest_shift_start: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub earliest_shift_start: Option<String>,
    #[serde(default)]
    pub minimum_consecutive_days_off: i64,
    #[serde(default)]
    pub minimum_hours_off_between_shifts: i64,
}

/// Constraint weights for the shift optimizer.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Weights {
    pub required_skills: i64,
    pub shift_capacity: i64,
    pub minimum_staffing: i64,
    pub no_double_booking: i64,
    pub rest_between_shifts: i64,
    pub employee_availability: i64,
    pub shift_preferences: i64,
    pub cost_minimization: i64,
    pub workload_balance: i64,
    pub fairness: i64,
    pub max_consecutive_work_days: i64,
    pub max_shifts_per_day: i64,
    pub max_working_days_per_week: i64,
    pub contract_rest_between_shifts: i64,
    pub earliest_shift_start: i64,
    pub latest_shift_start: i64,
    pub minimum_consecutive_days_off: i64,
    pub period_rule_violation: i64,
    pub desired_skills: i64,
    pub desired_day_off: i64,
    pub shift_off_request: i64,
    pub balance_time_worked: i64,
    pub employee_affinity: i64,
    pub avoid_shift_close_to_day_off: i64,
}

/// Solver options for the shift optimizer.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Options {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub partial_planning: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_iterations: Option<i64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub time_limit: Option<i64>,
}

/// Fairness configuration for the shift optimizer.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Fairness {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fairness_buckets: Vec<FairnessBucket>,
}

/// A fairness bucket grouping employees and shifts.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FairnessBucket {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub employees: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shifts: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub period: Option<String>,
}

/// An employee's request for a day off.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DayOffRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub employee_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub date: Option<String>,
    #[serde(default)]
    pub weight: i64,
}

/// An employee's request to be off a specific shift.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftOffRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub employee_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shift_name: Option<String>,
    #[serde(default)]
    pub weight: i64,
}

/// A period-based working constraint.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PeriodRule {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub period: Option<PlanningPeriod>,
    #[serde(default)]
    pub max_working_days: i64,
    #[serde(default)]
    pub min_working_days: i64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_working_duration: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_working_duration: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_rest_duration_between_shifts_same_day: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_rest_duration: Option<String>,
}

/// A time period for planning rules.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PlanningPeriod {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub to: Option<String>,
}

/// Overrides for constraint weights.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConstraintWeightOverrides {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub known_constraint_names: Vec<String>,
}

/// Response from starting a shift optimization.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftStartResponse {
    pub job_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub solver_job_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Response from getting shift optimization results.
///
/// The server returns a full echo of the request (all planning inputs) plus the
/// result fields `feasible`, `scoreString`, `score`, `unassignedShifts`, and
/// `assignedShifts`. The wire body carries **no** `jobId`; `job_id` here is a
/// client-side convenience stamped by [`ShiftClient::get_result`] and is never
/// read from or written to the wire.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ShiftResultResponse {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub job_id: Option<String>,
    // Echoed request fields.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub contracts: Vec<Contract>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shifts: Vec<ShiftAssignment>,
    #[serde(default)]
    pub employees: Vec<ShiftEmployee>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub day_off_requests: Vec<DayOffRequest>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shift_off_requests: Vec<ShiftOffRequest>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub options: Option<Options>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub weights: Option<Weights>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fairness: Option<Fairness>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub hook: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub constraint_weight_overrides: Option<ConstraintWeightOverrides>,
    // Result fields.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub feasible: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub score_string: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub score: Option<serde_json::Value>,
    #[serde(default)]
    pub unassigned_shifts: Vec<ShiftAssignment>,
    #[serde(default)]
    pub assigned_shifts: Vec<ShiftAssignment>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serializes_and_omits_empty_collections() {
        let json = serde_json::to_string(&ShiftRequest::default()).unwrap();
        // Empty optional vectors are omitted, keeping payloads lean.
        assert!(!json.contains("\"shifts\""));
        assert!(!json.contains("\"employees\""));
    }

    #[test]
    fn serializes_populated_assignment() {
        let request = ShiftRequest {
            shifts: vec![ShiftAssignment {
                name: Some("Night".into()),
                cost: 12.5,
                priority: 3,
                pinned_by_user: true,
                ..Default::default()
            }],
            ..Default::default()
        };
        let json = serde_json::to_string(&request).unwrap();
        assert!(json.contains("\"shifts\""));
        assert!(json.contains("\"pinnedByUser\":true"));
        assert!(json.contains("\"cost\":12.5"));
    }

    #[test]
    fn request_omits_result_only_fields() {
        // score / assignedShifts / unassignedShifts belong to the response, never the
        // request — a fully-defaulted request must not emit them.
        let json = serde_json::to_string(&ShiftRequest::default()).unwrap();
        assert!(!json.contains("\"score\""));
        assert!(!json.contains("\"assignedShifts\""));
        assert!(!json.contains("\"unassignedShifts\""));
    }

    #[test]
    fn deserializes_result_response() {
        // The response is a full echo of the request plus the result fields.
        let json = r#"{
            "id": "roster-1",
            "name": "January roster",
            "description": "night ward",
            "contracts": [{"name": "full-time", "max": "PT40H"}],
            "shifts": [
                {"name": "Ward A", "from": "2024-01-15T22:00:00", "to": "2024-01-16T06:00:00"}
            ],
            "dayOffRequests": [{"employeeName": "Alice", "date": "2024-01-20", "weight": 5}],
            "shiftOffRequests": [{"employeeName": "Alice", "shiftName": "Ward B", "weight": 3}],
            "options": {"partialPlanning": true},
            "hook": "https://example.test/hook",
            "feasible": true,
            "scoreString": "0hard/0medium/-45soft",
            "score": {"hard": 0, "medium": 0, "soft": -45},
            "assignedShifts": [
                {"name": "Ward A", "from": "2024-01-15T22:00:00", "to": "2024-01-16T06:00:00",
                 "skills": ["nurse"], "cost": 12.5, "value": 2, "priority": 3}
            ],
            "unassignedShifts": [
                {"name": "Ward B", "from": "2024-01-16T22:00:00", "to": "2024-01-17T06:00:00",
                 "skills": ["nurse", "first_aid"]}
            ],
            "employees": [
                {"name": "Alice", "skills": ["nurse"], "shifts": [
                    {"name": "Ward A", "from": "2024-01-15T22:00:00", "to": "2024-01-16T06:00:00"}
                ]}
            ]
        }"#;

        let result: ShiftResultResponse = serde_json::from_str(json).unwrap();
        // Echoed request fields.
        assert_eq!(result.id.as_deref(), Some("roster-1"));
        assert_eq!(result.name.as_deref(), Some("January roster"));
        assert_eq!(result.contracts.len(), 1);
        assert_eq!(result.shifts.len(), 1);
        assert_eq!(result.day_off_requests.len(), 1);
        assert_eq!(result.shift_off_requests.len(), 1);
        assert_eq!(
            result.options.as_ref().unwrap().partial_planning,
            Some(true)
        );
        assert_eq!(result.hook.as_deref(), Some("https://example.test/hook"));
        // Result fields.
        assert_eq!(result.feasible, Some(true));
        assert_eq!(
            result.score_string.as_deref(),
            Some("0hard/0medium/-45soft")
        );
        assert_eq!(result.assigned_shifts.len(), 1);
        assert_eq!(result.assigned_shifts[0].name.as_deref(), Some("Ward A"));
        assert_eq!(
            result.assigned_shifts[0].from.as_deref(),
            Some("2024-01-15T22:00:00")
        );
        assert_eq!(result.assigned_shifts[0].cost, 12.5);
        assert_eq!(result.unassigned_shifts.len(), 1);
        assert_eq!(result.employees.len(), 1);
        assert_eq!(result.employees[0].shifts.len(), 1);
    }
}