ipfs_unixfs/dir/
directory.rs

1use crate::pb::FlatUnixFs;
2use core::fmt;
3
4/// Ensures the directory looks like something we actually support.
5pub(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/// Error case for checking if we support this directory.
29#[derive(Debug)]
30pub struct UnexpectedDirectoryProperties {
31    /// filesize is a property of Files
32    filesize: Option<u64>,
33    /// blocksizes is a property of Files
34    blocksizes: Vec<u64>,
35    /// hash_type is a property of HAMT Shards
36    hash_type: Option<u64>,
37    /// fanout is a property of HAMT shards
38    fanout: Option<u64>,
39    /// directories should have no Data
40    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}