-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathswitch.rs
101 lines (87 loc) · 2.38 KB
/
switch.rs
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
use std::collections::HashMap;
use derivative::Derivative;
use serde::Deserialize;
use serde::Serialize;
use super::OSRDIdentified;
use super::ObjectType;
use crate::infra_cache::Cache;
use crate::infra_cache::ObjectCache;
use editoast_common::Identifier;
use editoast_common::NonBlankString;
use editoast_schemas::infra::TrackEndpoint;
use editoast_schemas::primitives::OSRDTyped;
#[derive(Debug, Derivative, Clone, Deserialize, Serialize, PartialEq)]
#[serde(deny_unknown_fields)]
#[derivative(Default)]
pub struct Switch {
pub id: Identifier,
pub switch_type: Identifier,
pub group_change_delay: f64,
pub ports: HashMap<Identifier, TrackEndpoint>,
#[serde(default)]
pub extensions: SwitchExtensions,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SwitchExtensions {
sncf: Option<SwitchSncfExtension>,
}
#[derive(Debug, Default, Clone, Deserialize, Serialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct SwitchSncfExtension {
pub label: NonBlankString,
}
impl OSRDTyped for Switch {
fn get_type() -> ObjectType {
ObjectType::Switch
}
}
impl OSRDIdentified for Switch {
fn get_id(&self) -> &String {
&self.id
}
}
#[derive(Debug, Clone, Derivative)]
#[derivative(Hash, PartialEq)]
pub struct SwitchCache {
pub obj_id: String,
pub switch_type: String,
#[derivative(Hash = "ignore", PartialEq = "ignore")]
pub ports: HashMap<String, TrackEndpoint>,
}
impl SwitchCache {
pub fn new(obj_id: String, switch_type: String, ports: HashMap<String, TrackEndpoint>) -> Self {
Self {
obj_id,
switch_type,
ports,
}
}
}
impl From<Switch> for SwitchCache {
fn from(switch: Switch) -> Self {
Self::new(
switch.id.0,
switch.switch_type.0,
switch.ports.into_iter().map(|(k, v)| (k.0, v)).collect(),
)
}
}
impl OSRDTyped for SwitchCache {
fn get_type() -> ObjectType {
ObjectType::Switch
}
}
impl OSRDIdentified for SwitchCache {
fn get_id(&self) -> &String {
&self.obj_id
}
}
impl Cache for SwitchCache {
fn get_track_referenced_id(&self) -> Vec<&String> {
self.ports.iter().map(|port| &*port.1.track).collect()
}
fn get_object_cache(&self) -> ObjectCache {
ObjectCache::Switch(self.clone())
}
}