Struct RsMerger

Source
pub struct RsMerger<'a> { /* private fields */ }
Expand description

A Merger that can merge multiple sources that implement Read and Seek into one.

Generally speaking, when using RsMerger, you’ll first call new to create a merger builder, then call configuration methods to set each option. And eventually, call merge_sources_into to actually merge multiple sources into a given writer.

§Behaviours

The current algorithm to merge sources is described as following:

  1. If any leading padding is given by pad_with, writes it into the writer.
  2. For each source, if skip range is given by skip_head and skip_tail, writes the contents remaining into the writer.
  3. For each source, if forcing ending newline option is set by force_ending_newline, writes a ending newline into the writer if this source is not ends with a newline.
  4. For each source, if any inner padding is given by pad_with, writes it into the writer.
  5. If any ending padding is given by pad_with, writes it into the writer.

§Examples

use admerge::{RsMerger, Skip, Pad, Newline, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new(" record 1\n date created: 2000/01/01\n");
    let mut c2 = Cursor::new(" record 2\n date created: 2000/01/01\n");
    let mut c3 = Cursor::new(" record 3\n date created: 2000/01/01\n");
    let mut buf = Vec::new();

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

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        "header\n record 1\n record 2\n record 3\n date created: 2000/01/01\n"
    );

    Ok(())
}

Implementations§

Source§

impl<'a> RsMerger<'a>

Source

pub fn new() -> Self

Creates a new RsMerger builder.

§Examples
use admerge::{RsMerger, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    let c1 = Cursor::new("foo ");
    let c2 = Cursor::new("bar ");
    let c3 = Cursor::new("baz ");
    let mut buf = Vec::new();

    let merger = RsMerger::new();
    merger.merge_sources_into(vec![c1, c2, c3], &mut buf)?;

    Ok(())
}
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 source.

§Examples

Keeps the first given source untouched, but skips first line from the rest sources.

use admerge::{RsMerger, Skip, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new("header\n record 1\n");
    let mut c2 = Cursor::new("header\n record 2\n");
    let mut c3 = Cursor::new("header\n record 3\n");
    let mut buf = Vec::new();

    // Configures merger.
    let mut merger = RsMerger::new();
    merger.skip_head(Skip::LinesOnce(1));

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        "header\n record 1\n record 2\n record 3\n"
    );

    Ok(())
}
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 source.

§Examples

Keeps the last given source untouched, but skips last line from the rest sources.

use admerge::{RsMerger, Skip, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new(" record 1\n date created: 2000/01/01\n");
    let mut c2 = Cursor::new(" record 2\n date created: 2000/01/01\n");
    let mut c3 = Cursor::new(" record 3\n date created: 2000/01/01\n");
    let mut buf = Vec::new();

    // Configures merger.
    let mut merger = RsMerger::new();
    merger.skip_tail(Skip::LinesOnce(1));

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        " record 1\n record 2\n record 3\n date created: 2000/01/01\n"
    );

    Ok(())
}
Source

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

Configures this merger to fill some padding before, between or after the given sources.

§Examples
use admerge::{RsMerger, Pad, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new(" record 1 ");
    let mut c2 = Cursor::new(" record 2 ");
    let mut c3 = Cursor::new(" record 3 ");
    let mut buf = Vec::new();

    // Configures merger.
    let mut merger = RsMerger::new();
    merger.pad_with(Pad::Custom(
        Some(b"header"),
        Some(b"padding"),
        Some(b"date created: 2000/01/01")
    ));

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        "header record 1 padding record 2 padding record 3 date created: 2000/01/01"
    );

    Ok(())
}
Source

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

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

Noting that ending newlines are given after sources, not after paddings.

§Examples
use admerge::{RsMerger, Newline, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new(" line 1 ");
    let mut c2 = Cursor::new(" line 2 ");
    let mut c3 = Cursor::new(" line 3 ");
    let mut buf = Vec::new();

    // Configures merger.
    let mut merger = RsMerger::new();
    merger.force_ending_newline(Newline::Crlf);

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        " line 1 \r\n line 2 \r\n line 3 \r\n"
    );

    Ok(())
}
Source

pub fn merge_sources_into<RS, W>( &self, sources: Vec<RS>, writer: &mut W, ) -> Result<()>
where RS: Read + Seek, W: Write,

Merges the given sources into the given writer according to the given configurations.

§Errors

Returns an error variant of ErrorKind::NothingPassed if the given source 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.

§Examples
use admerge::{RsMerger, Skip, Pad, Newline, Result};
use std::io::Cursor;

fn main() -> Result<()> {
    // Cursor implements `Read` and `Seek`.
    let mut c1 = Cursor::new(" record 1\n date created: 2000/01/01\n");
    let mut c2 = Cursor::new(" record 2\n date created: 2000/01/01\n");
    let mut c3 = Cursor::new(" record 3\n date created: 2000/01/01\n");
    let mut buf = Vec::new();

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

    // Merges sources into one.
    merger.merge_sources_into(vec![&mut c1, &mut c2, &mut c3], &mut buf)?;
    assert_eq!(
        std::str::from_utf8(&buf).unwrap(),
        "header\n record 1\n record 2\n record 3\n date created: 2000/01/01\n"
    );

    Ok(())
}

Trait Implementations§

Source§

impl<'a> Clone for RsMerger<'a>

Source§

fn clone(&self) -> RsMerger<'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 RsMerger<'a>

Source§

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

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

impl<'a> Default for RsMerger<'a>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'a> Freeze for RsMerger<'a>

§

impl<'a> RefUnwindSafe for RsMerger<'a>

§

impl<'a> Send for RsMerger<'a>

§

impl<'a> Sync for RsMerger<'a>

§

impl<'a> Unpin for RsMerger<'a>

§

impl<'a> UnwindSafe for RsMerger<'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.