Newer
Older
//! Configuration for bb-imager to use.
pub mod compact;
use std::collections::{HashMap, HashSet};
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
pub imager: Imager,
pub os_list: Vec<OsList>,
#[derive(Deserialize, Serialize, Debug, Default, Clone)]
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Device {
pub name: String,
pub description: String,
pub icon: Url,
pub flasher: Flasher,
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct OsList {
pub name: String,
pub description: String,
pub icon: Url,
pub release_date: chrono::NaiveDate,
pub tags: HashSet<String>,
}
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
pub enum Flasher {
SdCard,
BeagleConnectFreedom,
}
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
}
pub fn images_by_device<'a>(
&'a self,
device: &'a Device,
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();