Fixed stuff
All checks were successful
Build Project / test (push) Successful in 5m30s

This commit is contained in:
2025-01-15 23:10:54 +00:00
parent b9551628f7
commit e19a11d5f3
7 changed files with 127 additions and 50 deletions

View File

@@ -1,4 +1,15 @@
use std::{fs, path::PathBuf};
use log::info;
use strum::EnumString;
use thiserror::Error;
use serde;
use std::str::FromStr;
use crate::schemas::{BoardConfig, BoardType, OTAConfiguration};
pub fn parse_mac_address(mac: &str) -> Result<[u8; 6], MacAddressError> {
if mac.len() != 12 {
@@ -22,10 +33,62 @@ pub enum MacAddressError {
Length(usize),
}
#[derive(Error, Debug)]
pub enum GetFilesError {
#[error("IO Error while reading files")]
IO(#[from] std::io::Error),
#[error("Error getting filename")]
Filename,
#[error("Error getting extension (.bin)")]
Extension,
#[error("Strum parse Error")]
Conversion(#[from] strum::ParseError)
}
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 entries = fs::read_dir(root_path)?;
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
println!("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)?;
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) };
configs.push(cfg);
} else if path.is_dir() {
println!("Directory: {:?}", path);
}
}
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 loaded_configs = get_files(&PathBuf::from("./test/waterlevel"), "example.com").unwrap();
assert_eq!(loaded_configs[0], expected_1);
assert_eq!(loaded_configs[1], expected_2);
}
#[test]
fn test_valid_mac_address_plain() {
let mac_str = "001A2B3C4D5E";