Allen George | de6f61a | 2021-03-11 08:25:26 -0500 | [diff] [blame^] | 1 | // Licensed to the Apache Software Foundation (ASF) under one |
| 2 | // or more contributor license agreements. See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership. The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | #![allow(dead_code)] |
| 19 | |
| 20 | pub mod transit; |
| 21 | pub mod vehicles; |
| 22 | pub mod maintenance; |
| 23 | |
| 24 | mod server { |
| 25 | use crate::maintenance::maintenance_facility::{ |
| 26 | BigBarnSyncHandler, MultimodalFacilitySyncHandler, |
| 27 | }; |
| 28 | use crate::transit::buses::{Bus, GarageSyncHandler}; |
| 29 | use crate::transit::buses::{Powertrain, Route as BusRoute}; |
| 30 | use crate::transit::light::streetcars::{ |
| 31 | BarnSyncHandler, Flexity, RollingStock, Route, RouteNumber, Streetcar, |
| 32 | }; |
| 33 | use crate::transit::services::city_services::TransitImprovements; |
| 34 | use crate::transit::trains::Locomotive; |
| 35 | use crate::transit::transporters::{FlatcarConsist, SingleVehicleTransporter}; |
| 36 | use crate::vehicles::Material; |
| 37 | use thrift::Result; |
| 38 | |
| 39 | // |
| 40 | // implement a whole bunch of handler methods just to make sure I can, and that everything compiles |
| 41 | // |
| 42 | |
| 43 | pub struct AllInOneHandler; |
| 44 | |
| 45 | impl BigBarnSyncHandler for AllInOneHandler { |
| 46 | fn handle_add_streetcar(&self, route: Route) -> Result<Streetcar> { |
| 47 | if let Some(route_number) = route.id { |
| 48 | match route_number { |
| 49 | RouteNumber::LAKESHORE => Ok(Streetcar { |
| 50 | id: Some(4417), |
| 51 | stock: Some(RollingStock::Flexity(Flexity { |
| 52 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 53 | locomotive: Some(Locomotive::ELECTRIC_PANTOGRAPH), |
| 54 | })), |
| 55 | route: Some(Route { |
| 56 | id: Some(RouteNumber::LAKESHORE), |
| 57 | improvements: None, |
| 58 | }), |
| 59 | }), |
| 60 | _ => Err(thrift::Error::from(format!( |
| 61 | "Cannot create streetcar for route number {}", |
| 62 | route_number.0 |
| 63 | ))), |
| 64 | } |
| 65 | } else { |
| 66 | Err(thrift::Error::from("Can't add a streetcar")) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | impl BarnSyncHandler for AllInOneHandler { |
| 72 | fn handle_upgrade_streetcar(&self, streetcar: Streetcar) -> Result<Streetcar> { |
| 73 | if let Some(rolling_stock) = streetcar.stock { |
| 74 | match rolling_stock { |
| 75 | RollingStock::Clrv(_) => Ok(Streetcar { |
| 76 | stock: Some(RollingStock::Flexity(Flexity { |
| 77 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 78 | locomotive: Some(Locomotive::ELECTRIC_PANTOGRAPH), |
| 79 | })), |
| 80 | ..streetcar |
| 81 | }), |
| 82 | RollingStock::Flexity(_) => { |
| 83 | Err(thrift::Error::from("Streetcar already upgraded")) |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | Err(thrift::Error::from("Can't upgrade streetcar")) |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | impl MultimodalFacilitySyncHandler for AllInOneHandler { |
| 93 | fn handle_build_transporter( |
| 94 | &self, |
| 95 | source: String, |
| 96 | destination: String, |
| 97 | consist: FlatcarConsist, |
| 98 | ) -> Result<SingleVehicleTransporter> { |
| 99 | Ok(SingleVehicleTransporter { |
| 100 | consist: Some(consist), |
| 101 | source: Some(source), |
| 102 | destination: Some(destination), |
| 103 | }) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | impl GarageSyncHandler for AllInOneHandler { |
| 108 | fn handle_upgrade_bus(&self, bus: Bus) -> Result<Bus> { |
| 109 | if let Some(p) = bus.powertrain { |
| 110 | match p { |
| 111 | Powertrain::COMPRESSED_NATURAL_GAS => Ok(Bus { |
| 112 | powertrain: Some(Powertrain::DIESEL), |
| 113 | ..bus |
| 114 | }), |
| 115 | _ => Err(thrift::Error::from("Cannot upgrade from this powertrain")), |
| 116 | } |
| 117 | } else { |
| 118 | Err(thrift::Error::from("Cannot upgrade bus")) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | fn handle_improvements_for_route( |
| 123 | &self, |
| 124 | route: BusRoute, |
| 125 | ) -> Result<Vec<TransitImprovements>> { |
| 126 | Ok(route |
| 127 | .improvements |
| 128 | .expect("Expecting a list of improvements")) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #[cfg(test)] |
| 134 | mod tests { |
| 135 | |
| 136 | // |
| 137 | // TODO: consider using the generated client/server and doing a round-trip |
| 138 | // |
| 139 | |
| 140 | use crate::server::AllInOneHandler; |
| 141 | use crate::transit::buses::{Bus, Powertrain, Route as BusRoute, DEFAULT4WHEELCAPACITY}; |
| 142 | use crate::transit::light::light_rail::Lrt; |
| 143 | use crate::transit::light::streetcars::{ |
| 144 | BarnSyncHandler, Flexity, RollingStock, Route, RouteNumber, Streetcar, CLRV, |
| 145 | }; |
| 146 | use crate::transit::services::city_services::TransitImprovements; |
| 147 | use crate::transit::trains::Locomotive; |
| 148 | use crate::transit::transporters::{FlatcarConsist, SingleVehicleTransporter}; |
| 149 | use crate::vehicles::{Material, VehicleIdentifier}; |
| 150 | |
| 151 | use crate::maintenance::maintenance_facility::{ |
| 152 | BigBarnSyncHandler, MultimodalFacilitySyncHandler, |
| 153 | }; |
| 154 | use crate::transit::buses::GarageSyncHandler; |
| 155 | |
| 156 | #[test] |
| 157 | fn handle_add_streetcar_compiles_and_returns_expected_value() { |
| 158 | let expected = Streetcar { |
| 159 | id: Some(4417), |
| 160 | stock: Some(RollingStock::Flexity(Flexity { |
| 161 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 162 | locomotive: Some(Locomotive::ELECTRIC_PANTOGRAPH), |
| 163 | })), |
| 164 | route: Some(Route { |
| 165 | id: Some(RouteNumber::LAKESHORE), |
| 166 | improvements: None, |
| 167 | }), |
| 168 | }; |
| 169 | |
| 170 | let handler = AllInOneHandler {}; |
| 171 | let actual = handler |
| 172 | .handle_add_streetcar(Route { |
| 173 | id: Some(RouteNumber::LAKESHORE), |
| 174 | improvements: None, |
| 175 | }) |
| 176 | .expect("Expected a result"); |
| 177 | |
| 178 | assert_eq!(expected, actual) |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn handle_upgrade_streetcar_compiles_and_returns_expected_value() { |
| 183 | let input = Streetcar { |
| 184 | stock: Some(RollingStock::Clrv(CLRV { |
| 185 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 186 | locomotive: Some(Locomotive::ELECTRIC_POLE), |
| 187 | })), |
| 188 | id: Some(4415), |
| 189 | route: Some(Route { |
| 190 | id: Some(RouteNumber::SPADINA), |
| 191 | improvements: Some(vec![TransitImprovements::DEDICATED_RIGHT_OF_WAY]), |
| 192 | }), |
| 193 | }; |
| 194 | |
| 195 | let expected = Streetcar { |
| 196 | stock: Some(RollingStock::Flexity(Flexity { |
| 197 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 198 | locomotive: Some(Locomotive::ELECTRIC_PANTOGRAPH), |
| 199 | })), |
| 200 | id: Some(4415), |
| 201 | route: Some(Route { |
| 202 | id: Some(RouteNumber::SPADINA), |
| 203 | improvements: Some(vec![TransitImprovements::DEDICATED_RIGHT_OF_WAY]), |
| 204 | }), |
| 205 | }; |
| 206 | |
| 207 | let handler = AllInOneHandler {}; |
| 208 | let actual = handler |
| 209 | .handle_upgrade_streetcar(input) |
| 210 | .expect("Expected an upgraded streetcar"); |
| 211 | |
| 212 | assert_eq!(expected, actual) |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn handle_build_transporter_compiles_and_returns_expected_value() { |
| 217 | let consist = FlatcarConsist::Lrt(Lrt { |
| 218 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 219 | locomotive: Some(Locomotive::ELECTRIC_PANTOGRAPH), |
| 220 | }); |
| 221 | let expected = SingleVehicleTransporter { |
| 222 | consist: Some(consist.clone()), |
| 223 | source: Some("905".to_owned()), |
| 224 | destination: Some("416".to_owned()), |
| 225 | }; |
| 226 | |
| 227 | let handler = AllInOneHandler {}; |
| 228 | let actual = handler |
| 229 | .handle_build_transporter("905".to_owned(), "416".to_owned(), consist) |
| 230 | .expect("Expected a transporter"); |
| 231 | |
| 232 | assert_eq!(expected, actual) |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn handle_upgrade_bus_compiles_and_returns_expected_value() { |
| 237 | let bus = Bus { |
| 238 | identifier: Some(VehicleIdentifier { |
| 239 | manufacturer: Some("Orion".to_owned()), |
| 240 | model: Some("Orion 07.501 NG HEV".to_owned()), |
| 241 | qualifiers: None, |
| 242 | }), |
| 243 | capacity: Some(DEFAULT4WHEELCAPACITY), |
| 244 | powertrain: Some(Powertrain::COMPRESSED_NATURAL_GAS), |
| 245 | materials: Some(vec![Material::STEEL, Material::ALUMINUM]), |
| 246 | }; |
| 247 | |
| 248 | let expected = Bus { |
| 249 | powertrain: Some(Powertrain::DIESEL), |
| 250 | ..(bus.clone()) |
| 251 | }; |
| 252 | |
| 253 | let handler = AllInOneHandler {}; |
| 254 | let actual = handler |
| 255 | .handle_upgrade_bus(bus) |
| 256 | .expect("Expected improved bus"); |
| 257 | |
| 258 | assert_eq!(expected, actual) |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn handle_improvements_for_route_compiles_and_returns_expected_value() { |
| 263 | let expected = vec![TransitImprovements::TRANSIT_SIGNAL_PRIORITY]; |
| 264 | let bus_route = BusRoute { |
| 265 | route_id: Some("320".to_owned()), |
| 266 | improvements: Some(expected.clone()), |
| 267 | }; |
| 268 | |
| 269 | let handler = AllInOneHandler {}; |
| 270 | let actual = handler |
| 271 | .handle_improvements_for_route(bus_route) |
| 272 | .expect("Expected list of transit improvements"); |
| 273 | |
| 274 | assert_eq!(expected, actual) |
| 275 | } |
| 276 | } |