calamine

Struct RangeDeserializerBuilder

Source
pub struct RangeDeserializerBuilder<'h, H> { /* private fields */ }
Expand description

Builds a Range deserializer with some configuration options.

This can be used to optionally parse the first row as a header. Once built, a RangeDeserializers cannot be changed.

Implementations§

Source§

impl RangeDeserializerBuilder<'static, &'static str>

Source

pub fn new() -> Self

Constructs a new builder for configuring Range deserialization.

Source

pub fn has_headers(&mut self, yes: bool) -> &mut Self

Decide whether to treat the first row as a special header row.

§Example
fn main() -> Result<(), Error> {
    let path = format!("{}/tests/temperature.xlsx", env!("CARGO_MANIFEST_DIR"));
    let mut workbook: Xlsx<_> = open_workbook(path)?;
    let range = workbook.worksheet_range("Sheet1")
        .ok_or(Error::Msg("Cannot find 'Sheet1'"))??;

    let mut iter = RangeDeserializerBuilder::new()
        .has_headers(false)
        .from_range(&range)?;

    if let Some(result) = iter.next() {
        let row: Vec<DataType> = result?;
        assert_eq!(row, [DataType::from("label"), DataType::from("value")]);
    } else {
        return Err(From::from("expected at least three records but got none"));
    }

    if let Some(result) = iter.next() {
        let row: Vec<DataType> = result?;
        assert_eq!(row, [DataType::from("celsius"), DataType::from(22.2222)]);
    } else {
        return Err(From::from("expected at least three records but got one"));
    }

    Ok(())
}
Source§

impl<'h, H: AsRef<str> + Clone + 'h> RangeDeserializerBuilder<'h, H>

Source

pub fn with_headers(headers: &'h [H]) -> Self

Build a RangeDeserializer from this configuration and keep only selected headers.

§Example
fn main() -> Result<(), Error> {
    let path = format!("{}/tests/temperature.xlsx", env!("CARGO_MANIFEST_DIR"));
    let mut workbook: Xlsx<_> = open_workbook(path)?;
    let range = workbook.worksheet_range("Sheet1")
        .ok_or(Error::Msg("Cannot find 'Sheet1'"))??;
    let mut iter = RangeDeserializerBuilder::with_headers(&["value", "label"]).from_range(&range)?;

    if let Some(result) = iter.next() {
        let (value, label): (f64, String) = result?;
        assert_eq!(label, "celsius");
        assert_eq!(value, 22.2222);

        Ok(())
    } else {
        return Err(From::from("expected at least one record but got none"));
    }
}
Source

pub fn from_range<'cell, T, D>( &self, range: &'cell Range<T>, ) -> Result<RangeDeserializer<'cell, T, D>, DeError>

Build a RangeDeserializer from this configuration.

§Example
fn main() -> Result<(), Error> {
    let path = format!("{}/tests/temperature.xlsx", env!("CARGO_MANIFEST_DIR"));
    let mut workbook: Xlsx<_> = open_workbook(path)?;
    let range = workbook.worksheet_range("Sheet1")
        .ok_or(Error::Msg("Cannot find 'Sheet1'"))??;
    let mut iter = RangeDeserializerBuilder::new().from_range(&range)?;

    if let Some(result) = iter.next() {
        let (label, value): (String, f64) = result?;
        assert_eq!(label, "celsius");
        assert_eq!(value, 22.2222);

        Ok(())
    } else {
        return Err(From::from("expected at least one record but got none"));
    }
}

Trait Implementations§

Source§

impl<'h, H: Clone> Clone for RangeDeserializerBuilder<'h, H>

Source§

fn clone(&self) -> RangeDeserializerBuilder<'h, H>

Returns a copy 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 Default for RangeDeserializerBuilder<'static, &'static str>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<'h, H> Freeze for RangeDeserializerBuilder<'h, H>

§

impl<'h, H> RefUnwindSafe for RangeDeserializerBuilder<'h, H>
where H: RefUnwindSafe,

§

impl<'h, H> Send for RangeDeserializerBuilder<'h, H>
where H: Sync,

§

impl<'h, H> Sync for RangeDeserializerBuilder<'h, H>
where H: Sync,

§

impl<'h, H> Unpin for RangeDeserializerBuilder<'h, H>

§

impl<'h, H> UnwindSafe for RangeDeserializerBuilder<'h, H>
where H: RefUnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> Same for T

Source§

type Output = T

Should always be Self
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.