1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::ops::Deref;
use thiserror::Error;

/// OpenTok error status codes.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Error)]
#[must_use]
#[allow(clippy::manual_non_exhaustive)]
pub enum OtcError {
    /// An argument used in a function call is not valid
    #[error("The argument {0} used in a function call is not valid")]
    InvalidParam(&'static str),
    /// Generic error
    #[error("Generic error")]
    Fatal,
    /// The connection to the OpenTok messaging server was dropped. Check the network connection"
    #[error(
        "The connection to the OpenTok messaging server was dropped. Check the network connection"
    )]
    ConnectionDropped,
    /// Timeout while performing a connect action
    #[error("Timeout while performing a connect action")]
    TimedOut,
    /// An unknown Publisher instance was used as a function argument
    #[error("An unknown Publisher instance was used as a function argument")]
    UnknownPublisherInstance,
    /// An unknown Subscriber instance was used as a function argument
    #[error("An unknown Subscriber instance was used as a function argument")]
    UnknownSubscriberInstance,
    /// There was an error with video capturer
    #[error("There was an error with video capturer")]
    VideoCaptureFailed,
    /// There was an error while acquiring video from the camera
    #[error("There was an error while acquiring video from the camera")]
    CameraFailed,
    /// There was an error while rendering video
    #[error("There was an error while rendering video")]
    VideoRenderFailed,
    /// There was an error when trying to get the list of supported video codecs
    #[error("There was an error when trying to get the list of supported video codecs.")]
    UnableToAccessMediaEngine,
    /// Unexpected null pointer
    #[error("Unexpected null pointer")]
    NullError,
    /// Double initilization error.
    #[error("{0} has already been initialized")]
    AlreadyInitialized(&'static str),
    /// Initialization error.
    #[error("Could not initialize {0}: {1}")]
    Initialization(&'static str, &'static str),
    /// Unknown error
    #[doc(hidden)]
    #[error("Unknown error. Life is hard sometimes")]
    __Unknown,
}

pub type OtcResult = Result<(), OtcError>;

pub trait IntoResult {
    fn into_result(self) -> Result<(), OtcError>;
}

impl IntoResult for ffi::otc_status {
    fn into_result(self) -> Result<(), OtcError> {
        match self as u32 {
            ffi::otc_constant_OTC_SUCCESS => Ok(()),
            ffi::otc_error_code_OTC_INVALID_PARAM => Err(OtcError::InvalidParam("")),
            ffi::otc_error_code_OTC_FATAL => Err(OtcError::Fatal),
            ffi::otc_error_code_OTC_CONNECTION_DROPPED => Err(OtcError::ConnectionDropped),
            ffi::otc_error_code_OTC_CONNECTION_TIMED_OUT => Err(OtcError::TimedOut),
            ffi::otc_error_code_OTC_UNKNOWN_PUBLISHER_INSTANCE => {
                Err(OtcError::UnknownPublisherInstance)
            }
            ffi::otc_error_code_OTC_UNKNOWN_SUBSCRIBER_INSTANCE => {
                Err(OtcError::UnknownSubscriberInstance)
            }
            ffi::otc_error_code_OTC_VIDEO_CAPTURE_FAILED => Err(OtcError::VideoCaptureFailed),
            ffi::otc_error_code_OTC_CAMERA_FAILED => Err(OtcError::CameraFailed),
            ffi::otc_error_code_OTC_VIDEO_RENDER_FAILED => Err(OtcError::VideoRenderFailed),
            ffi::otc_error_code_OT_UNABLE_TO_ACCESS_MEDIA_ENGINE => {
                Err(OtcError::UnableToAccessMediaEngine)
            }
            _ => Err(OtcError::__Unknown),
        }
    }
}

pub struct OtcBool(pub ffi::otc_bool);

impl Deref for OtcBool {
    type Target = bool;

    fn deref(&self) -> &bool {
        match self.0 {
            0 => &false,
            _ => &true,
        }
    }
}

impl From<OtcResult> for OtcBool {
    fn from(result: OtcResult) -> OtcBool {
        match result {
            Ok(_) => OtcBool(1),
            Err(_) => OtcBool(0),
        }
    }
}

impl From<bool> for OtcBool {
    fn from(value: bool) -> OtcBool {
        match value {
            true => OtcBool(1),
            false => OtcBool(0),
        }
    }
}

impl From<OtcBool> for ffi::otc_bool {
    fn from(value: OtcBool) -> ffi::otc_bool {
        value.0
    }
}