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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
    parse_macro_input, punctuated::Punctuated, Attribute, Data, DeriveInput, Expr, Fields, LitStr,
    MetaNameValue, Token,
};

#[allow(unused_assignments)]
#[proc_macro_attribute]
/// This macro is used to generate a builder pattern for an obs source. <br>
/// The attribute should be the id of the source.<br>
/// The struct should have named fields, each field should have an attribute `#[obs_property(type_t="your_type")]`. <br>
/// `type_t` can be `enum`, `enum_string`, `string`, `bool` or `int`. <br>
/// - `enum`: the field should be an enum with `num_derive::{FromPrimitive, ToPrimitive}`.
/// - `enum_string`: the field should be an enum which implements `StringEnum`.
/// - `string`: the field should be a string.
/// - `bool`: the field should be a bool.
/// - `type_t`: `int`, the field should be an i64.
/// The attribute can also have a `settings_key` which is the key used in the settings, if this attribute is not given, the macro defaults to the field name. <br>
/// Documentation is inherited from the field to the setter function.<br>
/// Example: <br>
/// ```
/// use libobs_wrapper::sources::StringEnum;
/// use libobs_source_macro::obs_source_builder;
/// use num_derive::{FromPrimitive, ToPrimitive};
///
/// #[repr(i32)]
/// #[derive(Clone, Copy, Debug, PartialEq, Eq, FromPrimitive, ToPrimitive)]
/// pub enum ObsWindowCaptureMethod {
///     MethodAuto = libobs::window_capture_method_METHOD_AUTO,
/// 	MethodBitBlt = libobs::window_capture_method_METHOD_BITBLT,
/// 	MethodWgc = libobs::window_capture_method_METHOD_WGC,
/// }
///
/// #[derive(Clone, Copy, Debug, PartialEq, Eq)]
/// pub enum ObsGameCaptureRgbaSpace {
///     SRgb,
///     RGBA2100pq
/// }
///
/// impl StringEnum for ObsGameCaptureRgbaSpace {
///     fn to_str(&self) -> &str {
///         match self {
///             ObsGameCaptureRgbaSpace::SRgb => "sRGB",
///             ObsGameCaptureRgbaSpace::RGBA2100pq => "Rec. 2100 (PQ)"
///         }
///     }
/// }
///
/// /// Provides a easy to use builder for the window capture source.
/// #[derive(Debug)]
/// #[obs_source_builder("window_capture")]
/// pub struct WindowCaptureSourceBuilder {
///     #[obs_property(type_t="enum")]
///     /// Sets the capture method for the window capture
///     capture_method: ObsWindowCaptureMethod,
///
///     /// Sets the window to capture.
///     #[obs_property(type_t = "string", settings_key = "window")]
///     window_raw: String,
///
///     #[obs_property(type_t = "bool")]
///     /// Sets whether the cursor should be captured
///     cursor: bool,
///
///     /// Sets the capture mode for the game capture source. Look at doc for `ObsGameCaptureMode`
///     #[obs_property(type_t = "enum_string")]
///     capture_mode: ObsGameCaptureMode,
/// }
/// ```
pub fn obs_source_builder(attr: TokenStream, item: TokenStream) -> TokenStream {
    let id = parse_macro_input!(attr as LitStr);

    let input = parse_macro_input!(item as DeriveInput);

    let name = input.ident;
    let generics = input.generics;
    let visibility = input.vis;
    let attributes = input.attrs;

    let fields = match input.data {
        Data::Struct(data) => match data.fields {
            Fields::Named(fields) => fields.named,
            _ => panic!("Only named fields are supported"),
        },
        _ => panic!("Only structs are supported"),
    };

    let id_value = id.value();
    let fields_tokens = fields.iter().map(|f| {
        let name = &f.ident;
        quote! {
            /// IGNORE THIS FIELD. This is just so intellisense doesn't get confused and isn't complaining
            #name: u8
        }
    });

    let field_initializers = fields.iter().map(|f| {
        let name = &f.ident;
        quote! {
            #name: 0
        }
    });

    let obs_properties = fields
        .iter()
        .filter_map(|f| {
            let attr = f.attrs.iter().find(|e| e.path().is_ident("obs_property"));

            attr.map(|a| (f, a))
        })
        .collect::<Vec<_>>();

    let mut functions = Vec::new();
    for (field, attr) in obs_properties {
        let field_type = &field.ty;
        let field_name = field.ident.as_ref().unwrap();

        let name_values: Punctuated<MetaNameValue, Token![,]> = attr
            .parse_args_with(Punctuated::parse_terminated)
            .expect(&format!(
                "Field {} has invalid obs_property, should be name value",
                field_name
            ));

        let type_t = &name_values
            .iter()
            .find(|e| e.path.get_ident().unwrap().to_string() == "type_t")
            .expect("type_t is required for obs_property")
            .value;

        let type_t = match type_t {
            syn::Expr::Lit(e) => match &e.lit {
                syn::Lit::Str(s) => s.value(),
                _ => panic!("type_t must be a string"),
            },
            _ => panic!("type_t must be a string"),
        };

        #[allow(unused_variables)]
        let mut obs_settings_name = field_name.to_string();
        let pot_name = &name_values
            .iter()
            .find(|e| e.path.get_ident().unwrap().to_string() == "settings_key");

        if let Some(n) = pot_name {
            obs_settings_name = match &n.value {
                syn::Expr::Lit(e) => match &e.lit {
                    syn::Lit::Str(s) => s.value(),
                    _ => panic!("setings_key must be a string"),
                },
                _ => panic!("settings_key must be a string"),
            };
        }

        let (_docs_str, docs_attr) = collect_doc(&field.attrs);

        let obs_settings_key = LitStr::new(&obs_settings_name, Span::call_site());
        let set_field = quote::format_ident!("set_{}", field_name);
        let type_t_str = type_t.as_str();
        let to_add = match type_t_str {
            "enum" => {
                quote! {
                    #(#docs_attr)*
                    pub fn #set_field(mut self, #field_name: #field_type) -> Self {
                        use num_traits::ToPrimitive;
                        use libobs_wrapper::sources::ObsSourceBuilder;
                        let val = #field_name.to_i32().unwrap();

                        self.get_or_create_settings()
                            .set_int(#obs_settings_key, val as i64);

                        self
                    }
                }
            },
            "enum_string" => {
                quote! {
                    #(#docs_attr)*
                    pub fn #set_field(mut self, #field_name: #field_type) -> Self {
                        use libobs_wrapper::sources::StringEnum;
                        use libobs_wrapper::sources::ObsSourceBuilder;

                        self.get_or_create_settings()
                            .set_string(#obs_settings_key, #field_name.to_str());

                        self
                    }
                }
            },
            "string" => {
                quote! {
                    #(#docs_attr)*
                    pub fn #set_field(mut self, #field_name: impl Into<libobs_wrapper::utils::ObsString>) -> Self {
                        use libobs_wrapper::sources::ObsSourceBuilder;
                        self.get_or_create_settings()
                            .set_string(#obs_settings_key, #field_name);
                        self
                    }
                }
            }
            "bool" => {
                quote! {
                    #(#docs_attr)*
                    pub fn #set_field(mut self, #field_name: bool) -> Self {
                        use libobs_wrapper::sources::ObsSourceBuilder;
                        self.get_or_create_settings()
                            .set_bool(#obs_settings_key, #field_name);
                        self
                    }
                }
            },
            "int" => {
                quote! {
                    #(#docs_attr)*
                    pub fn #set_field(mut self, #field_name: i64) -> Self {
                        use libobs_wrapper::sources::ObsSourceBuilder;
                        self.get_or_create_settings()
                            .set_int(#obs_settings_key, #field_name);
                        self
                    }
                }
            }
            _ => panic!(
                "Unsupported type_t {}. Should either be `enum`, `string`, `bool` or `int`",
                type_t
            ),
        };

        functions.push(to_add);
    }

    let expanded = quote! {
        #(#attributes)*
        #[allow(dead_code)]
        #visibility struct #name #generics {
            #(#fields_tokens,)*
            settings: Option<libobs_wrapper::data::ObsData>,
            hotkeys: Option<libobs_wrapper::data::ObsData>,
            name: libobs_wrapper::utils::ObsString
        }

        impl libobs_wrapper::sources::ObsSourceBuilder for #name {
            fn new(name: impl Into<libobs_wrapper::utils::ObsString>) -> Self {
                Self {
                    #(#field_initializers,)*
                    settings: None,
                    hotkeys: None,
                    name: name.into(),
                }
            }

            fn get_settings(&self) -> &Option<libobs_wrapper::data::ObsData> {
                &self.settings
            }

            fn get_settings_mut(&mut self) -> &mut Option<libobs_wrapper::data::ObsData> {
                &mut self.settings
            }

            fn get_hotkeys(&self) -> &Option<libobs_wrapper::data::ObsData> {
                &self.hotkeys
            }

            fn get_hotkeys_mut(&mut self) -> &mut Option<libobs_wrapper::data::ObsData> {
                &mut self.hotkeys
            }

            fn get_name(&self) -> libobs_wrapper::utils::ObsString {
                self.name.clone()
            }

            fn get_id() -> libobs_wrapper::utils::ObsString {
                #id_value.into()
            }
        }

        impl #name {
            #(#functions)*
        }
    };

    TokenStream::from(expanded)
}

fn collect_doc(attrs: &Vec<Attribute>) -> (Vec<String>, Vec<&Attribute>) {
    let mut docs_str = Vec::new();
    let mut docs_attr = Vec::new();
    for attr in attrs {
        let name_val = match &attr.meta {
            syn::Meta::NameValue(n) => n,
            _ => continue,
        };

        let is_doc = name_val.path.is_ident("doc");
        if !is_doc {
            continue;
        }

        let lit = match &name_val.value {
            Expr::Lit(l) => match &l.lit {
                syn::Lit::Str(s) => s.value(),
                _ => continue,
            },
            _ => continue,
        };

        docs_str.push(lit);
        docs_attr.push(attr);
    }

    (docs_str, docs_attr)
}