refactor: rename dir submodule to directory

also the file was forgotten.
This commit is contained in:
Joonas Koivunen 2020-06-17 12:22:32 +03:00
parent 6be2556341
commit bfd8722ae4
2 changed files with 62 additions and 2 deletions

View File

@ -7,8 +7,8 @@ use std::fmt;
mod sharded_lookup;
pub use sharded_lookup::{Cache, LookupError, ShardError, ShardedLookup};
mod dir;
pub(crate) use dir::{check_directory_supported, UnexpectedDirectoryProperties};
mod directory;
pub(crate) use directory::{check_directory_supported, UnexpectedDirectoryProperties};
pub(crate) fn check_hamtshard_supported(
mut flat: FlatUnixFs<'_>,

View File

@ -0,0 +1,60 @@
use crate::pb::FlatUnixFs;
use std::fmt;
/// Ensures the directory looks like something we actually support.
pub(crate) fn check_directory_supported(
flat: FlatUnixFs<'_>,
) -> Result<FlatUnixFs<'_>, UnexpectedDirectoryProperties> {
let data = flat.data.Data.as_deref();
if flat.data.filesize.is_some()
|| !flat.data.blocksizes.is_empty()
|| flat.data.hashType.is_some()
|| flat.data.fanout.is_some()
|| !data.unwrap_or_default().is_empty()
{
let data = data.map(|s| s.to_vec());
Err(UnexpectedDirectoryProperties {
filesize: flat.data.filesize,
blocksizes: flat.data.blocksizes,
hash_type: flat.data.hashType,
fanout: flat.data.fanout,
data,
})
} else {
Ok(flat)
}
}
/// Error case for checking if we support this directory.
#[derive(Debug)]
pub struct UnexpectedDirectoryProperties {
/// filesize is a property of Files
filesize: Option<u64>,
/// blocksizes is a property of Files
blocksizes: Vec<u64>,
/// hash_type is a property of HAMT Shards
hash_type: Option<u64>,
/// fanout is a property of HAMT shards
fanout: Option<u64>,
/// directories should have no Data
data: Option<Vec<u8>>,
}
impl fmt::Display for UnexpectedDirectoryProperties {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
fmt,
"filesize={:?}, {} blocksizes, hash_type={:?}, fanout={:?}, data=[",
self.filesize,
self.blocksizes.len(),
self.hash_type,
self.fanout,
)?;
for b in self.data.as_deref().unwrap_or_default() {
write!(fmt, "{:02x}", b)?;
}
write!(fmt, "]")
}
}