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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Take a look at the license at the top of the repository in the LICENSE file.

use anyhow::{bail, Result};
use proc_macro2::Span;
use syn::{
    parse::Error, spanned::Spanned, Attribute, DeriveInput, Field, Fields, Ident, Lit, Meta,
    MetaList, MetaNameValue, NestedMeta, Type,
};

pub struct Template {
    pub source: TemplateSource,
    pub allow_template_child_without_attribute: bool,
}

pub enum TemplateSource {
    File(String),
    Resource(String),
    Xml(String),
    #[cfg(feature = "blueprint")]
    Blueprint(String),
}

impl TemplateSource {
    fn from_string_source(source: String) -> Result<Self> {
        for c in source.chars() {
            #[cfg(feature = "blueprint")]
            if c.is_ascii_alphabetic() {
                // blueprint code starts with some alphabetic letters
                return Ok(Self::Blueprint(source));
            } else if c == '<' {
                // xml tags starts with '<' symbol
                return Ok(Self::Xml(source));
            }
            #[cfg(not(feature = "blueprint"))]
            if c == '<' {
                // xml tags starts with '<' symbol
                return Ok(Self::Xml(source));
            }
        }
        bail!("Unknown language")
    }
}

pub fn parse_template_source(input: &DeriveInput) -> Result<Template> {
    let meta = match find_attribute_meta(&input.attrs, "template")? {
        Some(meta) => meta,
        _ => bail!("Missing 'template' attribute"),
    };

    let mut allow_template_child_without_attribute = false;
    let mut source = None;

    for n in meta.nested.iter() {
        if let NestedMeta::Meta(m) = n {
            let p = m.path();
            if p.is_ident("file") || p.is_ident("resource") || p.is_ident("string") {
                if source.is_some() {
                    bail!(Error::new_spanned(
                        p,
                        "Specify only one of 'file', 'resource', or 'string'"
                    ));
                }
                source.replace(n);
            }
            if p.is_ident("allow_template_child_without_attribute") {
                if !matches!(m, Meta::Path(_)) {
                    bail!(Error::new_spanned(m, "Wrong meta"));
                }
                if allow_template_child_without_attribute {
                    bail!(Error::new_spanned(
                        p,
                        "Duplicate 'allow_template_child_without_attribute'"
                    ));
                }
                allow_template_child_without_attribute = true;
            }
        } else {
            bail!(Error::new_spanned(n, "wrong meta type"));
        }
    }
    let source = match source {
        Some(source) => source,
        None => bail!(Error::new_spanned(
            &meta,
            "Invalid meta, specify one of 'file', 'resource', or 'string'"
        )),
    };

    let (ident, v) = parse_attribute(source)?;

    let source = match ident.as_ref() {
        "file" => TemplateSource::File(v),
        "resource" => TemplateSource::Resource(v),
        "string" => TemplateSource::from_string_source(v)?,
        s => bail!("Unknown enum meta {}", s),
    };
    Ok(Template {
        source,
        allow_template_child_without_attribute,
    })
}

// find the #[@attr_name] attribute in @attrs
fn find_attribute_meta(attrs: &[Attribute], attr_name: &str) -> Result<Option<MetaList>> {
    let meta = match attrs.iter().find(|a| a.path.is_ident(attr_name)) {
        Some(a) => a.parse_meta(),
        _ => return Ok(None),
    };
    match meta? {
        Meta::List(n) => Ok(Some(n)),
        _ => bail!("wrong meta type"),
    }
}

// parse a single meta like: ident = "value"
fn parse_attribute(meta: &NestedMeta) -> Result<(String, String)> {
    let meta = match &meta {
        NestedMeta::Meta(m) => m,
        _ => bail!("wrong meta type"),
    };
    let meta = match meta {
        Meta::NameValue(n) => n,
        _ => bail!("wrong meta type"),
    };
    let value = match &meta.lit {
        Lit::Str(s) => s.value(),
        _ => bail!("wrong meta type"),
    };

    let ident = match meta.path.get_ident() {
        None => bail!("missing ident"),
        Some(ident) => ident,
    };

    Ok((ident.to_string(), value))
}

pub enum FieldAttributeArg {
    Id(String, Span),
    Internal(bool),
}

#[derive(Debug)]
pub enum FieldAttributeType {
    TemplateChild,
}

pub struct FieldAttribute {
    pub ty: FieldAttributeType,
    pub args: Vec<FieldAttributeArg>,
    pub path_span: Span,
    pub span: Span,
}

pub struct AttributedField {
    pub ident: Ident,
    pub ty: Type,
    pub attr: FieldAttribute,
}

impl AttributedField {
    pub fn id(&self) -> String {
        let mut name = None;
        for arg in &self.attr.args {
            if let FieldAttributeArg::Id(value, _) = arg {
                name = Some(value)
            }
        }
        name.cloned().unwrap_or_else(|| self.ident.to_string())
    }

    #[cfg(feature = "xml_validation")]
    pub fn id_span(&self) -> Span {
        for arg in &self.attr.args {
            if let FieldAttributeArg::Id(_, span) = arg {
                return *span;
            }
        }
        self.ident.span()
    }
}

fn parse_field_attr_value_str(name_value: &MetaNameValue) -> Result<String, Error> {
    match &name_value.lit {
        Lit::Str(s) => Ok(s.value()),
        _ => Err(Error::new(
            name_value.lit.span(),
            "invalid value type: Expected str literal",
        )),
    }
}

