Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove some calls to unwrap #113

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ pub enum Error {
EntryInTrunNotFound(u32, BoxType, u32),
#[error("{0} version {1} is not supported")]
UnsupportedBoxVersion(BoxType, u8),
#[error("{0}")]
GenericError(String),
}
8 changes: 0 additions & 8 deletions src/mp4box/avc1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ impl Mp4Box for Avc1Box {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!(
"data_reference_index={} width={} height={} frame_count={}",
Expand Down Expand Up @@ -206,10 +202,6 @@ impl Mp4Box for AvcCBox {
size
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("avc_profile_indication={}", self.avc_profile_indication);
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/co64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ impl Mp4Box for Co64Box {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("entries_count={}", self.entries.len());
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/ctts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ impl Mp4Box for CttsBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("entries_count={}", self.entries.len());
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl Mp4Box for DataBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("type={:?} len={}", self.data_type, self.data.len());
Ok(s)
Expand Down
22 changes: 2 additions & 20 deletions src/mp4box/dinf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ impl Mp4Box for DinfBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = String::new();
Ok(s)
Expand Down Expand Up @@ -68,15 +64,9 @@ impl<R: Read + Seek> ReadBox<&mut R> for DinfBox {
current = reader.stream_position()?;
}

if dref.is_none() {
return Err(Error::BoxNotFound(BoxType::DrefBox));
}

let dref = dref.ok_or(Error::BoxNotFound(BoxType::DrefBox))?;
skip_bytes_to(reader, start + size)?;

Ok(DinfBox {
dref: dref.unwrap(),
})
Ok(DinfBox { dref })
}
}

Expand Down Expand Up @@ -131,10 +121,6 @@ impl Mp4Box for DrefBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = String::new();
Ok(s)
Expand Down Expand Up @@ -248,10 +234,6 @@ impl Mp4Box for UrlBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("location={}", self.location);
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/edts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl Mp4Box for EdtsBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = String::new();
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/elst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ impl Mp4Box for ElstBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("elst_entries={}", self.entries.len());
Ok(s)
Expand Down
12 changes: 6 additions & 6 deletions src/mp4box/emsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ impl Mp4Box for EmsgBox {
+ self.message_data.len() as u64
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("id={} value={}", self.id, self.value);
Ok(s)
Expand Down Expand Up @@ -131,13 +127,17 @@ impl<W: Write> WriteBox<&mut W> for EmsgBox {
write_null_terminated_str(writer, &self.scheme_id_uri)?;
write_null_terminated_str(writer, &self.value)?;
writer.write_u32::<BigEndian>(self.timescale)?;
writer.write_u32::<BigEndian>(self.presentation_time_delta.unwrap())?;
writer.write_u32::<BigEndian>(self.presentation_time_delta.ok_or(
crate::error::Error::InvalidData("presentation_time_delta not found"),
)?)?;
writer.write_u32::<BigEndian>(self.event_duration)?;
writer.write_u32::<BigEndian>(self.id)?;
}
1 => {
writer.write_u32::<BigEndian>(self.timescale)?;
writer.write_u64::<BigEndian>(self.presentation_time.unwrap())?;
writer.write_u64::<BigEndian>(self.presentation_time.ok_or(
crate::error::Error::InvalidData("presentation_time not found"),
)?)?;
writer.write_u32::<BigEndian>(self.event_duration)?;
writer.write_u32::<BigEndian>(self.id)?;
write_null_terminated_str(writer, &self.scheme_id_uri)?;
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/ftyp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ impl Mp4Box for FtypBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let mut compatible_brands = Vec::new();
for brand in self.compatible_brands.iter() {
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/hdlr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ impl Mp4Box for HdlrBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("handler_type={} name={}", self.handler_type, self.name);
Ok(s)
Expand Down
8 changes: 0 additions & 8 deletions src/mp4box/hev1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ impl Mp4Box for Hev1Box {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!(
"data_reference_index={} width={} height={} frame_count={}",
Expand Down Expand Up @@ -204,10 +200,6 @@ impl Mp4Box for HvcCBox {
.sum::<u64>()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
Ok(format!("configuration_version={} general_profile_space={} general_tier_flag={} general_profile_idc={} general_profile_compatibility_flags={} general_constraint_indicator_flag={} general_level_idc={} min_spatial_segmentation_idc={} parallelism_type={} chroma_format_idc={} bit_depth_luma_minus8={} bit_depth_chroma_minus8={} avg_frame_rate={} constant_frame_rate={} num_temporal_layers={} temporal_id_nested={} length_size_minus_one={}",
self.configuration_version,
Expand Down
14 changes: 2 additions & 12 deletions src/mp4box/ilst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl Mp4Box for IlstBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("item_count={}", self.items.len());
Ok(s)
Expand Down Expand Up @@ -153,15 +149,9 @@ impl<R: Read + Seek> ReadBox<&mut R> for IlstItemBox {
current = reader.stream_position()?;
}

if data.is_none() {
return Err(Error::BoxNotFound(BoxType::DataBox));
}

let data = data.ok_or(Error::BoxNotFound(BoxType::DataBox))?;
skip_bytes_to(reader, start + size)?;

Ok(IlstItemBox {
data: data.unwrap(),
})
Ok(IlstItemBox { data })
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/mdhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ impl Mp4Box for MdhdBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!(
"creation_time={} timescale={} duration={} language={}",
Expand Down
22 changes: 4 additions & 18 deletions src/mp4box/mdia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ impl Mp4Box for MdiaBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = String::new();
Ok(s)
Expand Down Expand Up @@ -79,23 +75,13 @@ impl<R: Read + Seek> ReadBox<&mut R> for MdiaBox {
current = reader.stream_position()?;
}

if mdhd.is_none() {
return Err(Error::BoxNotFound(BoxType::MdhdBox));
}
if hdlr.is_none() {
return Err(Error::BoxNotFound(BoxType::HdlrBox));
}
if minf.is_none() {
return Err(Error::BoxNotFound(BoxType::MinfBox));
}
let mdhd = mdhd.ok_or(Error::BoxNotFound(BoxType::MdhdBox))?;
let hdlr = hdlr.ok_or(Error::BoxNotFound(BoxType::HdlrBox))?;
let minf = minf.ok_or(Error::BoxNotFound(BoxType::MinfBox))?;

skip_bytes_to(reader, start + size)?;

Ok(MdiaBox {
mdhd: mdhd.unwrap(),
hdlr: hdlr.unwrap(),
minf: minf.unwrap(),
})
Ok(MdiaBox { mdhd, hdlr, minf })
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/mehd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ impl Mp4Box for MehdBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("fragment_duration={}", self.fragment_duration);
Ok(s)
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ impl Mp4Box for MetaBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = match self {
Self::Mdir { .. } => "hdlr=ilst".to_string(),
Expand Down
4 changes: 0 additions & 4 deletions src/mp4box/mfhd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ impl Mp4Box for MfhdBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = format!("sequence_number={}", self.sequence_number);
Ok(s)
Expand Down
16 changes: 4 additions & 12 deletions src/mp4box/minf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ impl Mp4Box for MinfBox {
self.get_size()
}

fn to_json(&self) -> Result<String> {
Ok(serde_json::to_string(&self).unwrap())
}

fn summary(&self) -> Result<String> {
let s = String::new();
Ok(s)
Expand Down Expand Up @@ -97,20 +93,16 @@ impl<R: Read + Seek> ReadBox<&mut R> for MinfBox {
current = reader.stream_position()?;
}

if dinf.is_none() {
return Err(Error::BoxNotFound(BoxType::DinfBox));
}
if stbl.is_none() {
return Err(Error::BoxNotFound(BoxType::StblBox));
}
let dinf = dinf.ok_or(Error::BoxNotFound(BoxType::DinfBox))?;
let stbl = stbl.ok_or(Error::BoxNotFound(BoxType::StblBox))?;

skip_bytes_to(reader, start + size)?;

Ok(MinfBox {
vmhd,
smhd,
dinf: dinf.unwrap(),
stbl: stbl.unwrap(),
dinf,
stbl,
})
}
}
Expand Down
16 changes: 13 additions & 3 deletions src/mp4box/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
//!

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use serde::Serialize;
use std::convert::TryInto;
use std::io::{Read, Seek, SeekFrom, Write};

Expand Down Expand Up @@ -244,7 +245,12 @@ boxtype! {
pub trait Mp4Box: Sized {
fn box_type(&self) -> BoxType;
fn box_size(&self) -> u64;
fn to_json(&self) -> Result<String>;
fn to_json(&self) -> Result<String>
where
Self: Serialize,
{
serde_json::to_string(&self).map_err(|e| crate::error::Error::IoError(e.into()))
}
fn summary(&self) -> Result<String>;
}

Expand Down Expand Up @@ -274,11 +280,15 @@ impl BoxHeader {
reader.read_exact(&mut buf)?;

// Get size.
let s = buf[0..4].try_into().unwrap();
let s = buf[0..4]
.try_into()
.map_err(|_e| crate::error::Error::InvalidData("could not get slice"))?;
let size = u32::from_be_bytes(s);

// Get box type string.
let t = buf[4..8].try_into().unwrap();
let t = buf[4..8]
.try_into()
.map_err(|_e| crate::error::Error::InvalidData("could not get slice"))?;
let typ = u32::from_be_bytes(t);

// Get largesize if size is 1
Expand Down
Loading