From df52ef68d859a8d7dc91c5ab1afe916d88cca877 Mon Sep 17 00:00:00 2001 From: meh Date: Sun, 24 May 2015 19:13:20 +0200 Subject: [PATCH] util/error: make index a private function --- src/util/error.rs | 123 ++++++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/src/util/error.rs b/src/util/error.rs index cb1a76d..5aafaa2 100644 --- a/src/util/error.rs +++ b/src/util/error.rs @@ -40,40 +40,6 @@ pub enum Error { HttpServerError, } -impl Error { - pub fn index(self) -> usize { - match self { - Error::BsfNotFound => 0, - Error::Bug => 1, - Error::BufferTooSmall => 2, - Error::DecoderNotFound => 3, - Error::DemuxerNotFound => 4, - Error::EncoderNotFound => 5, - Error::Eof => 6, - Error::Exit => 7, - Error::External => 8, - Error::FilterNotFound => 9, - Error::InvalidData => 10, - Error::MuxerNotFound => 11, - Error::OptionNotFound => 12, - Error::PatchWelcome => 13, - Error::ProtocolNotFound => 14, - Error::StreamNotFound => 15, - Error::Bug2 => 16, - Error::Unknown => 17, - Error::Experimental => 18, - Error::InputChanged => 19, - Error::OutputChanged => 20, - Error::HttpBadRequest => 21, - Error::HttpUnauthorized => 22, - Error::HttpForbidden => 23, - Error::HttpNotFound => 24, - Error::HttpOther4xx => 25, - Error::HttpServerError => 26, - } - } -} - impl From for Error { fn from(value: c_int) -> Error { match value { @@ -144,7 +110,6 @@ impl Into for Error { } } - impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { f.write_str(error::Error::description(self)) @@ -160,49 +125,81 @@ impl fmt::Debug for Error { } } +fn index(error: &Error) -> usize { + match error { + &Error::BsfNotFound => 0, + &Error::Bug => 1, + &Error::BufferTooSmall => 2, + &Error::DecoderNotFound => 3, + &Error::DemuxerNotFound => 4, + &Error::EncoderNotFound => 5, + &Error::Eof => 6, + &Error::Exit => 7, + &Error::External => 8, + &Error::FilterNotFound => 9, + &Error::InvalidData => 10, + &Error::MuxerNotFound => 11, + &Error::OptionNotFound => 12, + &Error::PatchWelcome => 13, + &Error::ProtocolNotFound => 14, + &Error::StreamNotFound => 15, + &Error::Bug2 => 16, + &Error::Unknown => 17, + &Error::Experimental => 18, + &Error::InputChanged => 19, + &Error::OutputChanged => 20, + &Error::HttpBadRequest => 21, + &Error::HttpUnauthorized => 22, + &Error::HttpForbidden => 23, + &Error::HttpNotFound => 24, + &Error::HttpOther4xx => 25, + &Error::HttpServerError => 26, + } +} + // XXX: the length has to be synced with the number of errors static mut STRINGS: [[i8; AV_ERROR_MAX_STRING_SIZE as usize]; 27] = [[0i8; AV_ERROR_MAX_STRING_SIZE as usize]; 27]; pub fn register_all() { unsafe { - av_strerror(Error::Bug.into(), STRINGS[Error::Bug.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::Bug2.into(), STRINGS[Error::Bug2.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::Unknown.into(), STRINGS[Error::Unknown.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::Experimental.into(), STRINGS[Error::Experimental.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::BufferTooSmall.into(), STRINGS[Error::BufferTooSmall.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::Eof.into(), STRINGS[Error::Eof.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::Exit.into(), STRINGS[Error::Exit.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::External.into(), STRINGS[Error::External.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::InvalidData.into(), STRINGS[Error::InvalidData.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::PatchWelcome.into(), STRINGS[Error::PatchWelcome.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Bug.into(), STRINGS[index(&Error::Bug)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Bug2.into(), STRINGS[index(&Error::Bug2)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Unknown.into(), STRINGS[index(&Error::Unknown)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Experimental.into(), STRINGS[index(&Error::Experimental)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::BufferTooSmall.into(), STRINGS[index(&Error::BufferTooSmall)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Eof.into(), STRINGS[index(&Error::Eof)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::Exit.into(), STRINGS[index(&Error::Exit)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::External.into(), STRINGS[index(&Error::External)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::InvalidData.into(), STRINGS[index(&Error::InvalidData)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::PatchWelcome.into(), STRINGS[index(&Error::PatchWelcome)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::InputChanged.into(), STRINGS[Error::InputChanged.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::OutputChanged.into(), STRINGS[Error::OutputChanged.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::InputChanged.into(), STRINGS[index(&Error::InputChanged)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::OutputChanged.into(), STRINGS[index(&Error::OutputChanged)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::BsfNotFound.into(), STRINGS[Error::BsfNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::DecoderNotFound.into(), STRINGS[Error::DecoderNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::DemuxerNotFound.into(), STRINGS[Error::DemuxerNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::EncoderNotFound.into(), STRINGS[Error::EncoderNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::OptionNotFound.into(), STRINGS[Error::OptionNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::MuxerNotFound.into(), STRINGS[Error::MuxerNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::FilterNotFound.into(), STRINGS[Error::FilterNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::ProtocolNotFound.into(), STRINGS[Error::ProtocolNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::StreamNotFound.into(), STRINGS[Error::StreamNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::BsfNotFound.into(), STRINGS[index(&Error::BsfNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::DecoderNotFound.into(), STRINGS[index(&Error::DecoderNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::DemuxerNotFound.into(), STRINGS[index(&Error::DemuxerNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::EncoderNotFound.into(), STRINGS[index(&Error::EncoderNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::OptionNotFound.into(), STRINGS[index(&Error::OptionNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::MuxerNotFound.into(), STRINGS[index(&Error::MuxerNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::FilterNotFound.into(), STRINGS[index(&Error::FilterNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::ProtocolNotFound.into(), STRINGS[index(&Error::ProtocolNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::StreamNotFound.into(), STRINGS[index(&Error::StreamNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpBadRequest.into(), STRINGS[Error::HttpBadRequest.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpUnauthorized.into(), STRINGS[Error::HttpUnauthorized.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpForbidden.into(), STRINGS[Error::HttpForbidden.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpNotFound.into(), STRINGS[Error::HttpNotFound.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpOther4xx.into(), STRINGS[Error::HttpOther4xx.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); - av_strerror(Error::HttpServerError.into(), STRINGS[Error::HttpServerError.index()].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpBadRequest.into(), STRINGS[index(&Error::HttpBadRequest)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpUnauthorized.into(), STRINGS[index(&Error::HttpUnauthorized)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpForbidden.into(), STRINGS[index(&Error::HttpForbidden)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpNotFound.into(), STRINGS[index(&Error::HttpNotFound)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpOther4xx.into(), STRINGS[index(&Error::HttpOther4xx)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); + av_strerror(Error::HttpServerError.into(), STRINGS[index(&Error::HttpServerError)].as_mut_ptr(), AV_ERROR_MAX_STRING_SIZE); } } impl error::Error for Error { fn description(&self) -> &str { unsafe { - from_utf8_unchecked(CStr::from_ptr(STRINGS[self.index()].as_ptr()).to_bytes()) + from_utf8_unchecked(CStr::from_ptr(STRINGS[index(self)].as_ptr()).to_bytes()) } } }