1#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
2
3use std::fmt::Display;
4use syn::{parse_macro_input, DeriveInput};
5
6mod column;
7mod model;
8
9#[proc_macro_derive(Model, attributes(ensemble, model, validate))]
10pub fn derive_model(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
11 let mut ast = parse_macro_input!(input as DeriveInput);
12 let opts = match deluxe::extract_attributes(&mut ast) {
13 Ok(opts) => opts,
14 Err(e) => return e.into_compile_error().into(),
15 };
16
17 model::r#impl(&ast, opts)
18 .unwrap_or_else(syn::Error::into_compile_error)
19 .into()
20}
21
22#[proc_macro_derive(Column, attributes(builder))]
23pub fn derive_column(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
24 let ast = parse_macro_input!(input as DeriveInput);
25
26 column::r#impl(&ast)
27 .unwrap_or_else(syn::Error::into_compile_error)
28 .into()
29}
30
31#[derive(Clone, Copy)]
32pub(crate) enum Relationship {
33 HasOne,
34 HasMany,
35 BelongsTo,
36 BelongsToMany,
37}
38
39impl Display for Relationship {
40 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(
42 f,
43 "{}",
44 match self {
45 Self::HasOne => "HasOne",
46 Self::HasMany => "HasMany",
47 Self::BelongsTo => "BelongsTo",
48 Self::BelongsToMany => "BelongsToMany",
49 }
50 )
51 }
52}
53
54#[allow(clippy::fallible_impl_from)]
55impl From<String> for Relationship {
56 fn from(value: String) -> Self {
57 match value.as_str() {
58 "HasOne" => Self::HasOne,
59 "HasMany" => Self::HasMany,
60 "BelongsTo" => Self::BelongsTo,
61 "BelongsToMany" => Self::BelongsToMany,
62 _ => panic!("Unknown relationship found."),
63 }
64 }
65}