return error when resource origin path doesn't exist

This commit is contained in:
amrbashir 2024-07-18 04:32:37 +03:00
parent 1f1ca94bea
commit b0b0205280
No known key found for this signature in database
GPG Key ID: BBD7A47A2003FF33
2 changed files with 14 additions and 4 deletions

View File

@ -371,6 +371,10 @@ pub enum Error {
#[cfg(feature = "resources")] #[cfg(feature = "resources")]
#[error("could not walk directory `{0}`, try changing `allow_walk` to true on the `ResourcePaths` constructor.")] #[error("could not walk directory `{0}`, try changing `allow_walk` to true on the `ResourcePaths` constructor.")]
NotAllowedToWalkDir(std::path::PathBuf), NotAllowedToWalkDir(std::path::PathBuf),
/// Resourece path doesn't exist
#[cfg(feature = "resources")]
#[error("resource path `{0}` doesn't exist")]
ResourcePathNotFound(std::path::PathBuf),
} }
/// Reconstructs a path from its components using the platform separator then converts it to String and removes UNC prefixes on Windows if it exists. /// Reconstructs a path from its components using the platform separator then converts it to String and removes UNC prefixes on Windows if it exists.

View File

@ -167,8 +167,12 @@ impl<'a> ResourcePathsIter<'a> {
self.next_current_path() self.next_current_path()
} }
fn resource_from_path(&mut self, path: &Path) -> Resource { fn resource_from_path(&mut self, path: &Path) -> crate::Result<Resource> {
Resource { if !path.exists() {
return Err(crate::Error::ResourcePathNotFound(path.to_path_buf()));
}
Ok(Resource {
path: path.to_path_buf(), path: path.to_path_buf(),
target: self target: self
.current_dest .current_dest
@ -192,7 +196,7 @@ impl<'a> ResourcePathsIter<'a> {
} }
}) })
.unwrap_or_else(|| resource_relpath(path)), .unwrap_or_else(|| resource_relpath(path)),
} })
} }
fn next_current_path(&mut self) -> Option<crate::Result<Resource>> { fn next_current_path(&mut self) -> Option<crate::Result<Resource>> {
@ -223,7 +227,7 @@ impl<'a> ResourcePathsIter<'a> {
} }
} }
} else { } else {
Some(Ok(self.resource_from_path(&path))) Some(self.resource_from_path(&path))
} }
} }
@ -470,6 +474,8 @@ mod tests {
("../src/tiles/**/*".into(), "tiles".into()), ("../src/tiles/**/*".into(), "tiles".into()),
("*.toml".into(), "".into()), ("*.toml".into(), "".into()),
("*.conf.json".into(), "json".into()), ("*.conf.json".into(), "json".into()),
("../non-existent-file".into(), "asd".into()), // invalid case
("../non/*".into(), "asd".into()), // invalid case
]), ]),
true, true,
) )