Skip to content

Commit

Permalink
simplify manifest handling
Browse files Browse the repository at this point in the history
  • Loading branch information
liamkinne committed Jul 26, 2024
1 parent fbd2f2e commit bdf9f61
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 59 deletions.
72 changes: 20 additions & 52 deletions src/gateway/manifest.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,44 +26,14 @@ use std::collections::HashMap;
/// ```
#[derive(Debug, Deserialize)]
pub struct Manifest {
schema: String,
latest: String,
stable: String,
binaries: HashMap<String, FirmwareBinary>,
}

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<String, FirmwareBinary> {
&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<String, FirmwareBinary>,
}

/// Metadata for a firmware release binary.
Expand All @@ -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,
}
2 changes: 1 addition & 1 deletion src/gateway/restart.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
13 changes: 7 additions & 6 deletions src/gateway/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
)))
}
}
};
Expand All @@ -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?;
}
Expand Down

0 comments on commit bdf9f61

Please sign in to comment.