Struct MediaParser

Source
pub struct MediaParser { /* private fields */ }
Expand description

A MediaParser/AsyncMediaParser can parse media info from a MediaSource.

MediaParser/AsyncMediaParser manages inner parse buffers that can be shared between multiple parsing tasks, thus avoiding frequent memory allocations.

Therefore:

  • Try to reuse a MediaParser/AsyncMediaParser instead of creating a new one every time you need it.

  • MediaSource should be created directly from Read, not from BufRead.

§Example

use nom_exif::*;
use chrono::DateTime;

let mut parser = MediaParser::new();

// ------------------- Parse Exif Info
let ms = MediaSource::file_path("./testdata/exif.heic").unwrap();
assert!(ms.has_exif());
let mut iter: ExifIter = parser.parse(ms).unwrap();

let entry = iter.next().unwrap();
assert_eq!(entry.tag().unwrap(), ExifTag::Make);
assert_eq!(entry.get_value().unwrap().as_str().unwrap(), "Apple");

// Convert `ExifIter` into an `Exif`. Clone it before converting, so that
// we can start the iteration from the beginning.
let exif: Exif = iter.clone().into();
assert_eq!(exif.get(ExifTag::Make).unwrap().as_str().unwrap(), "Apple");

// ------------------- Parse Track Info
let ms = MediaSource::file_path("./testdata/meta.mov").unwrap();
assert!(ms.has_track());
let info: TrackInfo = parser.parse(ms).unwrap();

assert_eq!(info.get(TrackInfoTag::Make), Some(&"Apple".into()));
assert_eq!(info.get(TrackInfoTag::Model), Some(&"iPhone X".into()));
assert_eq!(info.get(TrackInfoTag::GpsIso6709), Some(&"+27.1281+100.2508+000.000/".into()));
assert_eq!(info.get_gps_info().unwrap().latitude_ref, 'N');
assert_eq!(
    info.get_gps_info().unwrap().latitude,
    [(27, 1), (7, 1), (68, 100)].into(),
);

Implementations§

Source§

impl MediaParser

Source

pub fn new() -> Self

Source

pub fn parse<R: Read, S, O: ParseOutput<R, S>>( &mut self, ms: MediaSource<R, S>, ) -> Result<O>

MediaParser/AsyncMediaParser comes with its own buffer management, so that buffers can be reused during multiple parsing processes to avoid frequent memory allocations. Therefore, try to reuse a MediaParser instead of creating a new one every time you need it.

Note:

  • For ExifIter as parse output, Please avoid holding the ExifIter object all the time and drop it immediately after use. Otherwise, the parsing buffer referenced by the ExifIter object will not be reused by MediaParser, resulting in repeated memory allocation in the subsequent parsing process.

    If you really need to retain some data, please take out the required Entry values ​​and save them, or convert the ExifIter into an crate::Exif object to retain all Entry values.

  • For TrackInfo as parse output, you don’t need to worry about this, because TrackInfo dosn’t reference the parsing buffer.

Trait Implementations§

Source§

impl Debug for MediaParser

Source§

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

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

impl Default for MediaParser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more