diff --git a/naga/hlsl-snapshots/src/lib.rs b/naga/hlsl-snapshots/src/lib.rs index 6340fd11c..a479284fa 100644 --- a/naga/hlsl-snapshots/src/lib.rs +++ b/naga/hlsl-snapshots/src/lib.rs @@ -1,20 +1,27 @@ -use std::{error::Error, fmt::Display, fs, io, path::Path}; +use std::{error::Error, fmt::Display, fs, io, path::Path, path::PathBuf}; -use anyhow::{anyhow, ensure}; +use anyhow::anyhow; use nanoserde::{self, DeRon, DeRonErr, SerRon}; #[derive(Debug)] -struct BadRonParse(BadRonParseKind); +struct BadRonParse { + path: PathBuf, + kind: BadRonParseKind, +} impl Display for BadRonParse { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "failed to read RON configuration of HLSL snapshot test") + write!( + f, + "failed to read HLSL snapshot test RON configuration file `{}`", + self.path.display() + ) } } impl Error for BadRonParse { fn source(&self) -> Option<&(dyn Error + 'static)> { - Some(&self.0) + Some(&self.kind) } } @@ -68,12 +75,23 @@ impl Config { pub fn from_path(path: impl AsRef) -> anyhow::Result { let path = path.as_ref(); - let raw_config = fs::read_to_string(path) - .map_err(|source| BadRonParse(BadRonParseKind::Read { source }))?; - let config = Config::deserialize_ron(&raw_config) - .map_err(|source| BadRonParse(BadRonParseKind::Parse { source }))?; - ensure!(!config.is_empty(), BadRonParse(BadRonParseKind::Empty)); - Ok(config) + (|| -> Result { + let raw_config = + fs::read_to_string(path).map_err(|source| BadRonParseKind::Read { source })?; + let config = Config::deserialize_ron(&raw_config) + .map_err(|source| BadRonParseKind::Parse { source })?; + if config.is_empty() { + return Err(BadRonParseKind::Empty); + } + Ok(config) + })() + .map_err(|kind| { + BadRonParse { + path: path.to_owned(), + kind, + } + .into() + }) } pub fn to_file(&self, path: impl AsRef) -> anyhow::Result<()> {