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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Take a look at the license at the top of the repository in the LICENSE file.

use std::ffi::CStr;

use glib::{prelude::*, translate::*};

use crate::{ffi, Caps, PadDirection, PadPresence, PadTemplate, StaticPadTemplate};

impl PadTemplate {
    /// Converts a [`StaticPadTemplate`][crate::StaticPadTemplate] into a [`PadTemplate`][crate::PadTemplate] with a type.
    /// ## `pad_template`
    /// the static pad template
    /// ## `pad_type`
    /// The `GType` of the pad to create
    ///
    /// # Returns
    ///
    /// a new [`PadTemplate`][crate::PadTemplate].
    #[doc(alias = "gst_pad_template_new_from_static_pad_template_with_gtype")]
    pub fn from_static_pad_template_with_gtype(
        pad_template: &StaticPadTemplate,
        pad_type: glib::types::Type,
    ) -> Result<PadTemplate, glib::BoolError> {
        skip_assert_initialized!();
        unsafe {
            Option::<_>::from_glib_none(
                ffi::gst_pad_template_new_from_static_pad_template_with_gtype(
                    mut_override(pad_template.to_glib_none().0),
                    pad_type.into_glib(),
                ),
            )
            .ok_or_else(|| glib::bool_error!("Failed to create PadTemplate"))
        }
    }

    /// Gets the capabilities of the pad template.
    ///
    /// # Returns
    ///
    /// the [`Caps`][crate::Caps] of the pad template.
    /// Unref after usage.
    #[doc(alias = "gst_pad_template_get_caps")]
    #[doc(alias = "get_caps")]
    pub fn caps(&self) -> &Caps {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            &*(&templ.caps as *const *mut ffi::GstCaps as *const Caps)
        }
    }

    /// See [`set_documentation_caps()`][Self::set_documentation_caps()].
    ///
    /// # Returns
    ///
    /// The caps to document. For convenience, this will return
    ///  [`caps()`][Self::caps()] when no documentation caps were set.
    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    #[doc(alias = "gst_pad_template_get_documentation_caps")]
    #[doc(alias = "get_documentation_caps")]
    pub fn documentation_caps(&self) -> &Caps {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            if !templ.ABI.abi.documentation_caps.is_null() {
                &*(&templ.ABI.abi.documentation_caps as *const *mut ffi::GstCaps as *const Caps)
            } else {
                &*(&templ.caps as *const *mut ffi::GstCaps as *const Caps)
            }
        }
    }

    pub fn direction(&self) -> PadDirection {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            from_glib(templ.direction)
        }
    }

    pub fn gtype(&self) -> glib::types::Type {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            from_glib(templ.ABI.abi.gtype)
        }
    }

    #[doc(alias = "name-template")]
    pub fn name_template(&self) -> &str {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            CStr::from_ptr(templ.name_template).to_str().unwrap()
        }
    }

    pub fn presence(&self) -> PadPresence {
        unsafe {
            let templ = &*(self.as_ptr() as *const ffi::GstPadTemplate);
            from_glib(templ.presence)
        }
    }

    pub fn builder<'a>(
        name_template: &'a str,
        direction: PadDirection,
        presence: PadPresence,
        caps: &'a Caps,
    ) -> PadTemplateBuilder<'a> {
        skip_assert_initialized!();

        PadTemplateBuilder {
            name_template,
            direction,
            presence,
            caps,
            gtype: None,
            #[cfg(feature = "v1_18")]
            #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
            documentation_caps: None,
        }
    }
}

#[must_use = "The builder must be built to be used"]
#[derive(Debug)]
pub struct PadTemplateBuilder<'a> {
    name_template: &'a str,
    direction: PadDirection,
    presence: PadPresence,
    caps: &'a Caps,
    gtype: Option<glib::Type>,
    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    documentation_caps: Option<&'a Caps>,
}

impl<'a> PadTemplateBuilder<'a> {
    pub fn gtype(self, gtype: glib::Type) -> Self {
        PadTemplateBuilder {
            gtype: Some(gtype),
            ..self
        }
    }

    pub fn gtype_if(self, gtype: glib::Type, predicate: bool) -> Self {
        if predicate {
            PadTemplateBuilder {
                gtype: Some(gtype),
                ..self
            }
        } else {
            self
        }
    }

    pub fn gtype_if_some(self, gtype: Option<glib::Type>) -> Self {
        if let Some(gtype) = gtype {
            self.gtype(gtype)
        } else {
            self
        }
    }

    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    pub fn documentation_caps(self, documentation_caps: &'a Caps) -> Self {
        PadTemplateBuilder {
            documentation_caps: Some(documentation_caps),
            ..self
        }
    }

    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    pub fn documentation_caps_if(self, documentation_caps: &'a Caps, predicate: bool) -> Self {
        if predicate {
            PadTemplateBuilder {
                documentation_caps: Some(documentation_caps),
                ..self
            }
        } else {
            self
        }
    }

    #[cfg(feature = "v1_18")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
    pub fn documentation_caps_if_some(self, documentation_caps: Option<&'a Caps>) -> Self {
        if let Some(documentation_caps) = documentation_caps {
            self.documentation_caps(documentation_caps)
        } else {
            self
        }
    }

    pub fn build(self) -> Result<PadTemplate, glib::BoolError> {
        let templ = if let Some(gtype) = self.gtype {
            PadTemplate::with_gtype(
                self.name_template,
                self.direction,
                self.presence,
                self.caps,
                gtype,
            )?
        } else {
            PadTemplate::new(self.name_template, self.direction, self.presence, self.caps)?
        };

        #[cfg(feature = "v1_18")]
        #[cfg_attr(docsrs, doc(cfg(feature = "v1_18")))]
        if let Some(documentation_caps) = self.documentation_caps {
            unsafe {
                ffi::gst_pad_template_set_documentation_caps(
                    templ.to_glib_none().0,
                    documentation_caps.to_glib_none().0,
                );
            }
        }

        Ok(templ)
    }
}