Struct CsvByteDiffLocal

Source
pub struct CsvByteDiffLocal<T: CsvHashTaskSpawnerLocal> { /* private fields */ }
Expand description

Compare two CSVs eagerly with each other (for the lazy/iterator-based variant, see CsvByteDiff).

Use this instead of CsvByteDiff, when your CSV data is a local reference and you don’t own it.

This requires your CSV data to be Seekable. If it isn’t Seekable out of the box, it might still auto implement the trait CsvReadSeek (see Csv::with_reader_seek). If your CSV data can’t be made Seekable, consider using CsvByteDiff instead.

CsvByteDiffLocal uses scoped threads internally for comparison. By default, it uses rayon’s scoped threads within a rayon thread pool. See also rayon_thread_pool on CsvByteDiffLocalBuilder for using an existing rayon thread-pool when creating CsvByteDiffLocal.

§Example: create CsvByteDiffLocal with default values and compare two CSVs byte-wise eagerly

use csv_diff::{csv_diff::CsvByteDiffLocal, csv::Csv};
use csv_diff::diff_row::{ByteRecordLineInfo, DiffByteRecord};
use std::collections::HashSet;
use std::iter::FromIterator;
// some csv data with a header, where the first column is a unique id
let csv_data_left = "id,name,kind\n\
                     1,lemon,fruit\n\
                     2,strawberry,fruit";
let csv_data_right = "id,name,kind\n\
                      1,lemon,fruit\n\
                      2,strawberry,nut";

let csv_byte_diff = CsvByteDiffLocal::new()?;

let mut diff_byte_records = csv_byte_diff.diff(
    Csv::with_reader_seek(csv_data_left.as_bytes()),
    Csv::with_reader_seek(csv_data_right.as_bytes()),
)?;

let diff_byte_rows = diff_byte_records.as_slice();

assert_eq!(
    diff_byte_rows,
    &[DiffByteRecord::Modify {
        delete: ByteRecordLineInfo::new(
            csv::ByteRecord::from(vec!["2", "strawberry", "fruit"]),
            3
        ),
        add: ByteRecordLineInfo::new(csv::ByteRecord::from(vec!["2", "strawberry", "nut"]), 3),
        field_indices: vec![2]
    }]
);
Ok(())

Implementations§

Source§

impl CsvByteDiffLocal<CsvHashTaskSpawnerLocalRayon<'_>>

Source

pub fn new() -> Result<Self, CsvDiffNewError>

Constructs a new CsvByteDiffLocal<CsvHashTaskSpawnerRayon<'_>> with a default configuration. The values in the first column of each CSV will be declared as the primary key, in order to match the CSV records against each other. During the construction, a new rayon thread-pool is created, which will be used later during the comparison of CSVs.

If you need to have more control over the configuration of CsvByteDiffLocal<CsvHashTaskSpawnerRayon<'_>>, consider using a CsvByteDiffLocalBuilder instead.

Source§

impl<T> CsvByteDiffLocal<T>

Source

pub fn diff<R: Read + Seek + Send>( &self, csv_left: Csv<R>, csv_right: Csv<R>, ) -> Result<DiffByteRecords>

Compares csv_left with csv_right and returns a csv::Result with the CSV byte records that are different.

Csv<R> is a wrapper around a CSV reader with some configuration options.

§Example
use csv_diff::{csv_diff::CsvByteDiffLocal, csv::Csv};
use csv_diff::diff_row::{ByteRecordLineInfo, DiffByteRecord};
use std::collections::HashSet;
use std::iter::FromIterator;
// some csv data with a header, where the first column is a unique id
let csv_data_left = "id,name,kind\n\
                     1,lemon,fruit\n\
                     2,strawberry,fruit";
let csv_data_right = "id,name,kind\n\
                      1,lemon,fruit\n\
                      2,strawberry,nut";

let csv_byte_diff = CsvByteDiffLocal::new()?;

let mut diff_byte_records = csv_byte_diff.diff(
    Csv::with_reader_seek(csv_data_left.as_bytes()),
    Csv::with_reader_seek(csv_data_right.as_bytes()),
)?;

diff_byte_records.sort_by_line();

let diff_byte_rows = diff_byte_records.as_slice();

assert_eq!(
    diff_byte_rows,
    &[DiffByteRecord::Modify {
        delete: ByteRecordLineInfo::new(
            csv::ByteRecord::from(vec!["2", "strawberry", "fruit"]),
            3
        ),
        add: ByteRecordLineInfo::new(csv::ByteRecord::from(vec!["2", "strawberry", "nut"]), 3),
        field_indices: vec![2]
    }]
);
Ok(())

Trait Implementations§

Source§

impl<T: Debug + CsvHashTaskSpawnerLocal> Debug for CsvByteDiffLocal<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for CsvByteDiffLocal<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for CsvByteDiffLocal<T>
where T: RefUnwindSafe,

§

impl<T> Send for CsvByteDiffLocal<T>
where T: Send,

§

impl<T> Sync for CsvByteDiffLocal<T>
where T: Sync,

§

impl<T> Unpin for CsvByteDiffLocal<T>
where T: Unpin,

§

impl<T> UnwindSafe for CsvByteDiffLocal<T>
where T: UnwindSafe,

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T

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.