sim_lib_auto_core/
session.rs1use sim_kernel::{CapabilityName, CapabilitySet, diminish};
4
5use crate::{BrandCaps, VehicleId};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
9pub enum TransportPlacement {
10 Modeled,
12 Cassette {
14 id: String,
16 },
17 LocalBridge {
19 name: String,
21 },
22}
23
24impl TransportPlacement {
25 pub fn modeled() -> Self {
27 Self::Modeled
28 }
29
30 pub fn cassette(id: impl Into<String>) -> Self {
32 Self::Cassette { id: id.into() }
33 }
34
35 pub fn local_bridge(name: impl Into<String>) -> Self {
37 Self::LocalBridge { name: name.into() }
38 }
39
40 pub fn kind(&self) -> &'static str {
42 match self {
43 Self::Modeled => "modeled",
44 Self::Cassette { .. } => "cassette",
45 Self::LocalBridge { .. } => "local-bridge",
46 }
47 }
48}
49
50#[derive(Clone, Debug, PartialEq, Eq)]
52pub struct AutoSession {
53 pub vehicle: VehicleId,
55 pub brand: BrandCaps,
57 pub transport: TransportPlacement,
59 pub grants: Vec<CapabilityName>,
61}
62
63impl AutoSession {
64 pub fn new(
66 vehicle: VehicleId,
67 brand: BrandCaps,
68 transport: TransportPlacement,
69 grants: Vec<CapabilityName>,
70 ) -> Self {
71 Self {
72 vehicle,
73 brand,
74 transport,
75 grants,
76 }
77 }
78
79 pub fn modeled(vehicle: VehicleId, brand: BrandCaps, grants: Vec<CapabilityName>) -> Self {
81 Self::new(vehicle, brand, TransportPlacement::Modeled, grants)
82 }
83
84 pub fn with_transport(mut self, transport: TransportPlacement) -> Self {
86 self.transport = transport;
87 self
88 }
89
90 pub fn has_grant(&self, capability: &CapabilityName) -> bool {
92 self.grants.iter().any(|grant| grant == capability)
93 }
94
95 pub fn diminished_grants(&self, allowed: &[CapabilityName]) -> CapabilitySet {
97 let current = capability_set(self.grants.iter().cloned());
98 let allowed = capability_set(allowed.iter().cloned());
99 diminish(¤t, &allowed)
100 }
101}
102
103fn capability_set(capabilities: impl IntoIterator<Item = CapabilityName>) -> CapabilitySet {
104 capabilities
105 .into_iter()
106 .fold(CapabilitySet::new(), CapabilitySet::grant)
107}