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
// 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::{Caps, PadDirection, PadPresence, PadTemplate, StaticPadTemplate};

impl 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"))
        }
    }

    #[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)
        }
    }

    #[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
        }
    }

    #[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
        }
    }

    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)
    }
}