pub struct FileMerger<'a>(/* private fields */);
Implementations§
Source§impl<'a> FileMerger<'a>
impl<'a> FileMerger<'a>
Sourcepub fn skip_head(&mut self, skip: Skip<'a>) -> &mut Self
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.
Sourcepub fn skip_tail(&mut self, skip: Skip<'a>) -> &mut Self
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.
Sourcepub fn pad_with(&mut self, padding: Pad<'a>) -> &mut Self
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.
Sourcepub fn force_ending_newline(&mut self, newline: Newline) -> &mut Self
pub fn force_ending_newline(&mut self, newline: Newline) -> &mut Self
Configures this merger to force the presence of ending newline after each file.
Sourcepub fn with_paths<P, W>(&self, paths: Vec<P>, writer: &mut W) -> Result<()>
pub fn with_paths<P, W>(&self, paths: Vec<P>, writer: &mut W) -> Result<()>
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 Skip
s cannot
applied to the given sources;
Returns an error variant of ErrorKind::Io
if any I/O errors were encountered.
Sourcepub fn with_paths_lossy<P, W>(
&self,
paths: Vec<P>,
writer: &mut W,
) -> Result<()>
pub fn with_paths_lossy<P, W>( &self, paths: Vec<P>, writer: &mut W, ) -> Result<()>
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 Skip
s cannot
applied to the given sources;
Returns an error variant of ErrorKind::Io
if any I/O errors were encountered.
Sourcepub fn with_files<W>(&self, files: Vec<File>, writer: &mut W) -> Result<()>where
W: Write,
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 Skip
s 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>
impl<'a> Clone for FileMerger<'a>
Source§fn clone(&self) -> FileMerger<'a>
fn clone(&self) -> FileMerger<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more