Skip to content

Commit 4e5ba38

Browse files
committed
Cleaning up implementation
1 parent f0c6543 commit 4e5ba38

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

forged-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ probe-rs-cli-util = "0.13.0"
2929
thiserror = "1.0.30"
3030
dotenv = "0.15"
3131
uuid = { version = "1", features = ["serde"] }
32+
semver = "1"

forged-cli/src/functions/download.rs

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,27 @@ pub async fn download(
2323
chip: Option<String>,
2424
version: Option<String>,
2525
) -> Result<()> {
26-
println!("⛅ Grabbing binaries from the server ...");
27-
2826
let query = client.run_query(Chips::build(())).await?;
2927
let chips = query.current_provisioner.project.chips;
3028

29+
let chips_string =
30+
chips
31+
.iter()
32+
.map(|chip| chip.name.clone())
33+
.fold(String::new(), |acc, chip| {
34+
if acc.is_empty() {
35+
chip.to_string()
36+
} else {
37+
format!("{acc}, {chip}")
38+
}
39+
});
40+
3141
let chip = if let Some(chip_name) = chip {
3242
chips
3343
.iter()
3444
.find(|chip| chip.name == chip_name)
3545
.ok_or_else(|| {
36-
anyhow!(
37-
"Chip {chip_name} not found. Available chips: {:?}",
38-
chips.iter().map(|chip| chip.name.clone())
39-
)
46+
anyhow!("Chip `{chip_name}` not found. Available chips: [ {chips_string} ]")
4047
})?
4148
} else {
4249
match chips.len() {
@@ -45,41 +52,57 @@ pub async fn download(
4552
"No chips have been configured for this project. Add one to the project first."
4653
)))
4754
}
48-
1 => chips.iter().next().unwrap(),
49-
_ => return Err(Error::Other(anyhow!(
50-
"Multiple chips found for this project. Please specify one. Available chips: {:?}",
51-
chips.iter().map(|chip| chip.name.clone())
52-
))),
55+
1 => chips.first().unwrap(),
56+
_ => {
57+
return Err(Error::Other(anyhow!(
58+
"Multiple chips found for this project. Please specify one. Available chips: [ {chips_string} ]"
59+
)))
60+
}
5361
}
5462
};
5563

5664
let binaries = &chip.binaries;
5765
let binary = if let Some(version) = version {
66+
let version = semver::Version::parse(&version)?;
5867
binaries
5968
.iter()
6069
.find(|bin| bin.version() == version)
6170
.ok_or_else(|| {
71+
let mut versions: Vec<semver::Version> = binaries.iter().map(|bin| bin.version()).collect();
72+
versions.sort();
73+
versions.reverse();
74+
6275
anyhow!(
63-
"Binary version {version} not found for chip {}. Available versions: {:?}",
76+
"Binary version `{version}` not found for chip `{}`. Available versions: [ {} ]",
6477
chip.name,
65-
binaries.iter().map(|bin| bin.version())
66-
)
78+
versions.iter().fold(String::new(), |acc, version| {
79+
if acc.is_empty() {
80+
version.to_string()
81+
} else {
82+
format!("{acc}, {version}")
83+
}
84+
}
85+
))
6786
})?
6887
} else {
69-
match binaries.len() {
70-
0 => return Err(Error::Other(anyhow!(
71-
"No binaries have been configured for chip {}. Add a binary to this chip first.",
88+
// Otherwise, find the newest binary.
89+
let Some(binary) = binaries.iter().max_by_key(|x| x.version()) else {
90+
return Err(Error::Other(anyhow!(
91+
"No binaries have been uploaded for chip {}",
7292
chip.name
73-
))),
74-
1 => binaries.iter().next().unwrap(),
75-
_ => return Err(Error::Other(anyhow!(
76-
"Multiple binaries found for chip {}. Please specify one. Available versions: {:?}",
77-
chip.name,
78-
binaries.iter().map(|bin| bin.version())
79-
))),
80-
}
93+
)));
94+
};
95+
binary
8196
};
8297

98+
println!(
99+
" -> Flashing firmware v{} onto {} ({})",
100+
binary.version(),
101+
chip.name,
102+
chip.part_number
103+
);
104+
println!("⛅ Grabbing binaries from the server ...");
105+
83106
let result = run_flash_download(client, &chip, binary).await;
84107

85108
if result.is_err() {
@@ -107,7 +130,7 @@ async fn run_flash_download(
107130
}
108131

109132
// Create a new session
110-
let mut session = probe.attach(&chip.name, probe_rs::Permissions::default())?;
133+
let mut session = probe.attach(&chip.part_number, probe_rs::Permissions::default())?;
111134

112135
let target = session.target();
113136

forged-cli/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub enum Error {
5252
Probe(#[from] probe_rs::Error),
5353
FlashOperation(#[from] probe_rs_cli_util::common_options::OperationError),
5454
Other(#[from] anyhow::Error),
55+
Semver(#[from] semver::Error),
5556
}
5657

5758
impl Display for Error {
@@ -64,6 +65,7 @@ impl Display for Error {
6465
}
6566
Error::Other(error) => writeln!(f, "{error}"),
6667
Error::FlashOperation(error) => writeln!(f, "{error}"),
68+
Error::Semver(error) => writeln!(f, "Version format error: {error}"),
6769
}
6870
}
6971
}

forged-cli/src/queries.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod queries {
5151
pub struct Chip {
5252
pub id: Uuid,
5353
pub name: String,
54+
pub part_number: String,
5455
pub binaries: Vec<Binary>,
5556
}
5657

@@ -82,11 +83,14 @@ pub mod queries {
8283
}
8384

8485
impl Binary {
85-
pub fn version(&self) -> String {
86-
format!(
87-
"{}.{}.{}",
88-
self.version_major, self.version_minor, self.version_patch
89-
)
86+
pub fn version(&self) -> semver::Version {
87+
semver::Version {
88+
major: self.version_major as u64,
89+
minor: self.version_minor as u64,
90+
patch: self.version_patch as u64,
91+
pre: Default::default(),
92+
build: Default::default(),
93+
}
9094
}
9195
}
9296

0 commit comments

Comments
 (0)