Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
mod.rs 3.11 KiB
Newer Older
//! Configuration for bb-imager to use.

pub mod compact;

use std::collections::{HashMap, HashSet};

use semver::Version;
use serde::{Deserialize, Serialize};
use crate::{
    flasher::{bcf, msp430, sd},
    Destination,
};
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
pub struct Config {
Ayush Singh's avatar
Ayush Singh committed
    pub imager: Imager,
    pub os_list: Vec<OsList>,
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
Ayush Singh's avatar
Ayush Singh committed
pub struct Imager {
    pub latest_version: Option<Version>,
Ayush Singh's avatar
Ayush Singh committed
    pub devices: Vec<Device>,
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Device {
    pub name: String,
    pub description: String,
    pub icon: Url,
    pub flasher: Flasher,
    pub documentation: Url,
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct OsList {
    pub name: String,
    pub description: String,
    pub icon: Url,
    pub release_date: chrono::NaiveDate,
Ayush Singh's avatar
Ayush Singh committed
    #[serde(with = "const_hex")]
    pub image_sha256: [u8; 32],
    pub extract_path: Option<String>,
Ayush Singh's avatar
Ayush Singh committed
    pub devices: HashSet<String>,
    pub tags: HashSet<String>,
}

#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
pub enum Flasher {
    SdCard,
    BeagleConnectFreedom,
    Msp430Usb,
}

impl Config {
    pub fn from_json(data: &[u8]) -> serde_json::Result<Self> {
        serde_json::from_slice(data)
    }

    pub fn devices(&self) -> &[Device] {
        &self.imager.devices
    }

Ayush Singh's avatar
Ayush Singh committed
    pub fn images_by_device<'a>(
        &'a self,
        device: &'a Device,
Ayush Singh's avatar
Ayush Singh committed
    ) -> impl Iterator<Item = &'a OsList> + 'a {
Ayush Singh's avatar
Ayush Singh committed
        self.os_list
            .iter()
            .filter(|x| x.devices.contains(&device.name))
    pub fn merge_compact(mut self, comp: compact::Config) -> Self {
        let mut mapper = HashMap::new();

        // Imager
        self.imager.devices.reserve(comp.imager.devices.len());
        for d in comp.imager.devices {
            if d.name == "No filtering" {
                continue;
            }

            let temp = d.convert(&mut mapper);
            self.imager.devices.push(temp);
        }

        // OsList
        self.os_list.reserve(comp.os_list.len());
        for item in comp.os_list {
            let mut temp = item.convert(&mapper);
            self.os_list.append(&mut temp);
        }

        self
    }
    pub async fn destinations(&self) -> HashSet<Destination> {
            Flasher::SdCard => tokio::task::block_in_place(sd::destinations),
            Flasher::BeagleConnectFreedom => tokio::task::block_in_place(bcf::possible_devices),
            Flasher::Msp430Usb => tokio::task::block_in_place(msp430::possible_devices),
    pub fn file_filter(&self) -> (&'static str, &'static [&'static str]) {
        match self {
            Flasher::SdCard => ("image", &["img", "xz"]),
            Flasher::BeagleConnectFreedom => ("firmware", &["bin", "hex", "txt", "xz"]),
            Flasher::Msp430Usb => ("firmware", &["hex", "txt", "xz"]),
}

#[cfg(test)]
mod tests {
    #[test]
    fn basic() {
        let data = include_bytes!("../../../config.json");
        super::Config::from_json(data).unwrap();