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
use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{Ident, LitStr};

use util::{parse_eq, unknown_attribute, SQLITE_TYPE_NOTE};

enum Attr {
    Name(Ident, LitStr),
}

impl Parse for Attr {
    fn parse(input: ParseStream) -> Result<Self> {
        let name: Ident = input.parse()?;
        let name_str = name.to_string();

        match &*name_str {
            "name" => Ok(Attr::Name(name, parse_eq(input, SQLITE_TYPE_NOTE)?)),

            _ => unknown_attribute(&name, &["name"]),
        }
    }
}

pub struct SqliteType {
    pub name: LitStr,
}

impl Parse for SqliteType {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut name = None;

        for attr in Punctuated::<Attr, Comma>::parse_terminated(input)? {
            match attr {
                Attr::Name(_, value) => name = Some(value),
            }
        }

        if let Some(name) = name {
            Ok(SqliteType { name })
        } else {
            abort!(
                input.span(), "expected attribute `name`";
                help = "The correct format looks like #[diesel({})]", SQLITE_TYPE_NOTE
            );
        }
    }
}