From 17d8b70331528fc3c2be574bea4c9a1d458e5e6f Mon Sep 17 00:00:00 2001 From: gak Date: Thu, 21 Jul 2022 18:03:50 +1000 Subject: [PATCH] refactor: Move vechicle match into struct. --- examples/cli.rs | 64 ++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/examples/cli.rs b/examples/cli.rs index 17a27e3..0bcf3df 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -67,6 +67,37 @@ struct Vehicle { pub command: VehicleCommand, } +impl Vehicle { + async fn run(self, api: &Api) { + match self.command { + VehicleCommand::Data => { + dbg!(api.vehicle_data(&self.id).await.unwrap()); + } + VehicleCommand::ChargeState => { + dbg!(api.charge_state(&self.id).await.unwrap()); + } + VehicleCommand::SetChargeLimit { percent } => { + dbg!(api + .set_charge_limit(&self.id, &SetChargeLimit { percent }) + .await + .unwrap()); + } + VehicleCommand::SetChargingAmps { charging_amps } => { + dbg!(api + .set_charging_amps(&self.id, &SetChargingAmps { charging_amps }) + .await + .unwrap()); + } + VehicleCommand::ChargeStart => { + dbg!(api.charge_start(&self.id).await.unwrap()); + } + VehicleCommand::ChargeStop => { + dbg!(api.charge_stop(&self.id).await.unwrap()); + } + } + } +} + #[derive(Debug, Subcommand)] enum VehicleCommand { /// Get vehicle data. @@ -126,41 +157,14 @@ async fn main() { let vehicles = api.vehicles().await.unwrap(); dbg!(&vehicles); } - ApiCommand::Vehicle(v) => vehicles(&api, v).await, + ApiCommand::Vehicle(v) => { + v.run(&api).await; + } } } } } -async fn vehicles(api: &Api, vehicle: Vehicle) { - match vehicle.command { - VehicleCommand::Data => { - dbg!(api.vehicle_data(&vehicle.id).await.unwrap()); - } - VehicleCommand::ChargeState => { - dbg!(api.charge_state(&vehicle.id).await.unwrap()); - } - VehicleCommand::SetChargeLimit { percent } => { - dbg!(api - .set_charge_limit(&vehicle.id, &SetChargeLimit { percent }) - .await - .unwrap()); - } - VehicleCommand::SetChargingAmps { charging_amps } => { - dbg!(api - .set_charging_amps(&vehicle.id, &SetChargingAmps { charging_amps }) - .await - .unwrap()); - } - VehicleCommand::ChargeStart => { - dbg!(api.charge_start(&vehicle.id).await.unwrap()); - } - VehicleCommand::ChargeStop => { - dbg!(api.charge_stop(&vehicle.id).await.unwrap()); - } - } -} - fn updated_tokens(save: bool, access_token: AccessToken, refresh_token: RefreshToken) { println!("Access token: {}", access_token.0); println!("Refresh token: {}", refresh_token.0);