elevator-core 20.13.0

Engine-agnostic elevator simulation library with pluggable dispatch strategies
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Line (physical path) component — shaft, tether, track, etc.

use serde::{Deserialize, Serialize};

use crate::ids::GroupId;

/// Physical orientation of a line.
///
/// This is metadata for external systems (rendering, spatial queries).
/// The simulation always operates along a 1D axis regardless of orientation.
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Orientation {
    /// Standard vertical elevator shaft.
    #[default]
    Vertical,
    /// Angled incline (e.g., funicular).
    Angled {
        /// Angle from horizontal in degrees (0 = horizontal, 90 = vertical).
        degrees: f64,
    },
    /// Horizontal people-mover or transit line.
    Horizontal,
}

impl std::fmt::Display for Orientation {
    /// Bounded precision keeps TUI/HUD output stable when `degrees` is the
    /// result of a radians→degrees conversion that doesn't round-trip cleanly.
    ///
    /// ```
    /// # use elevator_core::components::Orientation;
    /// assert_eq!(format!("{}", Orientation::Vertical), "vertical");
    /// assert_eq!(format!("{}", Orientation::Horizontal), "horizontal");
    /// assert_eq!(format!("{}", Orientation::Angled { degrees: 30.0 }), "30.0°");
    /// assert_eq!(
    ///     format!("{}", Orientation::Angled { degrees: 22.123_456_789 }),
    ///     "22.1°",
    /// );
    /// ```
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Vertical => f.write_str("vertical"),
            Self::Horizontal => f.write_str("horizontal"),
            Self::Angled { degrees } => write!(f, "{degrees:.1}°"),
        }
    }
}

/// 2D position on a floor plan (for spatial queries and rendering).
///
/// On a [`LineKind::Linear`] line this anchors one end of the axis. On a
/// [`LineKind::Loop`] line this anchors the geometric *center* of the
/// loop; hosts derive any rendering radius from
/// [`Line::circumference`] (e.g. `r = C / (2π)` for a circular layout).
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct SpatialPosition {
    /// X coordinate on the floor plan.
    pub x: f64,
    /// Y coordinate on the floor plan.
    pub y: f64,
}

/// Topology of a line — open-ended linear axis or closed loop.
///
/// `Linear` is the default for elevator shafts, tethers, and other paths
/// bounded by `[min, max]`. `Loop` (gated behind the `loop_lines`
/// feature) models a closed-loop transit line where positions wrap
/// modulo `circumference`. Helpers in [`super::cyclic`] operate on Loop
/// positions; consumer code dispatches on this enum to pick linear vs
/// cyclic semantics.
///
/// `#[non_exhaustive]` — future topologies (figure-eight, branching, etc.)
/// can be added without a major version bump.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum LineKind {
    /// Open-ended line with hard `[min, max]` position bounds. Cars
    /// reverse at endpoints; this matches every existing dispatch
    /// strategy (LOOK, sweep, scan, destination, etc.).
    Linear {
        /// Lowest reachable position.
        min: f64,
        /// Highest reachable position.
        max: f64,
    },
    /// Closed loop with cyclic position semantics. Positions wrap into
    /// `[0, circumference)`; cars travel one direction only and
    /// maintain a strict no-overtake ordering with at least
    /// `min_headway` between successive cars.
    #[cfg(feature = "loop_lines")]
    Loop {
        /// Total path length around the loop. Must be `> 0` —
        /// construction (`Simulation::add_line` and the explicit-topology
        /// builder) rejects non-positive or non-finite values.
        circumference: f64,
        /// Minimum permitted forward distance between successive cars.
        /// PR 3 will add construction-time validation
        /// (`max_cars * min_headway <= circumference`); until then, the
        /// dispatch and movement code added in subsequent PRs is
        /// responsible for behaving sanely on degenerate values.
        /// Callers should set this strictly positive.
        min_headway: f64,
    },
}

impl LineKind {
    /// Whether this line is a closed loop.
    #[must_use]
    pub const fn is_loop(&self) -> bool {
        match self {
            Self::Linear { .. } => false,
            #[cfg(feature = "loop_lines")]
            Self::Loop { .. } => true,
        }
    }

    /// Whether this line is a (linear) open-ended axis.
    ///
    /// Paired with [`Self::is_loop`] so consumers can dispatch positively
    /// on the expected variant rather than negatively on "not Loop", which
    /// would silently absorb any future topology added to the enum.
    #[must_use]
    pub const fn is_linear(&self) -> bool {
        match self {
            Self::Linear { .. } => true,
            #[cfg(feature = "loop_lines")]
            Self::Loop { .. } => false,
        }
    }

