Struct pica_record::primitives::RecordRef
source · pub struct RecordRef<'a>(/* private fields */);
Expand description
An immutable PICA+ record.
Implementations§
source§impl<'a> RecordRef<'a>
impl<'a> RecordRef<'a>
sourcepub fn new<T>(
fields: Vec<(&'a str, Option<&'a str>, T)>,
) -> Result<Self, ParsePicaError>
pub fn new<T>( fields: Vec<(&'a str, Option<&'a str>, T)>, ) -> Result<Self, ParsePicaError>
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);
sourcepub fn from_bytes<B>(record: &'a B) -> Result<Self, ParsePicaError>
pub fn from_bytes<B>(record: &'a B) -> Result<Self, ParsePicaError>
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> 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);
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> 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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)