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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::Analysis;
use proc_macro2::TokenStream;
use quote::quote;
/// Code generator for FromRow trait implementation.
pub struct FromRowCodegen<'a> {
analysis: &'a Analysis<'a>,
}
impl<'a> FromRowCodegen<'a> {
/// Creates a new code generator for FromRow trait implementation.
pub fn new(analysis: &'a Analysis<'a>) -> Self {
Self { analysis }
}
/// Generates the `FromRow` trait implementation.
///
/// This implementation is generic over any sqlx Row type, allowing models
/// to work with any database backend that has the `Dialect` trait
/// implemented.
pub fn generate(self) -> TokenStream {
let base_struct_ident = &self.analysis.ident;
// Generate where-clause bounds for each field's decode type
let field_bounds = self.analysis.column_fields.iter().map(|field| {
let decode_ty = field.r#as.as_ref().unwrap_or(&field.ty);
quote! {
#decode_ty: ::sqlx::decode::Decode<'r, R::Database> + ::sqlx::Type<R::Database>
}
});
// Generate column field assignments
let field_assignments = self.analysis.column_fields.iter().map(|field| {
let field_ident = &field.ident;
let column_name = field.ident.to_string();
match &field.r#as {
Some(db_ty) => {
// Field has `as` attribute, need to convert from intermediate type
let rust_ty = &field.ty;
quote! {
#field_ident: {
let db_value: #db_ty = row.try_get(#column_name)?;
let value_str = format!("{:?}", &db_value);
<#rust_ty>::try_from(db_value).map_err(|e| {
::sqlx::Error::Decode(Box::new(::fabrique::Error::Conversion {
field: #column_name.to_string(),
from: stringify!(#db_ty),
to: stringify!(#rust_ty),
value: value_str,
reason: e.to_string(),
direction: ::fabrique::error::ConversionDirection::FromDb,
}))
})?
}
}
}
None => {
// No conversion needed, read directly
quote! {
#field_ident: row.try_get(#column_name)?
}
}
}
});
quote! {
impl<'r, R: ::sqlx::Row> ::sqlx::FromRow<'r, R> for #base_struct_ident
where
&'r str: ::sqlx::ColumnIndex<R>,
#(#field_bounds,)*
{
fn from_row(row: &'r R) -> ::sqlx::Result<Self> {
use ::sqlx::Row;
Ok(Self {
#(#field_assignments),*
})
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use syn::parse_quote;
#[test]
fn test_generate_from_row() {
let input = parse_quote! { struct Anvil { id: String } };
let analysis = Analysis::from(&input).unwrap();
let codegen = FromRowCodegen::new(&analysis);
let result = codegen.generate();
assert_eq!(
result.to_string(),
quote! {
impl<'r, R: ::sqlx::Row> ::sqlx::FromRow<'r, R> for Anvil
where
&'r str: ::sqlx::ColumnIndex<R>,
String: ::sqlx::decode::Decode<'r, R::Database> + ::sqlx::Type<R::Database>,
{
fn from_row(row: &'r R) -> ::sqlx::Result<Self> {
use ::sqlx::Row;
Ok(Self {
id: row.try_get("id")?
})
}
}
}
.to_string()
);
}
#[test]
fn test_generate_from_row_with_type_conversion() {
let input = parse_quote! {
struct Account {
id: String,
#[fabrique(as = "String")]
status: Status,
}
};
let analysis = Analysis::from(&input).unwrap();
let codegen = FromRowCodegen::new(&analysis);
let result = codegen.generate();
assert_eq!(
result.to_string(),
quote! {
impl<'r, R: ::sqlx::Row> ::sqlx::FromRow<'r, R> for Account
where
&'r str: ::sqlx::ColumnIndex<R>,
String: ::sqlx::decode::Decode<'r, R::Database> + ::sqlx::Type<R::Database>,
String: ::sqlx::decode::Decode<'r, R::Database> + ::sqlx::Type<R::Database>,
{
fn from_row(row: &'r R) -> ::sqlx::Result<Self> {
use ::sqlx::Row;
Ok(Self {
id: row.try_get("id")?,
status: {
let db_value: String = row.try_get("status")?;
let value_str = format!("{:?}", &db_value);
<Status>::try_from(db_value).map_err(|e| {
::sqlx::Error::Decode(Box::new(::fabrique::Error::Conversion {
field: "status".to_string(),
from: stringify!(String),
to: stringify!(Status),
value: value_str,
reason: e.to_string(),
direction: ::fabrique::error::ConversionDirection::FromDb,
}))
})?
}
})
}
}
}
.to_string()
);
}
}