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>>§
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 StringRecord<'a>
impl<'a> Debug for StringRecord<'a>
source§impl<'a> Deref for StringRecord<'a>
impl<'a> Deref for StringRecord<'a>
source§impl<'a> DerefMut for StringRecord<'a>
impl<'a> DerefMut for StringRecord<'a>
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);
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more