Using #[from] in a thiserror enum is an antipattern, IMO. I kind of wish it wasn't included at all because it leads people to this design pattern where errors are just propagated upwards without any type differentiation or additional context.<p>You can absolutely have two different enum variants from the same source type. It would look something like:<p><pre><code> #[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,
}
}
</code></pre>
This does mean that you need to manually specify which error variant you are returning rather than just using ?:<p><pre><code> create_dir(path).map_err(|err| MyErrorType::CreateStagingDirectory {
source: err, path: path.clone()
})?;
</code></pre>
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.