Moved DirWriterLister to it's own module.

Removed currently superfluous `DirList` associated type, as it cannot be used without creating an infinite type.
This commit is contained in:
Filipe Rodrigues 2021-01-27 09:02:02 +00:00
parent 03eee9ee66
commit 5504ace431
4 changed files with 44 additions and 24 deletions

View File

@ -30,9 +30,7 @@ pub struct DrvFsWriter;
impl DrvFsWriter {
/// Creates a `.DRV` filesystem
pub fn write_fs<W: io::Write + io::Seek, L: DirWriterLister>(
writer: &mut W, root_entries: L,
) -> Result<(), WriteFsError<L::Error>> {
pub fn write_fs<W: io::Write + io::Seek, L: DirWriterLister>(writer: &mut W, root_entries: L) -> Result<(), WriteFsError<L::Error>> {
// Get the root and write it
let root = DirWriter::new(root_entries);
root.write_entries(writer).map_err(WriteFsError::RootDir)?;

View File

@ -2,35 +2,19 @@
// Modules
pub mod error;
pub mod lister;
// Exports
pub use error::WriteEntriesError;
pub use lister::DirWriterLister;
// Imports
use super::{entry::DirEntryWriterKind, DirEntryWriter};
use super::entry::DirEntryWriterKind;
use std::{
convert::TryFrom,
io::{self, SeekFrom},
};
/// Directory lister
pub trait DirWriterLister: Sized + std::fmt::Debug
where
Self: IntoIterator<Item = Result<DirEntryWriter<Self>, <Self as DirWriterLister>::Error>>,
{
/// Reader used for the files in this directory
type FileReader: std::fmt::Debug + io::Read;
/// Directory lister for each directory in this directory
type DirList: DirWriterLister;
/// Error type for each entry
type Error: std::error::Error + 'static;
/// Returns the number of entries in this lister
fn entries_len(&self) -> u32;
}
/// Directory writer
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct DirWriter<L: DirWriterLister> {

View File

@ -0,0 +1,39 @@
//! Directory writer lister
// Imports
use crate::drv::DirEntryWriter;
use std::io;
/// Directory lister
///
/// This trait serves to provide an interface for the caller
/// to list all files and directories they want to write to
/// the drv file without requiring them all to be read before
/// hand.
///
/// It is similar to a visitor, but with the roles reversed,
/// it is the writer that visits the entries provided by the
/// user by means of the [`IntoIterator`] implementation.
///
/// It is required to know the size before-hand to know how
/// much space to allocate for each directory. It would be
/// possible to write the children first and then the directory
/// to avoid this problem, but the root directory would still
/// need to report it's size, and by putting the directories first
/// readers don't have to seek across the file to read the full
/// directory tree as much.
/// This is, of course, provided the implementor supplies directories
/// before the files, else they will not be placed at the start.
pub trait DirWriterLister: Sized + std::fmt::Debug
where
Self: IntoIterator<Item = Result<DirEntryWriter<Self>, <Self as DirWriterLister>::Error>>,
{
/// Type used to read all files in this directory tree
type FileReader: std::fmt::Debug + io::Read;
/// Error type for each entry
type Error: std::error::Error + 'static;
/// Returns the number of entries in this lister
fn entries_len(&self) -> u32;
}

View File

@ -43,7 +43,6 @@ impl DirLister {
}
impl DirWriterLister for DirLister {
type DirList = Self;
type Error = NextError;
type FileReader = fs::File;
@ -53,7 +52,7 @@ impl DirWriterLister for DirLister {
}
impl Iterator for DirLister {
type Item = Result<DirEntryWriter<<Self as DirWriterLister>::DirList>, <Self as DirWriterLister>::Error>;
type Item = Result<DirEntryWriter<Self>, <Self as DirWriterLister>::Error>;
fn next(&mut self) -> Option<Self::Item> {
// Get the next entry