Format
All checks were successful
Build Project / test (push) Successful in 5m38s

This commit is contained in:
2025-01-19 16:14:22 +00:00
parent b336b6b926
commit 92159bc276
3 changed files with 77 additions and 36 deletions

View File

@@ -1,14 +1,11 @@
use std::{fs, path::PathBuf};
use log::info;
use thiserror::Error;
use std::str::FromStr;
use thiserror::Error;
use crate::schemas::{BoardConfig, BoardType, OTAConfiguration};
pub fn parse_mac_address(mac: &str) -> Result<[u8; 6], MacAddressError> {
if mac.len() != 12 {
return Err(MacAddressError::Length(mac.len()));
@@ -40,13 +37,19 @@ pub enum GetFilesError {
#[error("Error getting extension (.bin)")]
Extension,
#[error("Strum parse Error")]
Conversion(#[from] strum::ParseError)
Conversion(#[from] strum::ParseError),
}
pub fn get_files(root_path: &PathBuf, hostname: &str) -> Result<Vec<OTAConfiguration>, GetFilesError>{
pub fn get_files(
root_path: &PathBuf,
hostname: &str,
) -> Result<Vec<OTAConfiguration>, GetFilesError> {
info!("Getting all files from path {root_path:?}");
let mut configs = Vec::new();
let product_name = root_path.file_name().ok_or(GetFilesError::Filename)?.to_string_lossy();
let product_name = root_path
.file_name()
.ok_or(GetFilesError::Filename)?
.to_string_lossy();
let entries = fs::read_dir(root_path)?;
for entry in entries.flatten() {
@@ -55,13 +58,28 @@ pub fn get_files(root_path: &PathBuf, hostname: &str) -> Result<Vec<OTAConfigura
if path.is_file() {
info!("processing file: {:?}", path);
// Splits the filename at the underscores. This is safe to do as names get sanitized on upload and are only uploaded by the pipeline
let split_name: Vec<_> = path.file_name().ok_or(GetFilesError::Filename)?.to_str().ok_or(GetFilesError::Filename)?.split("_").collect();
let version = split_name[2].strip_suffix(".bin").ok_or(GetFilesError::Extension)?.replace("-", ".");
let split_name: Vec<_> = path
.file_name()
.ok_or(GetFilesError::Filename)?
.to_str()
.ok_or(GetFilesError::Filename)?
.split("_")
.collect();
let version = split_name[2]
.strip_suffix(".bin")
.ok_or(GetFilesError::Extension)?
.replace("-", ".");
let board_config = BoardConfig::from_str(split_name[1])?;
let board_type = BoardType::from_str(&product_name).unwrap();
let version_replaced = version.replace(".", "_");
let fw_url = format!("{hostname}/firmware/{board_type}/{board_config}/{version_replaced}.bin");
let cfg = OTAConfiguration{version: version.to_string(), url: fw_url, board: Some(board_type), config: Some(board_config) };
let fw_url =
format!("{hostname}/firmware/{board_type}/{board_config}/{version_replaced}.bin");
let cfg = OTAConfiguration {
version: version.to_string(),
url: fw_url,
board: Some(board_type),
config: Some(board_config),
};
configs.push(cfg);
} else if path.is_dir() {
println!("Directory: {:?}", path);
@@ -69,21 +87,28 @@ pub fn get_files(root_path: &PathBuf, hostname: &str) -> Result<Vec<OTAConfigura
}
Ok(configs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_loading() {
let expected_1 = OTAConfiguration{ version: "1.2.3".to_string(), url: "example.com/firmware/waterlevel/INA233/1_2_3.bin".to_string(), board: Some(BoardType::Waterlevel), config: Some(BoardConfig::INA233) };
let expected_2 = OTAConfiguration{ version: "4.5.6".to_string(), url: "example.com/firmware/waterlevel/INA226/4_5_6.bin".to_string(), board: Some(BoardType::Waterlevel), config: Some(BoardConfig::INA226) };
let expected_1 = OTAConfiguration {
version: "1.2.3".to_string(),
url: "example.com/firmware/waterlevel/INA233/1_2_3.bin".to_string(),
board: Some(BoardType::Waterlevel),
config: Some(BoardConfig::INA233),
};
let expected_2 = OTAConfiguration {
version: "4.5.6".to_string(),
url: "example.com/firmware/waterlevel/INA226/4_5_6.bin".to_string(),
board: Some(BoardType::Waterlevel),
config: Some(BoardConfig::INA226),
};
let loaded_configs = get_files(&PathBuf::from("./test/waterlevel"), "example.com").unwrap();
assert_eq!(loaded_configs[1], expected_1);
assert_eq!(loaded_configs[0], expected_2);
}