ipfs_unixfs/dir/
directory.rs1use crate::pb::FlatUnixFs;
2use core::fmt;
3
4pub(crate) fn check_directory_supported(
6 flat: FlatUnixFs<'_>,
7) -> Result<FlatUnixFs<'_>, UnexpectedDirectoryProperties> {
8 let data = flat.data.Data.as_deref();
9 if flat.data.filesize.is_some()
10 || !flat.data.blocksizes.is_empty()
11 || flat.data.hashType.is_some()
12 || flat.data.fanout.is_some()
13 || !data.unwrap_or_default().is_empty()
14 {
15 let data = data.map(|s| s.to_vec());
16 Err(UnexpectedDirectoryProperties {
17 filesize: flat.data.filesize,
18 blocksizes: flat.data.blocksizes,
19 hash_type: flat.data.hashType,
20 fanout: flat.data.fanout,
21 data,
22 })
23 } else {
24 Ok(flat)
25 }
26}
27
28#[derive(Debug)]
30pub struct UnexpectedDirectoryProperties {
31 filesize: Option<u64>,
33 blocksizes: Vec<u64>,
35 hash_type: Option<u64>,
37 fanout: Option<u64>,
39 data: Option<Vec<u8>>,
41}
42
43impl fmt::Display for UnexpectedDirectoryProperties {
44 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
45 write!(
46 fmt,
47 "filesize={:?}, {} blocksizes, hash_type={:?}, fanout={:?}, data=[",
48 self.filesize,
49 self.blocksizes.len(),
50 self.hash_type,
51 self.fanout,
52 )?;
53
54 for b in self.data.as_deref().unwrap_or_default() {
55 write!(fmt, "{:02x}", b)?;
56 }
57
58 write!(fmt, "]")
59 }
60}