Skip to content

Commit 4b00dd5

Browse files
committed
code refactoring
1 parent 17c69aa commit 4b00dd5

File tree

1 file changed

+150
-117
lines changed

1 file changed

+150
-117
lines changed

src/mapper_entry.rs

+150-117
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use syn::{punctuated::Punctuated, spanned::Spanned, Attribute, Expr, Lit, Meta, Token};
1+
use syn::{
2+
punctuated::Punctuated, spanned::Spanned, Attribute, Expr, ExprArray, ExprLit, ExprTuple, Lit,
3+
Meta, Token,
4+
};
25

36
use crate::utils;
47

@@ -101,96 +104,37 @@ impl MapperEntry {
101104
if let Expr::Lit(expr) = &metaname.value {
102105
if keyname.eq_ignore_ascii_case(DTO) {
103106
//we should read the string value
104-
if let Lit::Str(lit_str) = &expr.lit {
105-
//println!("{}={}",keyname,lit_str.value())
106-
mapper_entry.dto = lit_str.value();
107-
dto_prop = Some(lit_str.value());
108-
}
107+
Self::parse_dto_attribute(&mut mapper_entry, &expr);
108+
dto_prop = Some(mapper_entry.dto.to_string());
109109
}
110110

111111
//
112112
if keyname.eq_ignore_ascii_case(WITHOUT_BUILDER) {
113-
if let Lit::Bool(lit_bool) = &expr.lit {
114-
mapper_entry.no_builder = lit_bool.value();
115-
}
113+
Self::parse_no_builder_attribute(&mut mapper_entry, &expr);
116114
}
117115
}
118116

119117
if let Expr::Array(expr_arr) = &metaname.value {
120118
//println!("{} array has {} elements",keyname,expr_arr.elems.iter().clone().count());
121-
122119
if keyname.eq_ignore_ascii_case(MAP) {
123120
//map is a vec of tuples such as map=[("f1",true),("f2",false)]
124-
let map_tuples = Self::parse_array_of_tuple(expr_arr);
125-
//println!("{}={:?}",keyname,map_tuple);
126-
mapper_entry.map = map_tuples.iter().map(MapValue::new).collect();
127-
if mapper_entry
128-
.map
129-
.iter()
130-
.filter(|&m_val| utils::isblank(&m_val.from_field))
131-
.count()
132-
> 0
133-
{
134-
panic!("`{}` attribute must not be blank", MAP);
135-
};
121+
Self::parse_map_attribute(&mut mapper_entry, expr_arr);
136122
}
137123

138124
if keyname.eq_ignore_ascii_case(NEW_FIELDS) {
139-
mapper_entry.new_fields = Self::parse_array_of_new_fields(expr_arr);
140-
//println!("{:?}",mapper_entry.new_fields);
141-
if mapper_entry.new_fields.len() == 0 {
142-
panic!(
143-
"`{}` attribute must not be empty or have odd number of elements",
144-
NEW_FIELDS
145-
);
146-
};
125+
Self::parse_new_fields_attribute(&mut mapper_entry, expr_arr);
147126
}
148127

149128
if keyname.eq_ignore_ascii_case(IGNORE) {
150129
//ignore is a vec of string such as ignore=["val1","val2"]
151-
let ignore_arr = Self::parse_array_of_string(expr_arr);
152-
//println!("{}={:?}",keyname, ignore_arr);
153-
//check if matt attribute is blank
154-
if ignore_arr
155-
.iter()
156-
.filter(|&text| utils::isblank(text))
157-
.count()
158-
> 0
159-
{
160-
panic!("`{}` attribute must not be blank", IGNORE);
161-
};
162-
mapper_entry.ignore = ignore_arr;
130+
Self::parse_ignore_attribute(&mut mapper_entry, expr_arr);
163131
}
164132
}
165133

166134
if let Expr::Tuple(tuple_expr) = &metaname.value {
167135
//println!("keyname {} is Tuple of literal value",keyname);
168136
if keyname.eq_ignore_ascii_case(DERIVE) {
169-
let derive_items = tuple_expr
170-
.elems
171-
.iter()
172-
.map(|elem_expr| {
173-
if let Expr::Path(path_exp) = &elem_expr {
174-
let ident = path_exp.path.get_ident().unwrap();
175-
let derive_obj: String = ident.to_string();
176-
derive_obj
177-
} else {
178-
"".to_string()
179-
}
180-
})
181-
.collect::<Vec<String>>();
182-
183-
//Adding a builder by default if property isn't explicitly set to true
184-
if !mapper_entry.no_builder {
185-
mapper_entry.derive.push("Builder".into());
186-
}
187-
188-
derive_items
189-
.iter()
190-
.filter(|&val| !val.eq("Default"))
191-
.map(|val| val.clone())
192-
.for_each(|val| mapper_entry.derive.push(val));
193-
//mapper_entry.derive = derive_items;
137+
Self::parse_derive_attribute(&mut mapper_entry, tuple_expr);
194138
}
195139
}
196140
}
@@ -210,6 +154,87 @@ impl MapperEntry {
210154
}
211155
}
212156

