Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move enum Chip from fwidgen to hubtools lib. #45

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions fwidgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use anyhow::{anyhow, Context, Result};
use clap::{Parser, ValueEnum};
use hubtools::RawHubrisArchive;
use hubtools::{Chip, RawHubrisArchive};
use sha2::{digest::DynDigest, Digest as _, Sha256};
use sha3::Sha3_256;
use std::{fmt, ops::Range, str};
Expand Down Expand Up @@ -63,40 +63,6 @@ impl Digest {
}
}

#[derive(Debug)]
enum Chip {
Lpc55,
Stm32,
}

impl TryFrom<&RawHubrisArchive> for Chip {
type Error = anyhow::Error;

fn try_from(archive: &RawHubrisArchive) -> Result<Self> {
let manifest = archive.extract_file("app.toml")?;
let manifest: toml::Value = toml::from_str(
str::from_utf8(&manifest).context("manifest bytes to UTF8")?,
)
.context("manifest UTF8 to TOML")?;

let chip = manifest
.as_table()
.ok_or(anyhow!("manifest isn't a table"))?
.get("chip")
.ok_or(anyhow!("no key \"chip\" in manifest"))?
.as_str()
.ok_or(anyhow!("value for key \"chip\" isn't a string"))?;

if chip.contains("lpc55") {
Ok(Chip::Lpc55)
} else if chip.contains("stm32") {
Ok(Chip::Stm32)
} else {
Err(anyhow!("Unsupported chip: {}", chip))
}
}
}

// Return a Range describing the named flash range from memory.toml).
fn get_flash_range(
name: &str,
Expand Down
64 changes: 48 additions & 16 deletions hubtools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,24 +830,12 @@ impl RawHubrisArchive {
}

fn is_lpc55(&self) -> Result<(), Error> {
let manifest = self.extract_file("app.toml")?;
let manifest: toml::Value = toml::from_str(
std::str::from_utf8(&manifest).map_err(Error::BadManifest)?,
)
.map_err(Error::BadToml)?;
let chip = manifest
.as_table()
.ok_or(Error::BadTomlType)?
.get("chip")
.ok_or(Error::BadTomlType)?
.as_str()
.ok_or(Error::BadTomlType)?
.to_owned();
let chip = Chip::try_from(self)?;

if !chip.contains("lpc55") {
Err(Error::WrongChip(chip))
} else {
if chip == Chip::Lpc55 {
Ok(())
} else {
Err(Error::WrongChip(chip.to_string()))
}
}

Expand Down Expand Up @@ -971,6 +959,50 @@ impl RawHubrisArchive {
}
}

#[derive(Debug, PartialEq)]
pub enum Chip {
Lpc55,
Stm32,
}

impl TryFrom<&RawHubrisArchive> for Chip {
type Error = Error;

fn try_from(archive: &RawHubrisArchive) -> Result<Self, Error> {
let manifest = archive.extract_file("app.toml")?;
let manifest: toml::Value = toml::from_str(
std::str::from_utf8(&manifest)
.map_err(Error::BadCommentEncoding)?,
)
.map_err(Error::BadToml)?;

let chip = manifest
.as_table()
.ok_or(Error::BadTomlType)?
.get("chip")
.ok_or(Error::BadTomlType)?
.as_str()
.ok_or(Error::BadTomlType)?;

if chip.contains("lpc55") {
Ok(Chip::Lpc55)
} else if chip.contains("stm32") {
Ok(Chip::Stm32)
} else {
Err(Error::WrongChip(chip.to_string()))
}
}
}

impl std::fmt::Display for Chip {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Chip::Lpc55 => write!(f, "lpc55"),
Chip::Stm32 => write!(f, "stm32"),
}
}
}

mod header {
use zerocopy::{AsBytes, FromBytes};

Expand Down
Loading