    /// Validate that this kind's intrinsic bounds are well-formed.
    ///
    /// Returns `Err((field, reason))` on a violation; both construction
    /// entry points ([`Simulation::add_line`](crate::sim::Simulation::add_line)
    /// and the explicit-topology builder) call this and lift the error
    /// into [`SimError::InvalidConfig`](crate::error::SimError::InvalidConfig).
    ///
    /// The intent is the *trivial* per-kind sanity checks — bounds finite
    /// and ordered, circumference positive. Cross-line invariants
    /// (`max_cars` × headway, group homogeneity, initial spacing) are PR 3.
    ///
    /// # Errors
    ///
    /// `Linear` rejects non-finite or `min > max` bounds. `Loop` rejects
    /// non-finite or non-positive `circumference`.
    pub fn validate(&self) -> Result<(), (&'static str, String)> {
        match self {
            Self::Linear { min, max } => {
                if !min.is_finite() || !max.is_finite() {
                    return Err((
                        "line.range",
                        format!("min/max must be finite (got min={min}, max={max})"),
                    ));
                }
                if min > max {
                    return Err(("line.range", format!("min ({min}) must be <= max ({max})")));
                }
            }
            #[cfg(feature = "loop_lines")]
            Self::Loop {
                circumference,
                min_headway,
            } => {
                if !circumference.is_finite() || *circumference <= 0.0 {
                    return Err((
                        "line.kind",
                        format!("loop circumference must be finite and > 0 (got {circumference})"),
                    ));
                }
                // Negative `min_headway` would make `headway_clamp_target`
                // compute `safe_advance = gap - min_headway = gap + |min_headway|`,
                // i.e. the trailer would be allowed to advance *past* the
                // leader. Reject up front so the cross-field guard
                // (`max_cars * min_headway <= circumference`) can't be
                // bypassed by sign-flipping `required` to negative.
                if !min_headway.is_finite() || *min_headway <= 0.0 {
                    return Err((
                        "line.kind",
                        format!("loop min_headway must be finite and > 0 (got {min_headway})"),
                    ));
                }
            }
        }
        Ok(())
    }
}

/// Component for a line entity — the physical path an elevator car travels.
///
/// In a building this is a hoistway/shaft. For a space elevator it is a
/// tether or cable. For a metro or people-mover (with the `loop_lines`
/// feature) it is a closed loop. The term "line" is domain-neutral.
///
/// A line belongs to exactly one [`GroupId`] at a time but can be
/// reassigned at runtime (swing-car pattern). Multiple cars may share
/// a line (multi-car shafts); collision avoidance is left to game hooks
/// for `Linear` lines and enforced by headway clamping for `Loop` lines.
///
/// Intrinsic properties only — relationship data (which elevators, which
/// stops) lives in [`LineInfo`](crate::dispatch::LineInfo) on the
/// [`ElevatorGroup`](crate::dispatch::ElevatorGroup).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(from = "LineWire", into = "LineWire")]
pub struct Line {
    /// Human-readable name.
    pub(crate) name: String,
    /// Dispatch group this line currently belongs to.
    pub(crate) group: GroupId,
    /// Physical orientation (metadata for rendering).
    pub(crate) orientation: Orientation,
    /// Optional floor-plan position (for spatial queries / rendering).
    pub(crate) position: Option<SpatialPosition>,
    /// Topology kind — open-ended linear axis or closed loop.
    pub(crate) kind: LineKind,
    /// Maximum number of cars allowed on this line (None = unlimited).
    pub(crate) max_cars: Option<usize>,
}

/// On-the-wire representation of [`Line`]. Bridges the legacy flat
/// `min_position` / `max_position` fields with the new `kind` field
/// during the transitional release: snapshots written by builds that
/// haven't migrated yet still deserialize correctly, and snapshots
/// from this build can still be inspected by older tooling.
///
/// On serialize we emit `kind` *and* derived flat fields. On deserialize,
/// `kind` is preferred; flat fields are the fallback.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct LineWire {
    /// Human-readable name (always present in both legacy and current shapes).
    name: String,
    /// Dispatch group ownership.
    group: GroupId,
    /// Physical orientation (defaults to Vertical if absent).
    #[serde(default)]
    orientation: Orientation,
    /// 2D anchor on the floor plan (defaults to None).
    #[serde(default)]
    position: Option<SpatialPosition>,
    /// New topology field — preferred when present.
    #[serde(default)]
    kind: Option<LineKind>,
    /// Legacy flat field — fallback when `kind` is absent.
    #[serde(default)]
    min_position: Option<f64>,
    /// Legacy flat field — fallback when `kind` is absent.
    #[serde(default)]
    max_position: Option<f64>,
    /// Maximum cars on this line.
    #[serde(default)]
    max_cars: Option<usize>,
}

