From bdf9f6123cdb4fe1672f3f760d640e1951f2bccb Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Fri, 26 Jul 2024 23:48:00 +1000 Subject: [PATCH] simplify manifest handling --- src/gateway/manifest.rs | 72 ++++++++++++----------------------------- src/gateway/restart.rs | 2 +- src/gateway/update.rs | 13 ++++---- 3 files changed, 28 insertions(+), 59 deletions(-) diff --git a/src/gateway/manifest.rs b/src/gateway/manifest.rs index 15daff3..204e46e 100644 --- a/src/gateway/manifest.rs +++ b/src/gateway/manifest.rs @@ -1,6 +1,14 @@ use serde::Deserialize; use std::collections::HashMap; +/// Manifest schema. +/// +/// Only deserializes the `schema` field. +#[derive(Debug, Deserialize)] +pub struct ManifestSchema { + pub schema: String, +} + /// Manifest file format. /// /// # Example @@ -18,44 +26,14 @@ use std::collections::HashMap; /// ``` #[derive(Debug, Deserialize)] pub struct Manifest { - schema: String, - latest: String, - stable: String, - binaries: HashMap, -} - -impl Manifest { - /// Return the metadata for the latest firmware binary. - /// - /// Note this will return `None` if the version specified as latest is not - /// in the map of binaries. - pub fn latest(&self) -> Option<(&String, &FirmwareBinary)> { - self.binaries.get_key_value(&self.latest) - } - - /// Return the metadata for the stable firmware binary. - /// - /// Note this will return `None` if the version specified as stable is not - /// in the map of binaries. - pub fn stable(&self) -> Option<(&String, &FirmwareBinary)> { - self.binaries.get_key_value(&self.stable) - } - - /// The metadata for a specific firmware binary version. - /// - /// Note this will return `None` if the version specified is not in the map - /// of binaries. - pub fn version(&self, ver: &str) -> Option<(&String, &FirmwareBinary)> { - self.binaries.get_key_value(ver) - } - - /// Returns the map of firmware binaries. - /// - /// Key: version identifier. - /// Value: firmware binary metadata. - pub fn binaries(&self) -> &HashMap { - &self.binaries - } + /// Schema version. + pub schema: String, + /// Latest firmware version. + pub latest: String, + /// Stable firmware version. + pub stable: String, + /// List of available binaries. + pub binaries: HashMap, } /// Metadata for a firmware release binary. @@ -65,18 +43,8 @@ impl Manifest { /// breaking changes. #[derive(Debug, Deserialize)] pub struct FirmwareBinary { - file: String, - min: String, -} - -impl FirmwareBinary { - /// Firmware binary file URL. - pub fn file(&self) -> &str { - &self.file - } - - /// Minimum version required to upgrade to this binary. - pub fn minimum_supported_version(&self) -> &str { - &self.min - } + /// File link. + pub file: String, + /// Minimum supported version. + pub min: String, } diff --git a/src/gateway/restart.rs b/src/gateway/restart.rs index ce7685f..a4f1a49 100644 --- a/src/gateway/restart.rs +++ b/src/gateway/restart.rs @@ -1,7 +1,7 @@ use crate::write_with_header; use colored::Colorize; use std::{net::IpAddr, time::Duration}; -use tokio_modbus::client::{Context as ModbusContext, Writer}; +use tokio_modbus::client::Writer; use super::connect_modbus; diff --git a/src/gateway/update.rs b/src/gateway/update.rs index b3c16eb..a5f85f1 100644 --- a/src/gateway/update.rs +++ b/src/gateway/update.rs @@ -107,7 +107,7 @@ pub async fn command( .await?; let firmware = if let Some(version) = options.version { - match manifest.version(&version) { + match manifest.binaries.get_key_value(&version) { Some(fw) => fw, None => { return Err(Error::msg(format!( @@ -117,12 +117,13 @@ pub async fn command( } } } else { - match manifest.stable() { + match manifest.binaries.get_key_value(&manifest.stable) { Some(fw) => fw, None => { - return Err(Error::msg( - "Stable firmware version was not found.", - )) + return Err(Error::msg(format!( + "Stable firmware version {} was not found.", + manifest.stable, + ))) } } }; @@ -133,7 +134,7 @@ pub async fn command( &format!("{}", firmware.0), ); - let binary = reqwest::get(firmware.1.file()).await?.bytes().await?; + let binary = reqwest::get(&firmware.1.file).await?.bytes().await?; upgrade_firmware(output, ip, &binary).await?; }