157+
fn parse_no_builder_attribute(mapper_entry: &mut MapperEntry, expr: &&ExprLit) {
158+
if let Lit::Bool(lit_bool) = &expr.lit {
159+
mapper_entry.no_builder = lit_bool.value();
160+
}
161+
}
162+
163+
fn parse_derive_attribute(mapper_entry: &mut MapperEntry, tuple_expr: &ExprTuple) {
164+
let derive_items = tuple_expr
165+
.elems
166+
.iter()
167+
.map(|elem_expr| {
168+
if let Expr::Path(path_exp) = &elem_expr {
169+
let ident = path_exp.path.get_ident().unwrap();
170+
let derive_obj: String = ident.to_string();
171+
derive_obj
172+
} else {
173+
"".to_string()
174+
}
175+
})
176+
.collect::<Vec<String>>();
177+
178+
//Adding a builder by default if property isn't explicitly set to true
179+
if !mapper_entry.no_builder {
180+
mapper_entry.derive.push("Builder".into());
181+
}
182+
183+
derive_items
184+
.iter()
185+
.filter(|&val| !val.eq("Default"))
186+
.map(|val| val.clone())
187+
.for_each(|val| mapper_entry.derive.push(val));
188+
}
189+
190+
fn parse_dto_attribute(mapper_entry: &mut MapperEntry, expr: &ExprLit) {
191+
if let Lit::Str(lit_str) = &expr.lit {
192+
//println!("{}={}",keyname,lit_str.value())
193+
mapper_entry.dto = lit_str.value();
194+
}
195+
}
196+
197+
fn parse_ignore_attribute(mapper_entry: &mut MapperEntry, expr_arr: &ExprArray) {
198+
let ignore_arr = Self::parse_array_of_string(expr_arr);
199+
//println!("{}={:?}",keyname, ignore_arr);
200+
//check if matt attribute is blank
201+
if ignore_arr
202+
.iter()
203+
.filter(|&text| utils::isblank(text))
204+
.count()
205+
> 0
206+
{
207+
panic!("`{}` attribute must not be blank", IGNORE);
208+
};
209+
mapper_entry.ignore = ignore_arr;
210+
}
211+
212+
fn parse_new_fields_attribute(mapper_entry: &mut MapperEntry, expr_arr: &ExprArray) {
213+
mapper_entry.new_fields = Self::parse_array_of_new_fields(expr_arr);
214+
//println!("{:?}",mapper_entry.new_fields);
215+
if mapper_entry.new_fields.len() == 0 {
216+
panic!(
217+
"`{}` attribute must not be empty or have odd number of elements",
218+
NEW_FIELDS
219+
);
220+
};
221+
}
222+
223+
fn parse_map_attribute(mapper_entry: &mut MapperEntry, expr_arr: &ExprArray) {
224+
let map_tuples = Self::parse_array_of_tuple(expr_arr);
225+
//println!("{}={:?}",keyname,map_tuple);
226+
mapper_entry.map = map_tuples.iter().map(MapValue::new).collect();
227+
if mapper_entry
228+
.map
229+
.iter()
230+
.filter(|&m_val| utils::isblank(&m_val.from_field))
231+
.count()
232+
> 0
233+
{
234+
panic!("`{}` attribute must not be blank", MAP);
235+
};
236+
}
237+
213238
fn parse_array_of_tuple(expr_arr: &syn::ExprArray) -> Vec<MapTuple> {
214239
let mut vec_tuple: Vec<(String, bool)> = Vec::new();
215240

@@ -247,65 +272,73 @@ impl MapperEntry {
247272
let mut vec_tuple: Vec<NewField> = Vec::new();
248273

249274
for elem in expr_arr.elems.iter() {
250-
if let Expr::Tuple(el_exp) = elem {
251-
//println!("{} content is a Tuple",keyname);
275+
Self::process_new_fields(&mut vec_tuple, elem);
276+
}
252277

253-
let mut prev_value: Option<String> = None;
278+
return vec_tuple;
279+
}
254280

255-
for (position, content_expr) in el_exp.elems.iter().enumerate() {
256-
if let Expr::Lit(content_lit) = content_expr {
257-
if let Lit::Str(content) = &content_lit.lit {
258-
//print!("valueStr={}",content.value());
259-
if let Some(str_val) =
260-
utils::remove_white_space(&content.value()).into()
261-
{
262-
//Read each 2 element and add it to the vec_tuple. We split elements by 2
263-
match position % 2 {
264-
0 => {
265-
prev_value = Some(str_val);
266-
}
267-
_ => {
268-
// current position is not an even number and is considered a 2nd recurrint element in the series
269-
if prev_value.is_some() {
270-
let field_decl = prev_value.clone().unwrap();
271-
//Parse fieldname and type
272-
if let Some(colon_position) = field_decl.find(":") {
273-
if colon_position == 0 {
274-
panic!("`:` cannot be the first character. Need to specify new fieldname for struct");
275-
}
276-
if colon_position == field_decl.len() - 1 {
277-
panic!("Need to specify a type for the fieldname after `:`");
278-
}
279-
280-
let field_name =
281-
&field_decl.as_str()[..colon_position];
282-
let field_type =
283-
&field_decl.as_str()[colon_position + 1..];
284-
285-
vec_tuple.push(NewField::new(
286-
field_name,
287-
field_type,
288-
str_val.as_str(),
289-
));
290-
//reset prev value
291-
prev_value = None;
292-
continue;
293-
}
294-
295-
panic!("Missing `:` character for field declaration");
281+
fn process_new_fields(mut vec_tuple: &mut Vec<NewField>, elem: &Expr) {
282+
if let Expr::Tuple(el_exp) = elem {
283+
//println!("{} content is a Tuple",keyname);
284+
285+
let mut prev_value: Option<String> = None;
286+
287+
for (position, content_expr) in el_exp.elems.iter().enumerate() {
288+
if let Expr::Lit(content_lit) = content_expr {
289+
if let Lit::Str(content) = &content_lit.lit {
290+
//print!("valueStr={}",content.value());
291+
if let Some(str_val) = utils::remove_white_space(&content.value()).into() {
292+
//Read each 2 element and add it to the vec_tuple. We split elements by 2
293+
match position % 2 {
294+
0 => {
295+
prev_value = Some(str_val);
296+
}
297+
_ => {
298+
// current position is not an even number and is considered a 2nd recurrint element in the series
299+
if prev_value.is_some() {
300+
let field_decl = prev_value.clone().unwrap();
301+
//Parse fieldname and type
302+
if let Some(colon_position) = field_decl.find(":") {
303+
Self::insert_next_field_value(
304+
&mut vec_tuple,
305+
str_val,
306+
&field_decl,
307+
&colon_position,
308+
);
309+
//reset prev value
310+
prev_value = None;
311+
continue;
296312
}
313+
314+
panic!("Missing `:` character for field declaration");
297315
}
298316
}
299-
} //Some(str_val)
300-
} //Lit::Str
301-
} //Expr::Lit
317+
}
318+
}
319+
}
302320
}
303-
304-
//println!("");
305321
}
306322
}
323+
}
307324

308-
return vec_tuple;
325+
fn insert_next_field_value(
326+
vec_tuple: &mut Vec<NewField>,
327+
str_val: String,
328+
field_decl: &String,
329+
colon_position: &usize,
330+
) {
331+
if *colon_position == 0 {
332+
panic!("`:` cannot be the first character. Need to specify new fieldname for struct");
333+
}
334+
if *colon_position == field_decl.len() - 1 {
335+
panic!("Need to specify a type for the fieldname after `:`");
336+
}
337+
338+
let field_name = &field_decl.as_str()[..*colon_position];
339+
let field_type = &field_decl.as_str()[*colon_position + 1..];
340+
341+
vec_tuple.push(NewField::new(field_name, field_type, str_val.as_str()));
309342
}
310343

311344
fn parse_array_of_string(expr_arr: &syn::ExprArray) -> Vec<String> {

0 commit comments

Comments
 (0)