forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattr.rs
More file actions
76 lines (68 loc) · 2.02 KB
/
Copy pathattr.rs
File metadata and controls
76 lines (68 loc) · 2.02 KB
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
use quote;
use syn;
use std::borrow::Cow;
use util::*;
#[derive(Debug)]
pub struct Attr {
pub column_name: Option<syn::Ident>,
pub field_name: Option<syn::Ident>,
pub ty: syn::Ty,
field_position: usize,
}
impl Attr {
pub fn from_struct_field((index, field): (usize, &syn::Field)) -> Self {
let field_name = field.ident.clone();
let column_name = ident_value_of_attr_with_name(&field.attrs, "column_name")
.cloned()
.or_else(|| field_name.clone());
let ty = field.ty.clone();
Attr {
column_name: column_name,
field_name: field_name,
ty: ty,
field_position: index,
}
}
pub fn name_for_pattern(&self) -> Cow<syn::Ident> {
match self.field_name {
Some(ref name) => Cow::Borrowed(name),
None => Cow::Owned(format!("field_{}", self.field_position).into()),
}
}
fn field_kind(&self) -> &str {
if is_option_ty(&self.ty) {
"option"
} else if self.column_name.is_none() && self.field_name.is_none() {
"bare"
} else {
"regular"
}
}
}
impl quote::ToTokens for Attr {
fn to_tokens(&self, tokens: &mut quote::Tokens) {
tokens.append("{");
if let Some(ref name) = self.field_name {
tokens.append("field_name: ");
name.to_tokens(tokens);
tokens.append(", ");
}
if let Some(ref name) = self.column_name {
tokens.append("column_name: ");
name.to_tokens(tokens);
tokens.append(", ");
}
tokens.append("field_ty: ");
self.ty.to_tokens(tokens);
tokens.append(", ");
tokens.append("field_kind: ");
tokens.append(self.field_kind());
tokens.append(", ");
tokens.append("inner_field_ty: ");
inner_of_option_ty(&self.ty)
.unwrap_or(&self.ty)
.to_tokens(tokens);
tokens.append(", ");
tokens.append("}");
}
}