-
-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathcomposites.rs
65 lines (56 loc) · 1.88 KB
/
composites.rs
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
use lifetimes::extract_borrowed_lifetimes;
use proc_macro2::Span;
use syn::{
punctuated::Punctuated, Error, GenericParam, Generics, Ident, Lifetime, Path, PathSegment,
Type, TypeParamBound,
};
use crate::{case::RenameRule, lifetimes, overrides::Overrides};
pub struct NamedField {
pub name: String,
pub ident: Ident,
pub type_: Type,
pub borrowed_lifetimes: Vec<Lifetime>,
}
impl NamedField {
pub fn parse(raw: &syn::Field, rename_all: Option<RenameRule>) -> Result<NamedField, Error> {
let overrides = Overrides::extract(&raw.attrs, false)?;
let ident = raw.ident.as_ref().unwrap().clone();
let borrowed_lifetimes = extract_borrowed_lifetimes(raw, &overrides);
// field level name override takes precedence over container level rename_all override
let name = match overrides.name {
Some(n) => n,
None => {
let name = ident.to_string();
let stripped = name.strip_prefix("r#").map(String::from).unwrap_or(name);
match rename_all {
Some(rule) => rule.apply_to_field(&stripped),
None => stripped,
}
}
};
Ok(NamedField {
name,
ident,
type_: raw.ty.clone(),
borrowed_lifetimes,
})
}
}
pub(crate) fn append_generic_bound(mut generics: Generics, bound: &TypeParamBound) -> Generics {
for param in &mut generics.params {
if let GenericParam::Type(param) = param {
param.bounds.push(bound.to_owned())
}
}
generics
}
pub(crate) fn new_derive_path(last: PathSegment) -> Path {
let mut path = Path {
leading_colon: None,
segments: Punctuated::new(),
};
path.segments
.push(Ident::new("postgres_types", Span::call_site()).into());
path.segments.push(last);
path
}