Skip to content

improve yaml path errors #117

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

Merged
merged 2 commits into from
Aug 13, 2022
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
4 changes: 2 additions & 2 deletions src/convert/convert_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{Read, Write};
use std::str::FromStr;
use std::{fs::File, path::Path};

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum InputFormat {
Xml,
Expand All @@ -23,7 +23,7 @@ impl FromStr for InputFormat {
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum OutputFormat {
Xml,
Expand Down
4 changes: 2 additions & 2 deletions src/interrupts/svd_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ struct PeripheralXml {
interrupt: Option<Vec<Interrupt>>,
}

#[derive(Deserialize, Debug, PartialEq)]
#[derive(Deserialize, Debug, PartialEq, Eq)]
pub struct Interrupt {
pub name: String,
pub description: Option<String>,
pub value: u32,
}

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Eq)]
pub struct Peripheral {
pub name: String,
pub interrupt: Vec<Interrupt>,
Expand Down
2 changes: 1 addition & 1 deletion src/patch/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl DeviceExt for Device {
.collect::<Vec<_>>();
let mut new = match pcopysrc.as_slice() {
[ppath, pcopyname] => {
let f = File::open(abspath(path, Path::new(ppath))).unwrap();
let f = File::open(abspath(path, Path::new(ppath))?)?;
let mut contents = String::new();
(&f).read_to_string(&mut contents).unwrap();
let filedev = svd_parser::parse(&contents)
Expand Down
10 changes: 6 additions & 4 deletions src/patch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn process_file(yaml_file: &Path) -> Result<()> {
root.get_str("_svd")?
.ok_or_else(|| anyhow!("You must have an svd key in the root YAML file"))?,
),
);
)?;
let mut svdpath_out = svdpath.clone();
svdpath_out.set_extension("svd.patched");
let f = File::open(svdpath)?;
Expand All @@ -69,8 +69,8 @@ pub fn process_file(yaml_file: &Path) -> Result<()> {
}

/// Gets the absolute path of relpath from the point of view of frompath.
fn abspath(frompath: &Path, relpath: &Path) -> PathBuf {
std::fs::canonicalize(frompath.parent().unwrap().join(relpath)).unwrap()
fn abspath(frompath: &Path, relpath: &Path) -> Result<PathBuf, std::io::Error> {
std::fs::canonicalize(frompath.parent().unwrap().join(relpath))
}

/// Recursively loads any included YAML files.
Expand All @@ -80,7 +80,9 @@ pub fn yaml_includes(parent: &mut Hash) -> Result<Vec<PathBuf>> {
let self_path = PathBuf::from(parent.get(&y_path).unwrap().str()?);
let inc = parent.get_vec("_include")?.unwrap_or(&Vec::new()).clone();
for relpath in inc {
let path = abspath(&self_path, Path::new(relpath.as_str().unwrap()));
let relpath = relpath.as_str().unwrap();
let path = abspath(&self_path, Path::new(relpath))
.with_context(|| anyhow!("Opening file \"{}\" from file {:?}", relpath, self_path))?;
if included.contains(&path) {
continue;
}
Expand Down