fn parse_field_attr_value_bool(internal_value: &MetaNameValue) -> Result<bool, Error> {
    match &internal_value.lit {
        Lit::Bool(s) => Ok(s.value()),
        _ => Err(Error::new(
            internal_value.lit.span(),
            "invalid value type: Expected bool",
        )),
    }
}

fn parse_field_attr_meta(
    ty: &FieldAttributeType,
    meta: &NestedMeta,
) -> Result<FieldAttributeArg, Error> {
    let meta = match &meta {
        NestedMeta::Meta(m) => m,
        _ => {
            return Err(Error::new(
                meta.span(),
                "invalid type - expected a name-value pair like id = \"widget\"",
            ))
        }
    };
    let name_value = match meta {
        Meta::NameValue(n) => n,
        _ => {
            return Err(Error::new(
                meta.span(),
                "invalid type - expected a name-value pair like id = \"widget\"",
            ))
        }
    };
    let path = &name_value.path;
    let ident = path
        .get_ident()
        .ok_or_else(|| Error::new(path.span(), "invalid name type - expected identifier"))?;

    let ident_str = ident.to_string();
    let unknown_err = Err(Error::new(
        ident.span(),
        format!("unknown attribute argument: `{ident_str}`"),
    ));
    let value = match ty {
        FieldAttributeType::TemplateChild => match ident_str.as_str() {
            "id" => FieldAttributeArg::Id(
                parse_field_attr_value_str(name_value)?,
                name_value.lit.span(),
            ),
            "internal" => FieldAttributeArg::Internal(parse_field_attr_value_bool(name_value)?),
            _ => return unknown_err,
        },
    };

    Ok(value)
}

fn parse_field_attr_args(
    ty: &FieldAttributeType,
    attr: &Attribute,
) -> Result<Vec<FieldAttributeArg>, Error> {
    let mut field_attribute_args = Vec::new();
    match attr.parse_meta()? {
        Meta::List(list) => {
            for meta in &list.nested {
                let new_arg = parse_field_attr_meta(ty, meta)?;
                for arg in &field_attribute_args {
                    // Comparison of enum variants, not data
                    if std::mem::discriminant(arg) == std::mem::discriminant(&new_arg) {
                        return Err(Error::new(
                            meta.span(),
                            "two instances of the same attribute \
                            argument, each argument must be specified only once",
                        ));
                    }
                }
                field_attribute_args.push(new_arg);
            }
        }
        Meta::Path(_) => (),
        meta => {
            return Err(Error::new(
                meta.span(),
                "invalid attribute argument type, expected `name = value` list or nothing",
            ))
        }
    }

    Ok(field_attribute_args)
}

fn parse_field(field: &Field) -> Result<Option<AttributedField>, Error> {
    let field_attrs = &field.attrs;
    let ident = match &field.ident {
        Some(ident) => ident,
        None => return Err(Error::new(field.span(), "expected identifier")),
    };

    let ty = &field.ty;
    let mut attr = None;

    for field_attr in field_attrs {
        let span = field_attr.span();
        let path_span = field_attr.path.span();
        let ty = if field_attr.path.is_ident("template_child") {
            Some(FieldAttributeType::TemplateChild)
        } else {
            None
        };

        if let Some(ty) = ty {
            let args = parse_field_attr_args(&ty, field_attr)?;

            if attr.is_none() {
                attr = Some(FieldAttribute {
                    ty,
                    args,
                    path_span,
                    span,
                })
            } else {
                return Err(Error::new(
                    span,
                    "multiple attributes on the same field are not supported",
                ));
            }
        }
    }

    if let Some(attr) = attr {
        Ok(Some(AttributedField {
            ident: ident.clone(),
            ty: ty.clone(),
            attr,
        }))
    } else {
        Ok(None)
    }
}

fn path_is_template_child(path: &syn::Path) -> bool {
    if path.leading_colon.is_none()
        && path.segments.len() == 1
        && matches!(
            &path.segments[0].arguments,
            syn::PathArguments::AngleBracketed(_)
        )
        && path.segments[0].ident == "TemplateChild"
    {
        return true;
    }
    if path.segments.len() == 2
        && (path.segments[0].ident == "gtk" || path.segments[0].ident == "gtk4")
        && matches!(
            &path.segments[1].arguments,
            syn::PathArguments::AngleBracketed(_)
        )
        && path.segments[1].ident == "TemplateChild"
    {
        return true;
    }
    false
}

pub fn parse_fields(
    fields: &Fields,
    allow_missing_attribute: bool,
) -> Result<Vec<AttributedField>, Error> {
    let mut attributed_fields = Vec::new();

    for field in fields {
        let mut has_attr = false;
        if !field.attrs.is_empty() {
            if let Some(attributed_field) = parse_field(field)? {
                attributed_fields.push(attributed_field);
                has_attr = true;
            }
        }
        if !has_attr && !allow_missing_attribute {
            if let syn::Type::Path(syn::TypePath { path, .. }) = &field.ty {
                if path_is_template_child(path) {
                    proc_macro_error::emit_error!(
                        field,
                        "field `{}` with type `TemplateChild` possibly missing #[template_child] attribute. Use a meta attribute on the struct to suppress this error: '#[template(string|file|resource = \"...\", allow_template_child_without_attribute)]'",
                        field.ident.as_ref().unwrap().to_string(),
                    );
                }
            }
        }
    }

    Ok(attributed_fields)
}