util/error: refactor error handling

This commit is contained in:
meh
2015-05-24 18:51:34 +02:00
parent 2716893612
commit 5b80b10949
13 changed files with 205 additions and 101 deletions

View File

@ -30,7 +30,7 @@ impl Context {
unsafe {
match avcodec_open2(self.ptr, codec.ptr, ptr::null_mut()) {
0 => Ok(Opened(self)),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
@ -39,7 +39,7 @@ impl Context {
unsafe {
match avcodec_open2(self.ptr, codec.ptr, &mut options.ptr) {
0 => Ok(Opened(self)),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
@ -49,7 +49,7 @@ impl Context {
self.clone().open(codec).and_then(|c| c.decoder())
}
else {
Err(Error::from(AVERROR_DECODER_NOT_FOUND))
Err(Error::DecoderNotFound)
}
}
@ -58,7 +58,7 @@ impl Context {
self.clone().open(codec).and_then(|c| c.encoder())
}
else {
Err(Error::from(AVERROR_ENCODER_NOT_FOUND))
Err(Error::EncoderNotFound)
}
}
@ -169,7 +169,7 @@ impl Opened {
Ok(Decoder(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
@ -184,7 +184,7 @@ impl Opened {
Ok(Encoder(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
}

View File

@ -16,7 +16,7 @@ impl Audio {
let mut got: c_int = 0;
match avcodec_decode_audio4(self.ptr, out.ptr, &mut got, &packet.val) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(got != 0)
}
}

View File

@ -34,7 +34,7 @@ impl Decoder {
Ok(Video(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
@ -43,7 +43,7 @@ impl Decoder {
Ok(Audio(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
@ -52,7 +52,7 @@ impl Decoder {
Ok(Subtitle(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}

View File

@ -14,7 +14,7 @@ impl Subtitle {
let mut got: c_int = 0;
match avcodec_decode_subtitle2(self.ptr, &mut out.val, &mut got, &packet.val) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(got != 0)
}
}

View File

@ -18,7 +18,7 @@ impl Video {
let mut got: c_int = 0;
match avcodec_decode_video2(self.ptr, out.ptr, &mut got, &packet.val) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(got != 0)
}
}

View File

@ -15,7 +15,7 @@ impl Audio {
let mut got: c_int = 0;
match avcodec_encode_audio2(self.ptr, &mut out.val, frame.ptr, &mut got) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(got != 0)
}
}

View File

@ -38,7 +38,7 @@ impl Encoder {
Ok(Video(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
@ -47,7 +47,7 @@ impl Encoder {
Ok(Audio(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}
@ -56,7 +56,7 @@ impl Encoder {
Ok(Subtitle(self))
}
else {
Err(Error::from(AVERROR_INVALIDDATA))
Err(Error::InvalidData)
}
}

View File

@ -12,7 +12,7 @@ impl Subtitle {
pub fn encode(&self, subtitle: &::Subtitle, out: &mut [u8]) -> Result<bool, Error> {
unsafe {
match avcodec_encode_subtitle(self.ptr, out.as_mut_ptr(), out.len() as c_int, &subtitle.val) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(true)
}
}

View File

@ -16,7 +16,7 @@ impl Video {
let mut got: c_int = 0;
match avcodec_encode_video2(self.ptr, &mut out.val, frame.ptr, &mut got) {
e if e < 0 => Err(Error::new(e)),
e if e < 0 => Err(Error::from(e)),
_ => Ok(got != 0)
}
}

View File

@ -23,7 +23,7 @@ impl<'a> Picture<'a> {
unsafe {
match avpicture_get_size(format.into(), width as c_int, height as c_int) {
v if v >= 0 => Ok(v as usize),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
@ -44,7 +44,7 @@ impl<'a> Picture<'a> {
_marker: PhantomData
}),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
@ -78,7 +78,7 @@ impl<'a> Picture<'a> {
unsafe {
match avpicture_layout(self.ptr, self.format.into(), self.width as c_int, self.height as c_int, out.as_mut_ptr(), out.len() as c_int) {
s if s >= 0 => Ok(s as usize),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
@ -87,20 +87,20 @@ impl<'a> Picture<'a> {
unsafe {
match avpicture_layout(self.ptr, format.into(), width as c_int, height as c_int, out.as_mut_ptr(), out.len() as c_int) {
s if s >= 0 => Ok(s as usize),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}
pub fn crop(&mut self, source: &Picture, top: u32, left: u32) -> Result<(), Error> {
if self.format != source.format {
return Err(Error::new(AVERROR_BUG));
return Err(Error::Bug);
}
unsafe {
match av_picture_crop(self.ptr, source.ptr, self.format.into(), top as c_int, left as c_int) {
0 => Ok(()),
e => Err(Error::new(e))
e => Err(Error::from(e))
}
}
}