diesel_derives/parsers/
mysql_type.rs

1use syn::parse::{Parse, ParseStream, Result};
2use syn::punctuated::Punctuated;
3use syn::token::Comma;
4use syn::{Ident, LitStr};
5
6use crate::util::{parse_eq, unknown_attribute, MYSQL_TYPE_NOTE};
7
8enum Attr {
9    Name(LitStr),
10}
11
12impl Parse for Attr {
13    fn parse(input: ParseStream) -> Result<Self> {
14        let name: Ident = input.parse()?;
15        let name_str = name.to_string();
16
17        match &*name_str {
18            "name" => Ok(Attr::Name(parse_eq(input, MYSQL_TYPE_NOTE)?)),
19
20            _ => Err(unknown_attribute(&name, &["name"])),
21        }
22    }
23}
24
25pub struct MysqlType {
26    pub name: LitStr,
27}
28
29impl Parse for MysqlType {
30    fn parse(input: ParseStream) -> Result<Self> {
31        let mut name = None;
32
33        for attr in Punctuated::<Attr, Comma>::parse_terminated(input)? {
34            match attr {
35                Attr::Name(value) => name = Some(value),
36            }
37        }
38
39        if let Some(name) = name {
40            Ok(MysqlType { name })
41        } else {
42            Err(syn::Error::new(
43                input.span(),
44                format!(
45                    "expected attribute `name`\n\
46                     help: The correct format looks like #[diesel({})]",
47                    MYSQL_TYPE_NOTE
48                ),
49            ))
50        }
51    }
52}