diesel_derives/deprecated/
postgres_type.rs

1use syn::parse::{Parse, ParseStream, Result};
2use syn::punctuated::Punctuated;
3use syn::token::Comma;
4use syn::{parenthesized, Ident, LitInt, LitStr};
5
6use crate::deprecated::utils::parse_eq_and_lit_str;
7use crate::parsers::PostgresType;
8use crate::util::{unknown_attribute, POSTGRES_TYPE_NOTE};
9
10enum Attr {
11    Oid(Ident, LitInt),
12    ArrayOid(Ident, LitInt),
13    TypeName(Ident, LitStr),
14}
15
16impl Parse for Attr {
17    fn parse(input: ParseStream) -> Result<Self> {
18        let name: Ident = input.parse()?;
19        let name_str = name.to_string();
20
21        match &*name_str {
22            "oid" => Ok(Attr::Oid(name.clone(), {
23                let lit_str = parse_eq_and_lit_str(name, input, POSTGRES_TYPE_NOTE)?;
24                lit_str.parse()?
25            })),
26            "array_oid" => Ok(Attr::ArrayOid(name.clone(), {
27                let lit_str = parse_eq_and_lit_str(name, input, POSTGRES_TYPE_NOTE)?;
28                lit_str.parse()?
29            })),
30            "type_name" => Ok(Attr::TypeName(
31                name.clone(),
32                parse_eq_and_lit_str(name, input, POSTGRES_TYPE_NOTE)?,
33            )),
34
35            _ => Err(unknown_attribute(&name, &["oid", "array_oid", "type_name"])),
36        }
37    }
38}
39
40pub fn parse_postgres_type(name: Ident, input: ParseStream) -> Result<PostgresType> {
41    if input.is_empty() {
42        return Err(syn::Error::new(
43            name.span(),
44            format!(
45                "unexpected end of input, expected parentheses\n\
46                 help: The correct format looks like `#[diesel({})]`",
47                POSTGRES_TYPE_NOTE
48            ),
49        ));
50    }
51
52    let content;
53    parenthesized!(content in input);
54
55    let mut oid = None;
56    let mut array_oid = None;
57    let mut type_name = None;
58
59    for attr in Punctuated::<Attr, Comma>::parse_terminated(&content)? {
60        match attr {
61            Attr::Oid(ident, value) => oid = Some((ident, value)),
62            Attr::ArrayOid(ident, value) => array_oid = Some((ident, value)),
63            Attr::TypeName(ident, value) => type_name = Some((ident, value)),
64        }
65    }
66
67    PostgresType::validate_and_build(&content, oid, array_oid, type_name, None)
68}