Struct pica_record::StringRecord

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

A record, that guarantees valid UTF-8 data.

Methods from Deref<Target = ByteRecord<'a>>§

source

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

Write the ByteRecord into the given writer.

§Example
use std::io::Cursor;

use pica_record::ByteRecord;

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

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

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

Retains only the fields specified by the predicate.

§Example
use pica_record::ByteRecord;

let mut record =
    ByteRecord::from_bytes(b"003@ \x1f0a\x1e002@ \x1f0Olfo\x1e\n")?;
record.retain(|field| field.tag() == "003@");
assert_eq!(record.fields().len(), 1);
source

pub fn sha256(&self) -> Vec<u8>

Returns the SHA-256 hash of the record.

§Example
use std::fmt::Write;

use pica_record::ByteRecord;

let mut record = ByteRecord::from_bytes(b"012A \x1fa123\x1e\n")?;

let hash =
    record.sha256().iter().fold(String::new(), |mut out, b| {
        let _ = write!(out, "{b:02x}");
        out
    });

assert!(hash.starts_with("95e266"));

Methods from Deref<Target = RecordRef<'a>>§

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

source§

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

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

impl<'a> Deref for StringRecord<'a>

source§

type Target = ByteRecord<'a>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<'a> DerefMut for StringRecord<'a>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<'a> TryFrom<ByteRecord<'a>> for StringRecord<'a>

source§

fn try_from(record: ByteRecord<'a>) -> Result<Self, Self::Error>

Creates a StringRecord from a ByteRecord.

§Errors

If the underlying ByteRecord contains invalid UTF-8 sequences, an Utf8Error is returned.

§Example
use pica_record::{ByteRecord, StringRecord};

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

type Error = Utf8Error

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<'a> Freeze for StringRecord<'a>

§

impl<'a> RefUnwindSafe for StringRecord<'a>

§

impl<'a> Send for StringRecord<'a>

§

impl<'a> Sync for StringRecord<'a>

§

impl<'a> Unpin for StringRecord<'a>

§

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