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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::ffi::CString;
use std::ops::Deref;

use super::{Ass, Bitmap, Flags, Text, Type};
use ffi::*;
use libc::c_int;

pub enum RectMut<'a> {
    None(*mut AVSubtitleRect),
    Bitmap(BitmapMut<'a>),
    Text(TextMut<'a>),
    Ass(AssMut<'a>),
}

impl<'a> RectMut<'a> {
    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
        match Type::from((*ptr).type_) {
            Type::None => RectMut::None(ptr),
            Type::Bitmap => RectMut::Bitmap(BitmapMut::wrap(ptr)),
            Type::Text => RectMut::Text(TextMut::wrap(ptr)),
            Type::Ass => RectMut::Ass(AssMut::wrap(ptr)),
        }
    }

    pub unsafe fn as_ptr(&self) -> *const AVSubtitleRect {
        match *self {
            RectMut::None(ptr) => ptr as *const _,
            RectMut::Bitmap(ref b) => b.as_ptr(),
            RectMut::Text(ref t) => t.as_ptr(),
            RectMut::Ass(ref a) => a.as_ptr(),
        }
    }

    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
        match *self {
            RectMut::None(ptr) => ptr,
            RectMut::Bitmap(ref mut b) => b.as_mut_ptr(),
            RectMut::Text(ref mut t) => t.as_mut_ptr(),
            RectMut::Ass(ref mut a) => a.as_mut_ptr(),
        }
    }
}

impl<'a> RectMut<'a> {
    pub fn flags(&self) -> Flags {
        unsafe {
            Flags::from_bits_truncate(match *self {
                RectMut::None(ptr) => (*ptr).flags,
                RectMut::Bitmap(ref b) => (*b.as_ptr()).flags,
                RectMut::Text(ref t) => (*t.as_ptr()).flags,
                RectMut::Ass(ref a) => (*a.as_ptr()).flags,
            })
        }
    }
}

pub struct BitmapMut<'a> {
    immutable: Bitmap<'a>,
}

impl<'a> BitmapMut<'a> {
    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
        BitmapMut {
            immutable: Bitmap::wrap(ptr as *const _),
        }
    }

    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
        self.as_ptr() as *mut _
    }
}

impl<'a> BitmapMut<'a> {
    pub fn set_x(&mut self, value: usize) {
        unsafe {
            (*self.as_mut_ptr()).x = value as c_int;
        }
    }

    pub fn set_y(&mut self, value: usize) {
        unsafe {
            (*self.as_mut_ptr()).y = value as c_int;
        }
    }

    pub fn set_width(&mut self, value: u32) {
        unsafe {
            (*self.as_mut_ptr()).w = value as c_int;
        }
    }

    pub fn set_height(&mut self, value: u32) {
        unsafe {
            (*self.as_mut_ptr()).h = value as c_int;
        }
    }

    pub fn set_colors(&mut self, value: usize) {
        unsafe {
            (*self.as_mut_ptr()).nb_colors = value as c_int;
        }
    }
}

impl<'a> Deref for BitmapMut<'a> {
    type Target = Bitmap<'a>;

    fn deref(&self) -> &Self::Target {
        &self.immutable
    }
}

pub struct TextMut<'a> {
    immutable: Text<'a>,
}

impl<'a> TextMut<'a> {
    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
        TextMut {
            immutable: Text::wrap(ptr as *const _),
        }
    }

    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
        self.as_ptr() as *mut _
    }
}

impl<'a> TextMut<'a> {
    pub fn set(&mut self, value: &str) {
        let value = CString::new(value).unwrap();

        unsafe {
            (*self.as_mut_ptr()).text = av_strdup(value.as_ptr());
        }
    }
}

impl<'a> Deref for TextMut<'a> {
    type Target = Text<'a>;

    fn deref(&self) -> &Self::Target {
        &self.immutable
    }
}

pub struct AssMut<'a> {
    immutable: Ass<'a>,
}

impl<'a> AssMut<'a> {
    pub unsafe fn wrap(ptr: *mut AVSubtitleRect) -> Self {
        AssMut {
            immutable: Ass::wrap(ptr),
        }
    }

    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVSubtitleRect {
        self.as_ptr() as *mut _
    }
}

impl<'a> AssMut<'a> {
    pub fn set(&mut self, value: &str) {
        let value = CString::new(value).unwrap();

        unsafe {
            (*self.as_mut_ptr()).ass = av_strdup(value.as_ptr());
        }
    }
}

impl<'a> Deref for AssMut<'a> {
    type Target = Ass<'a>;

    fn deref(&self) -> &Self::Target {
        &self.immutable
    }
}