impl From<LineWire> for Line {
    fn from(w: LineWire) -> Self {
        let kind = w.kind.unwrap_or_else(|| LineKind::Linear {
            min: w.min_position.unwrap_or(0.0),
            max: w.max_position.unwrap_or(0.0),
        });
        Self {
            name: w.name,
            group: w.group,
            orientation: w.orientation,
            position: w.position,
            kind,
            max_cars: w.max_cars,
        }
    }
}

impl From<Line> for LineWire {
    fn from(l: Line) -> Self {
        let (min_position, max_position) = match &l.kind {
            LineKind::Linear { min, max } => (Some(*min), Some(*max)),
            #[cfg(feature = "loop_lines")]
            LineKind::Loop { circumference, .. } => (Some(0.0), Some(*circumference)),
        };
        Self {
            name: l.name,
            group: l.group,
            orientation: l.orientation,
            position: l.position,
            kind: Some(l.kind),
            min_position,
            max_position,
            max_cars: l.max_cars,
        }
    }
}

impl Line {
    /// Human-readable name.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Dispatch group this line currently belongs to.
    #[must_use]
    pub const fn group(&self) -> GroupId {
        self.group
    }

    /// Physical orientation.
    #[must_use]
    pub const fn orientation(&self) -> Orientation {
        self.orientation
    }

    /// Optional floor-plan position. For [`LineKind::Loop`] this is the
    /// geometric *center* of the loop; hosts derive a rendering radius
    /// from [`Self::circumference`].
    #[must_use]
    pub const fn position(&self) -> Option<&SpatialPosition> {
        self.position.as_ref()
    }

    /// Topology kind — linear axis or closed loop.
    #[must_use]
    pub const fn kind(&self) -> &LineKind {
        &self.kind
    }

    /// Whether this is a closed-loop line.
    #[must_use]
    pub const fn is_loop(&self) -> bool {
        self.kind.is_loop()
    }

    /// Lowest reachable position on a [`LineKind::Linear`] line. Returns
    /// `None` for [`LineKind::Loop`] — loops have no endpoints.
    ///
    /// Replaces the former `min_position()` accessor. Callers that
    /// blindly dereferenced the old `f64` should now decide whether
    /// they want Linear-only behavior (`linear_min().expect("linear")`)
    /// or to handle Loop explicitly.
    #[must_use]
    pub const fn linear_min(&self) -> Option<f64> {
        match self.kind {
            LineKind::Linear { min, .. } => Some(min),
            #[cfg(feature = "loop_lines")]
            LineKind::Loop { .. } => None,
        }
    }

    /// Highest reachable position on a [`LineKind::Linear`] line. Returns
    /// `None` for [`LineKind::Loop`].
    #[must_use]
    pub const fn linear_max(&self) -> Option<f64> {
        match self.kind {
            LineKind::Linear { max, .. } => Some(max),
            #[cfg(feature = "loop_lines")]
            LineKind::Loop { .. } => None,
        }
    }

    /// Total path length of a [`LineKind::Loop`] line. Returns `None`
    /// for [`LineKind::Linear`].
    #[must_use]
    pub const fn circumference(&self) -> Option<f64> {
        match self.kind {
            LineKind::Linear { .. } => None,
            #[cfg(feature = "loop_lines")]
            LineKind::Loop { circumference, .. } => Some(circumference),
        }
    }

    /// Minimum forward distance between successive cars on a
    /// [`LineKind::Loop`] line. Returns `None` for [`LineKind::Linear`].
    #[must_use]
    pub const fn min_headway(&self) -> Option<f64> {
        match self.kind {
            LineKind::Linear { .. } => None,
            #[cfg(feature = "loop_lines")]
            LineKind::Loop { min_headway, .. } => Some(min_headway),
        }
    }

