Struct pica_record::ByteRecord
source · pub struct ByteRecord<'a> { /* private fields */ }
Expand description
A record, that may contain invalid UTF-8 data.
Implementations§
source§impl<'a> ByteRecord<'a>
impl<'a> ByteRecord<'a>
sourcepub fn from_bytes<B: AsRef<[u8]>>(bytes: &'a B) -> Result<Self, ParsePicaError>
pub fn from_bytes<B: AsRef<[u8]>>(bytes: &'a B) -> Result<Self, ParsePicaError>
Creates a new ByteRecord from a byte slice.
§Errors
If an invalid record is given, an error is returned.
§Example
use pica_record::ByteRecord;
let record = ByteRecord::from_bytes(b"003@ \x1f0abc\x1e\n")?;
assert_eq!(record.fields().len(), 1);
sourcepub fn write_to(&self, out: &mut impl Write) -> Result<()>
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"
);
sourcepub fn retain<F: FnMut(&FieldRef<'_>) -> bool>(&mut self, f: F)
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);
sourcepub fn sha256(&self) -> Vec<u8> ⓘ
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>>§
sourcepub fn fields(&self) -> &[FieldRef<'a>]
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]);
sourcepub fn retain<F: FnMut(&FieldRef<'_>) -> bool>(&mut self, f: F)
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);
sourcepub fn validate(&self) -> Result<(), Utf8Error>
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());
sourcepub fn write_to(&self, out: &mut impl Write) -> Result<()>
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 ByteRecord<'a>
impl<'a> Debug for ByteRecord<'a>
source§impl<'a> Deref for ByteRecord<'a>
impl<'a> Deref for ByteRecord<'a>
source§impl<'a> DerefMut for ByteRecord<'a>
impl<'a> DerefMut for ByteRecord<'a>
source§impl<'a> From<RecordRef<'a>> for ByteRecord<'a>
impl<'a> From<RecordRef<'a>> for ByteRecord<'a>
source§fn from(record: RecordRef<'a>) -> Self
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<'a> Hash for ByteRecord<'a>
impl<'a> Hash for ByteRecord<'a>
source§impl PartialEq<ByteRecord<'_>> for ByteRecord<'_>
impl PartialEq<ByteRecord<'_>> for ByteRecord<'_>
source§fn eq(&self, other: &ByteRecord<'_>) -> bool
fn eq(&self, other: &ByteRecord<'_>) -> bool
Compare two ByteRecords.
§Note
It’s important not to derive PartialEq for a ByteRecord, because a record might have cached the raw data. There are two cases to consider: If both records have raw data it is sufficient to compare these byte slices. Otherwise, bot records must be compared field by field.
§Example
use pica_record::primitives::RecordRef;
use pica_record::ByteRecord;
let record1 = ByteRecord::from_bytes(b"012A \x1fa123\x1e\n")?;
let record2 = ByteRecord::from(RecordRef::new(vec![(
"012A",
None,
vec![('a', "123")],
)])?);
assert_eq!(record1, record2);
source§impl<'a> TryFrom<ByteRecord<'a>> for StringRecord<'a>
impl<'a> TryFrom<ByteRecord<'a>> for StringRecord<'a>
source§fn try_from(record: ByteRecord<'a>) -> Result<Self, Self::Error>
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);