ipld_car/error/
invalid.rs1#[cfg(feature = "vfs")]
2use crate::error::vfs_err;
3
4use libipld::Cid;
5use std::{
6 io,
7 path::{Path, PathBuf},
8};
9use thiserror::Error;
10#[cfg(feature = "vfs")]
11use vfs::error::{VfsError, VfsErrorKind};
12
13#[cfg_attr(feature = "std", derive(Debug))]
14#[derive(Error)]
15pub enum InvalidErr {
16 #[error("Path `{0:?}` is NOT a directory")]
17 NotADirectory(PathBuf),
18 #[error("Path `{0:?}` IS a directory")]
19 IsADirectory(PathBuf),
20 #[error("Invalid file name in path `{0:?}`")]
21 FileName(PathBuf),
22 #[error("Path is not an utf-8: `{0:?}`")]
23 NotUtf8Path(PathBuf),
24 #[error("Directory `{0:?}` already exists")]
25 AlreadyExists(PathBuf),
26 #[error("Path `{0:?}` points to unavailable block (cid={1}) in this CAR")]
27 IsAMissingBlock(PathBuf, String),
28
29 #[error("Header length is invalid")]
30 HeaderLen,
31 #[error("Block length is invalid")]
32 BlockLen,
33 #[error("Invalid CBOR header: {0}")]
34 CborDec(#[from] ciborium::de::Error<io::Error>),
35 #[error("Invalid CBOR value: {0}")]
36 CborEnc(String),
37 #[error("Invalid CID: {0}")]
38 Cid(#[from] libipld::cid::Error),
39 #[error("Config builder failed: {0}")]
40 ConfigBuilder(String),
41}
42
43impl InvalidErr {
44 pub fn not_a_dir<P: AsRef<Path>>(path: P) -> Self {
45 Self::NotADirectory(path.as_ref().to_owned())
46 }
47
48 pub fn is_a_dir<P: AsRef<Path>>(path: P) -> Self {
49 Self::IsADirectory(path.as_ref().to_owned())
50 }
51
52 pub fn file_name<P: AsRef<Path>>(path: P) -> Self {
53 Self::FileName(path.as_ref().to_owned())
54 }
55
56 pub fn not_utf8_path<P: AsRef<Path>>(path: P) -> Self {
57 Self::NotUtf8Path(path.as_ref().to_owned())
58 }
59
60 pub fn exists<P: AsRef<Path>>(path: P) -> Self {
61 Self::AlreadyExists(path.as_ref().to_owned())
62 }
63
64 pub fn is_a_miss_block<P: AsRef<Path>>(path: P, cid: &Cid) -> Self {
65 Self::IsAMissingBlock(path.as_ref().to_owned(), cid.to_string())
66 }
67}
68
69#[cfg(feature = "vfs")]
70impl From<InvalidErr> for VfsError {
71 fn from(ci: InvalidErr) -> Self {
72 match ci {
73 InvalidErr::AlreadyExists(..) => VfsErrorKind::DirectoryExists.into(),
74 InvalidErr::CborDec(cbor) => VfsErrorKind::Other(cbor.to_string()).into(),
75 InvalidErr::CborEnc(cbor) => VfsErrorKind::Other(cbor).into(),
76 InvalidErr::Cid(cid) => VfsErrorKind::Other(cid.to_string()).into(),
77 e => vfs_err(VfsErrorKind::NotSupported, e),
78 }
79 }
80}