Struct FileMerger

Source
pub struct FileMerger<'a>(/* private fields */);
Expand description

Simliar to RsMerger but provides dedicated methods to work with Paths and Files.

Implementations§

Source§

impl<'a> FileMerger<'a>

Source

pub fn new() -> Self

Creates a new FileMerger builder.

Source

pub fn skip_head(&mut self, skip: Skip<'a>) -> &mut Self

Configures this merger to skip partial of contents from the head of each file.

Source

pub fn skip_tail(&mut self, skip: Skip<'a>) -> &mut Self

Configures this merger to skip partial of contents from the tail of each file.

Source

pub fn pad_with(&mut self, padding: Pad<'a>) -> &mut Self

Configures this merger to fill some padding before, between or after the file contents.

Source

pub fn force_ending_newline(&mut self, newline: Newline) -> &mut Self

Configures this merger to force the presence of ending newline after each file.

Source

pub fn with_paths<P, W>(&self, paths: Vec<P>, writer: &mut W) -> Result<()>
where P: AsRef<Path>, W: Write,

Opens file paths given and merges file contents into the given writer according to the given configrations.

Nothing that this method will return an error if any path given is not point to a regular file. For an variant that ignores invalid paths, see with_paths_lossy.

§Example
use admerge::{FileMerger, Skip, Pad, Newline, Result};
use std::fs::OpenOptions;

fn main() -> Result<()> {
    let mut file = OpenOptions::new().append(true).create(true).open("merged.txt")?;

    // Configures merger.
    let mut merger = FileMerger::new();
    merger.skip_tail(Skip::LinesOnce(1));
    merger.pad_with(Pad::Before(b"leading contents\n"));
    merger.force_ending_newline(Newline::Lf);

    // Merges sources into one.
    merger.with_paths(vec!["foo.txt", "bar.txt", "baz.txt"], &mut file)?;

    Ok(())
}
§Errors

Returns an error variant of ErrorKind::NothingPassed if the given path vector is empty;

Returns an error variant of ErrorKind::InvalidPath if the given paths contain invalid path.

Returns an error variant of ErrorKind::InvalidSkip if the given Skips cannot applied to the given sources;

Returns an error variant of ErrorKind::Io if any I/O errors were encountered.

Source

pub fn with_paths_lossy<P, W>( &self, paths: Vec<P>, writer: &mut W, ) -> Result<()>
where P: AsRef<Path>, W: Write,

Opens every file path given if path points to a regular file, and then merges file contents into the given writer according to the given configrations.

See also with_paths.

§Example
use admerge::{FileMerger, Skip, Pad, Newline, Result};
use std::fs::OpenOptions;

fn main() -> Result<()> {
    let mut file = OpenOptions::new().append(true).create(true).open("merged.txt")?;

    // Configures merger.
    let mut merger = FileMerger::new();
    merger.skip_tail(Skip::LinesOnce(1));
    merger.pad_with(Pad::Before(b"leading contents\n"));
    merger.force_ending_newline(Newline::Lf);

    // Merges sources into one.
    merger.with_paths_lossy(vec!["foo.txt", "bar.txt", "not a file path"], &mut file)?;

    Ok(())
}
§Errors

Returns an error variant of ErrorKind::NothingPassed if the given path vector is empty;

Returns an error variant of ErrorKind::InvalidSkip if the given Skips cannot applied to the given sources;

Returns an error variant of ErrorKind::Io if any I/O errors were encountered.

Source

pub fn with_files<W>(&self, files: Vec<File>, writer: &mut W) -> Result<()>
where W: Write,

Reads sequentially from the given files and merges their contents into the given writer according to the given configrations.

§Example
use admerge::{FileMerger, Skip, Pad, Newline, Result};
use std::fs::{File, OpenOptions};

fn main() -> Result<()> {
    let f1 = File::open("foo.txt")?;
    let f2 = File::open("bar.txt")?;
    let f3 = File::open("baz.txt")?;
    let mut file = OpenOptions::new().append(true).create(true).open("merged.txt")?;

    // Configures merger.
    let mut merger = FileMerger::new();
    merger.skip_tail(Skip::LinesOnce(1));
    merger.pad_with(Pad::Before(b"leading contents\n"));
    merger.force_ending_newline(Newline::Lf);

    // Merges sources into one.
    merger.with_files(vec![f1, f2, f3], &mut file)?;

    Ok(())
}
§Errors

Returns an error variant of ErrorKind::NothingPassed if the given path vector is empty;

Returns an error variant of ErrorKind::InvalidSkip if the given Skips cannot applied to the given sources;

Returns an error variant of ErrorKind::Io if any I/O errors were encountered.

Trait Implementations§

Source§

impl<'a> Clone for FileMerger<'a>

Source§

fn clone(&self) -> FileMerger<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for FileMerger<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for FileMerger<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for FileMerger<'a>

§

impl<'a> RefUnwindSafe for FileMerger<'a>

§

impl<'a> Send for FileMerger<'a>

§

impl<'a> Sync for FileMerger<'a>

§

impl<'a> Unpin for FileMerger<'a>

§

impl<'a> UnwindSafe for FileMerger<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.