(no title)
dmart | 1 year ago
You can absolutely have two different enum variants from the same source type. It would look something like:
#[derive(Debug, Error)]
pub(crate) enum MyErrorType {
#[error("failed to create staging directory at {}", path.display())]
CreateStagingDirectory{
source: std::io::Error,
path: std::path::PathBuf,
},
#[error("failed to copy files to staging directory")]
CopyFiles{
source: std::io::Error,
}
}
This does mean that you need to manually specify which error variant you are returning rather than just using ?: create_dir(path).map_err(|err| MyErrorType::CreateStagingDirectory {
source: err, path: path.clone()
})?;
but I would argue that that is the entire point of defining a specific error type. If you don't care about the context and only that an io::Error occurred, then just return that directly or use a type-erased error.
shepmaster|1 year ago
1. No need to use the closure
2. No need to carry the source error over yourself (`context` does this for you)
3. No need to explicitly call `clone` on the path (`context` does this for you)