Skip to main content

ipld_car/error/
not_found.rs

1use crate::car::BlockId;
2#[cfg(feature = "vfs")]
3use crate::error::vfs_err;
4
5use std::path::{Path, PathBuf};
6use thiserror::Error;
7#[cfg(feature = "vfs")]
8use vfs::error::{VfsError, VfsErrorKind};
9
10#[cfg_attr(feature = "std", derive(Debug))]
11#[derive(Error)]
12pub enum NotFoundErr {
13	#[error("Path `{0:?}` not found")]
14	Path(PathBuf),
15	#[error("BlockId {0:?} not found")]
16	BlockId(BlockId),
17	#[error("Path `{0:?} does not contain a file name")]
18	FileName(PathBuf),
19	#[error("CID is missing on an Directory Entry")]
20	CidOnDirEntry,
21	#[error("CID is missing on block {0:?}")]
22	CidOnBlock(BlockId),
23}
24
25impl NotFoundErr {
26	pub fn path<P: AsRef<Path>>(p: P) -> Self {
27		Self::Path(p.as_ref().to_owned())
28	}
29
30	pub fn file_name<P: AsRef<Path>>(p: P) -> Self {
31		Self::FileName(p.as_ref().to_owned())
32	}
33}
34
35#[cfg(feature = "vfs")]
36impl From<NotFoundErr> for VfsError {
37	fn from(nf: NotFoundErr) -> Self {
38		match nf {
39			e @ NotFoundErr::Path(..) => vfs_err(VfsErrorKind::FileNotFound, e),
40			e @ NotFoundErr::FileName(..) => vfs_err(VfsErrorKind::FileNotFound, e),
41			e => VfsErrorKind::Other(e.to_string()).into(),
42		}
43	}
44}