diesel_derives/deprecated/
belongs_to.rs

1use syn::parse::{ParseStream, Result};
2use syn::token::Comma;
3use syn::{parenthesized, Ident, LitStr};
4
5use crate::deprecated::utils::parse_eq_and_lit_str;
6use crate::parsers::BelongsTo;
7use crate::util::BELONGS_TO_NOTE;
8
9pub fn parse_belongs_to(name: Ident, input: ParseStream) -> Result<BelongsTo> {
10    if input.is_empty() {
11        return Err(syn::Error::new(
12            name.span(),
13            format!(
14                "unexpected end of input, expected parentheses\n\
15                 help: The correct format looks like `#[diesel({BELONGS_TO_NOTE})]`"
16            ),
17        ));
18    }
19
20    let content;
21    parenthesized!(content in input);
22
23    let parent = if content.peek(Ident) {
24        let name: Ident = content.parse()?;
25
26        if name == "parent" {
27            let lit_str = parse_eq_and_lit_str(name, &content, BELONGS_TO_NOTE)?;
28            lit_str.parse()?
29        } else {
30            LitStr::new(&name.to_string(), name.span()).parse()?
31        }
32    } else {
33        content.parse()?
34    };
35
36    let mut foreign_key = None;
37
38    if content.peek(Comma) {
39        content.parse::<Comma>()?;
40
41        let name: Ident = content.parse()?;
42
43        if name != "foreign_key" {
44            return Err(syn::Error::new(name.span(), "expected `foreign_key`"));
45        }
46
47        let lit_str = parse_eq_and_lit_str(name, &content, BELONGS_TO_NOTE)?;
48        foreign_key = Some(lit_str.parse()?);
49    }
50
51    Ok(BelongsTo {
52        parent,
53        foreign_key,
54    })
55}