    /// Maximum number of cars allowed on this line.
    #[must_use]
    pub const fn max_cars(&self) -> Option<usize> {
        self.max_cars
    }
}

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

    #[test]
    fn linear_accessors_return_some() {
        let line = Line::from(LineWire {
            name: "L1".into(),
            group: GroupId(0),
            orientation: Orientation::Vertical,
            position: None,
            kind: Some(LineKind::Linear {
                min: 0.0,
                max: 100.0,
            }),
            min_position: None,
            max_position: None,
            max_cars: None,
        });
        assert_eq!(line.linear_min(), Some(0.0));
        assert_eq!(line.linear_max(), Some(100.0));
        assert_eq!(line.circumference(), None);
        assert_eq!(line.min_headway(), None);
        assert!(!line.is_loop());
    }

    #[test]
    fn legacy_flat_fields_construct_linear_kind() {
        let line = Line::from(LineWire {
            name: "L1".into(),
            group: GroupId(0),
            orientation: Orientation::Vertical,
            position: None,
            kind: None,
            min_position: Some(0.0),
            max_position: Some(50.0),
            max_cars: None,
        });
        assert_eq!(
            line.kind(),
            &LineKind::Linear {
                min: 0.0,
                max: 50.0
            }
        );
    }

    #[test]
    #[allow(clippy::unwrap_used, reason = "test helper")]
    fn round_trip_writes_both_kind_and_flat_fields() {
        let line = Line {
            name: "L1".into(),
            group: GroupId(0),
            orientation: Orientation::Vertical,
            position: None,
            kind: LineKind::Linear {
                min: 0.0,
                max: 75.0,
            },
            max_cars: None,
        };
        let serialized = serde_json::to_value(&line).unwrap();
        // Both shapes must be present so an older deserializer can still read it.
        assert!(serialized.get("kind").is_some());
        assert_eq!(
            serialized
                .get("min_position")
                .and_then(serde_json::Value::as_f64),
            Some(0.0)
        );
        assert_eq!(
            serialized
                .get("max_position")
                .and_then(serde_json::Value::as_f64),
            Some(75.0)
        );

        let deserialized: Line = serde_json::from_value(serialized).unwrap();
        assert_eq!(deserialized.kind(), line.kind());
    }

    #[test]
    fn validate_rejects_non_finite_linear() {
        assert!(
            LineKind::Linear {
                min: f64::NAN,
                max: 10.0
            }
            .validate()
            .is_err()
        );
        assert!(
            LineKind::Linear {
                min: 5.0,
                max: f64::INFINITY
            }
            .validate()
            .is_err()
        );
    }

    #[test]
    fn validate_rejects_inverted_linear_bounds() {
        assert!(
            LineKind::Linear {
                min: 10.0,
                max: 5.0
            }
            .validate()
            .is_err()
        );
    }

    #[test]
    fn validate_accepts_well_formed_linear() {
        assert!(
            LineKind::Linear {
                min: 0.0,
                max: 100.0
            }
            .validate()
            .is_ok()
        );
    }

    #[cfg(feature = "loop_lines")]
    #[test]
    fn validate_rejects_non_positive_circumference() {
        assert!(
            LineKind::Loop {
                circumference: 0.0,
                min_headway: 5.0
            }
            .validate()
            .is_err()
        );
        assert!(
            LineKind::Loop {
                circumference: -1.0,
                min_headway: 5.0
            }
            .validate()
            .is_err()
        );
        assert!(
            LineKind::Loop {
                circumference: f64::NAN,
                min_headway: 5.0
            }
            .validate()
            .is_err()
        );
    }

    #[cfg(feature = "loop_lines")]
    #[test]
    fn validate_accepts_positive_circumference() {
        assert!(
            LineKind::Loop {
                circumference: 100.0,
                min_headway: 5.0
            }
            .validate()
            .is_ok()
        );
    }

    #[cfg(feature = "loop_lines")]
    #[test]
    fn validate_rejects_non_positive_min_headway() {
        // Negative `min_headway` would let `headway_clamp_target` compute
        // a `safe_advance` larger than the actual gap, allowing overtaking.
        for bad in [0.0_f64, -1.0, f64::NAN] {
            let result = LineKind::Loop {
                circumference: 100.0,
                min_headway: bad,
            }
            .validate();
            assert!(
                result.is_err(),
                "min_headway={bad} should have been rejected, got {result:?}",
            );
        }
    }

    #[cfg(feature = "loop_lines")]
    #[test]
    fn loop_accessors_return_some() {
        let line = Line::from(LineWire {
            name: "L1".into(),
            group: GroupId(0),
            orientation: Orientation::Horizontal,
            position: None,
            kind: Some(LineKind::Loop {
                circumference: 200.0,
                min_headway: 10.0,
            }),
            min_position: None,
            max_position: None,
            max_cars: None,
        });
        assert_eq!(line.linear_min(), None);
        assert_eq!(line.linear_max(), None);
        assert_eq!(line.circumference(), Some(200.0));
        assert_eq!(line.min_headway(), Some(10.0));
        assert!(line.is_loop());
    }
}