Struct pica_record::primitives::FieldRef
source · pub struct FieldRef<'a> { /* private fields */ }
Expand description
An immutable field.
Implementations§
source§impl<'a> FieldRef<'a>
impl<'a> FieldRef<'a>
sourcepub fn new<T>(
tag: &'a str,
occ: Option<&'a str>,
subfields: T,
) -> Result<Self, ParsePicaError>
pub fn new<T>( tag: &'a str, occ: Option<&'a str>, subfields: T, ) -> Result<Self, ParsePicaError>
sourcepub fn from_bytes<B>(field: &'a B) -> Result<Self, ParsePicaError>
pub fn from_bytes<B>(field: &'a B) -> Result<Self, ParsePicaError>
sourcepub fn occurrence(&self) -> Option<&OccurrenceRef<'a>>
pub fn occurrence(&self) -> Option<&OccurrenceRef<'a>>
Returns a reference to the OccurrenceRef of the field.
§Example
use pica_record::primitives::{FieldRef, OccurrenceRef};
let field =
FieldRef::new("003@", Some("01"), vec![('0', "123456789X")])?;
assert_eq!(field.occurrence(), Some(&OccurrenceRef::new("01")?));
sourcepub fn subfields(&self) -> &[SubfieldRef<'a>]
pub fn subfields(&self) -> &[SubfieldRef<'a>]
Returns a reference to the SubfieldRefs of the field.
§Example
use pica_record::primitives::FieldRef;
let field = FieldRef::new("003@", None, vec![('0', "123456789X")])?;
let subfields = field.subfields();
assert_eq!(subfields.len(), 1);
sourcepub fn contains(&self, code: char) -> bool
pub fn contains(&self, code: char) -> bool
Checks whether a FieldRef contains a SubfieldRef with the given code or not.
§Example
use pica_record::primitives::FieldRef;
let field = FieldRef::new("003@", None, vec![('0', "123456789X")])?;
assert!(!field.contains('a'));
assert!(field.contains('0'));
sourcepub fn find<F>(&self, predicate: F) -> Option<&SubfieldRef<'_>>
pub fn find<F>(&self, predicate: F) -> Option<&SubfieldRef<'_>>
Searches for the first SubfieldRef that satisfies the given predicate.
§Example
use pica_record::primitives::{FieldRef, SubfieldRef};
let field =
FieldRef::new("012A", None, vec![('a', "b"), ('c', "d")])?;
assert!(field.find(|subfield| subfield.code() == 'b').is_none());
let subfield =
field.find(|subfield| subfield.code() == 'a').unwrap();
assert_eq!(subfield, &SubfieldRef::new('a', "b")?);
sourcepub fn level(&self) -> Level
pub fn level(&self) -> Level
Returns the level of the field.
§Example
use pica_record::primitives::{FieldRef, Level};
let field = FieldRef::from_bytes(b"012A/01 \x1fab\x1fcd\x1e")?;
assert_eq!(field.level(), Level::Main);
sourcepub fn validate(&self) -> Result<(), Utf8Error>
pub fn validate(&self) -> Result<(), Utf8Error>
Returns an std::str::Utf8Error
if the field
contains invalid UTF-8 data, otherwise the unit.
§Example
use pica_record::primitives::FieldRef;
let field = FieldRef::from_bytes(b"003@ \x1f0123\x1e")?;
assert!(field.validate().is_ok());
let field = FieldRef::from_bytes(b"003@ \x1f0\x00\x9F\x1e")?;
assert!(field.validate().is_err());
sourcepub fn write_to(&self, out: &mut impl Write) -> Result<()>
pub fn write_to(&self, out: &mut impl Write) -> Result<()>
Write the FieldRef into the given writer.
§Example
use std::io::Cursor;
use pica_record::primitives::FieldRef;
let mut writer = Cursor::new(Vec::<u8>::new());
let field = FieldRef::from_bytes(b"012A/01 \x1fab\x1fcd\x1e")?;
field.write_to(&mut writer);
assert_eq!(
String::from_utf8(writer.into_inner())?,
"012A/01 \x1fab\x1fcd\x1e"
);
Trait Implementations§
source§impl<'a> IntoIterator for &'a FieldRef<'a>
impl<'a> IntoIterator for &'a FieldRef<'a>
source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator from a single field. The iterator just returns the field once.
§Example
use pica_record::primitives::FieldRef;
let field = FieldRef::new("003@", None, vec![('0', "abc")])?;
let mut iter = field.into_iter();
assert_eq!(iter.next(), Some(&field));
assert_eq!(iter.next(), None);
impl<'a> Eq for FieldRef<'a>
impl<'a> StructuralPartialEq for FieldRef<'a>
Auto Trait Implementations§
impl<'a> Freeze for FieldRef<'a>
impl<'a> RefUnwindSafe for FieldRef<'a>
impl<'a> Send for FieldRef<'a>
impl<'a> Sync for FieldRef<'a>
impl<'a> Unpin for FieldRef<'a>
impl<'a> UnwindSafe for FieldRef<'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
)