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
use ffi::*;
use std::result;
use util::*;
use std::fmt::{self,Debug,Formatter,Display};
use std::error;

unsafe impl Send for GError {}
unsafe impl Send for Error {}

pub struct Error{
    error: *mut GError
}

impl Debug for Error{
    fn fmt(&self, fmt: &mut Formatter) -> result::Result<(), fmt::Error>{
        fmt.write_str(format!("gst::Error: domain: {}, code: {}, message: {}",self.domain(),self.code(),self.message()).as_ref())
    }
}

impl Drop for Error{
    fn drop(&mut self){
        unsafe{
            if self.error != ptr::null_mut(){
                g_error_free(self.error);
            }
        }
    }
}

impl Error{
    pub fn new(domain: u32, code: i32, message: &str) -> Error{
        let cmessage = CString::new(message).unwrap();
        unsafe{
            Error{error: g_error_new(domain, code, cmessage.as_ptr())}
        }
    }

    pub unsafe fn new_from_g_error(err: *mut GError) -> Error{
        Error{ error: err }
    }

    pub fn message(&self) -> String{
        unsafe{
            if self.error != ptr::null_mut(){
                from_c_str!(mem::transmute((*self.error).message)).to_string()
            }else{
                "".to_string()
            }
        }
    }

    pub fn code(&self) -> i32{
        unsafe{
            if self.error !=ptr::null_mut(){
                (*self.error).code
            }else{
                0
            }
        }
    }

    pub fn domain(&self) -> u32{
        unsafe{
            if self.error != ptr::null_mut(){
                (*self.error).domain
            }else{
                0
            }
        }
    }
}

impl Display for Error{
    fn fmt(&self, fmt: &mut Formatter) -> result::Result<(), fmt::Error>{
        fmt.write_str(format!("gst::Error: domain: {}, code: {}, message: {}",self.domain(),self.code(),self.message()).as_ref())
    }
}

impl error::Error for Error{
    fn description(&self) -> &str{
        if self.error != ptr::null_mut(){
            unsafe{ from_c_str!(mem::transmute((*self.error).message)) }
        }else{
            ""
        }
    }
}

pub type Result<T> = result::Result<T,Error>;