Struct pica_record::primitives::RecordRef

source ·
pub struct RecordRef<'a>(/* private fields */);
Expand description

An immutable PICA+ record.

Implementations§

source§

impl<'a> RecordRef<'a>

source

pub fn new<T>( fields: Vec<(&'a str, Option<&'a str>, T)>, ) -> Result<Self, ParsePicaError>
where T: IntoIterator<Item = (char, &'a str)>,

Creates a new RecordRef.

§Errors

This function fails if either the tag, occcurrence or any subfield is nvalid.

§Example
use pica_record::primitives::RecordRef;

let record = RecordRef::new(vec![
    ("003@", None, vec![('0', "123456789X")]),
    ("002@", None, vec![('0', "Tp1")]),
])?;

assert_eq!(record.fields().len(), 2);
source

pub fn from_bytes<B>(record: &'a B) -> Result<Self, ParsePicaError>
where B: AsRef<[u8]> + ?Sized,

Creates a new RecordRef from a byte slice.

§Example
use pica_record::primitives::RecordRef;

let record = RecordRef::from_bytes(b"012A \x1f0abc\x1e\n")?;
assert_eq!(record.fields().len(), 1);
source

pub fn fields(&self) -> &[FieldRef<'a>]

Returns the fields of the record.

§Example
use pica_record::primitives::{FieldRef, RecordRef};

let record = RecordRef::from_bytes(b"012A \x1f0abc\x1e\n")?;
let field = FieldRef::from_bytes(b"012A \x1f0abc\x1e")?;
assert_eq!(record.fields(), [field]);
source

pub fn is_empty(&self) -> bool

Returns true if the RecordRef contains no fields, otherwise false.

§Example
use pica_record::primitives::RecordRef;

let record = RecordRef::from_bytes(b"002@ \x1f0Oaf\x1e\n")?;
assert!(!record.is_empty());
source

pub fn retain<F: FnMut(&FieldRef<'_>) -> bool>(&mut self, f: F)

Retains only the FieldRefs specified by the predicate.

§Example
use pica_record::primitives::RecordRef;

let mut record = RecordRef::new(vec![
    ("003@", None, vec![('0', "123456789X")]),
    ("002@", None, vec![('0', "Oaf")]),
])?;

assert_eq!(record.fields().len(), 2);

record.retain(|field| field.tag() == "003@");
assert_eq!(record.fields().len(), 1);
source

pub fn validate(&self) -> Result<(), Utf8Error>

Returns an std::str::Utf8Error if the record contains invalid UTF-8 data, otherwise the unit.

§Example
use pica_record::primitives::RecordRef;

let record = RecordRef::from_bytes(b"003@ \x1f0a\x1e\n")?;
assert!(record.validate().is_ok());
source

pub fn write_to(&self, out: &mut impl Write) -> Result<()>

Write the RecordRef into the given writer.

§Example
use std::io::Cursor;

use pica_record::primitives::RecordRef;

let mut writer = Cursor::new(Vec::<u8>::new());
let record = RecordRef::from_bytes(b"003@ \x1f0a\x1e\n")?;
record.write_to(&mut writer);

assert_eq!(
    String::from_utf8(writer.into_inner())?,
    "003@ \x1f0a\x1e\n"
);

Trait Implementations§

source§

impl<'a> Clone for RecordRef<'a>

source§

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

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<'a> Debug for RecordRef<'a>

source§

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

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

impl From<RecordRef<'_>> for Record

source§

fn from(record: RecordRef<'_>) -> Self

Converts a RecordRef into a Record.

§Example
use pica_record::primitives::{Record, RecordRef};

let record_ref = RecordRef::from_bytes(b"003@ \x1f0a\x1e\n")?;
let record = Record::from(record_ref);
assert_eq!(RecordRef::from_bytes(b"003@ \x1f0a\x1e\n")?, record);
source§

impl<'a> From<RecordRef<'a>> for ByteRecord<'a>

source§

fn from(record: RecordRef<'a>) -> Self

Creates a ByteRecord from a RecordRef.

§Example
use pica_record::primitives::RecordRef;
use pica_record::ByteRecord;

let record1 = ByteRecord::from_bytes(b"012A \x1fa123\x1e\n")?;
let record2 = ByteRecord::from(record1);
assert_eq!(record2.fields().len(), 1);
source§

impl PartialEq<Record> for RecordRef<'_>

source§

fn eq(&self, other: &Record) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<RecordRef<'_>> for Record

source§

fn eq(&self, other: &RecordRef<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq for RecordRef<'a>

source§

fn eq(&self, other: &RecordRef<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> StructuralPartialEq for RecordRef<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for RecordRef<'a>

§

impl<'a> RefUnwindSafe for RecordRef<'a>

§

impl<'a> Send for RecordRef<'a>

§

impl<'a> Sync for RecordRef<'a>

§

impl<'a> Unpin for RecordRef<'a>

§

impl<'a> UnwindSafe for RecordRef<'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, dst: *mut T)

🔬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.