Skip to content

Commit 83628a0

Browse files
committed
Correct path extension checks for cargo package metadata
1 parent 7320026 commit 83628a0

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

cargo-espflash/src/package_metadata.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
ffi::OsStr,
23
fs::read_to_string,
34
path::{Path, PathBuf},
45
};
@@ -23,35 +24,42 @@ pub struct Meta {
2324
}
2425

2526
impl CargoEspFlashMeta {
26-
pub fn load<P: AsRef<Path>>(path: P) -> Result<CargoEspFlashMeta> {
27-
let path = path.as_ref();
28-
if !path.exists() {
27+
pub fn load<P>(manifest: P) -> Result<CargoEspFlashMeta>
28+
where
29+
P: AsRef<Path>,
30+
{
31+
let manifest = manifest.as_ref();
32+
if !manifest.exists() {
2933
return Err(Error::NoProject.into());
3034
}
31-
let toml = read_to_string(path)
35+
36+
let toml = read_to_string(manifest)
3237
.into_diagnostic()
3338
.wrap_err("Failed to read Cargo.toml")?;
39+
3440
let manifest = Manifest::<Meta>::from_slice_with_metadata(toml.as_bytes())
3541
.map_err(move |e| TomlError::new(e, toml))
3642
.wrap_err("Failed to parse Cargo.toml")?;
43+
3744
let meta = manifest
3845
.package
3946
.and_then(|pkg| pkg.metadata)
4047
.unwrap_or_default()
4148
.espflash
4249
.unwrap_or_default();
43-
match meta.partition_table {
44-
Some(table) if !table.ends_with(".csv") => {
45-
return Err(Error::InvalidPartitionTablePath.into())
50+
51+
if let Some(table) = &meta.partition_table {
52+
if table.extension() != Some(OsStr::new("csv")) {
53+
return Err(Error::InvalidPartitionTablePath.into());
4654
}
47-
_ => {}
4855
}
49-
match meta.bootloader {
50-
Some(table) if !table.ends_with(".bin") => {
51-
return Err(Error::InvalidBootloaderPath.into())
56+
57+
if let Some(bootloader) = &meta.bootloader {
58+
if bootloader.extension() != Some(OsStr::new("bin")) {
59+
return Err(Error::InvalidBootloaderPath.into());
5260
}
53-
_ => {}
5461
}
62+
5563
Ok(meta)
5664
}
5765
}

0 commit comments

Comments
 (0)