From d9645e7f5c5962e79c5bd7a5416213dc7ad53a2b Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Fri, 2 Jun 2023 18:51:48 +0200 Subject: [PATCH] Refactor --- src/types.rs | 26 +++++++++++++------------- src/updater.rs | 38 +++++++++++++++++--------------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/types.rs b/src/types.rs index e4f02fb..ca2dd47 100644 --- a/src/types.rs +++ b/src/types.rs @@ -6,7 +6,7 @@ use tokio_postgres::Client; use crate::utils::{fix_annotation_text, parse_lang, remove_wrong_chars}; pub trait FromVecExpression { - fn from_vec_expression(value: &Vec) -> T; + fn from_vec_expression(value: &[Expression]) -> T; } #[async_trait] @@ -31,7 +31,7 @@ pub struct Author { } impl FromVecExpression for Author { - fn from_vec_expression(value: &Vec) -> Author { + fn from_vec_expression(value: &[Expression]) -> Author { Author { id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -107,7 +107,7 @@ pub struct Book { } impl FromVecExpression for Book { - fn from_vec_expression(value: &Vec) -> Book { + fn from_vec_expression(value: &[Expression]) -> Book { Book { id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -197,7 +197,7 @@ pub struct BookAuthor { } impl FromVecExpression for BookAuthor { - fn from_vec_expression(value: &Vec) -> BookAuthor { + fn from_vec_expression(value: &[Expression]) -> BookAuthor { BookAuthor { book_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -270,7 +270,7 @@ pub struct Translator { } impl FromVecExpression for Translator { - fn from_vec_expression(value: &Vec) -> Translator { + fn from_vec_expression(value: &[Expression]) -> Translator { Translator { book_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -347,7 +347,7 @@ pub struct Sequence { } impl FromVecExpression for Sequence { - fn from_vec_expression(value: &Vec) -> Sequence { + fn from_vec_expression(value: &[Expression]) -> Sequence { Sequence { id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -410,7 +410,7 @@ pub struct SequenceInfo { } impl FromVecExpression for SequenceInfo { - fn from_vec_expression(value: &Vec) -> SequenceInfo { + fn from_vec_expression(value: &[Expression]) -> SequenceInfo { SequenceInfo { book_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -497,7 +497,7 @@ pub struct BookAnnotation { } impl FromVecExpression for BookAnnotation { - fn from_vec_expression(value: &Vec) -> BookAnnotation { + fn from_vec_expression(value: &[Expression]) -> BookAnnotation { BookAnnotation { book_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -572,7 +572,7 @@ pub struct BookAnnotationPic { } impl FromVecExpression for BookAnnotationPic { - fn from_vec_expression(value: &Vec) -> BookAnnotationPic { + fn from_vec_expression(value: &[Expression]) -> BookAnnotationPic { BookAnnotationPic { book_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -625,7 +625,7 @@ pub struct AuthorAnnotation { } impl FromVecExpression for AuthorAnnotation { - fn from_vec_expression(value: &Vec) -> AuthorAnnotation { + fn from_vec_expression(value: &[Expression]) -> AuthorAnnotation { AuthorAnnotation { author_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -700,7 +700,7 @@ pub struct AuthorAnnotationPic { } impl FromVecExpression for AuthorAnnotationPic { - fn from_vec_expression(value: &Vec) -> AuthorAnnotationPic { + fn from_vec_expression(value: &[Expression]) -> AuthorAnnotationPic { AuthorAnnotationPic { author_id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -753,7 +753,7 @@ pub struct Genre { } impl FromVecExpression for Genre { - fn from_vec_expression(value: &Vec) -> Genre { + fn from_vec_expression(value: &[Expression]) -> Genre { Genre { id: match &value[0] { sql_parse::Expression::Integer(v) => v.0, @@ -826,7 +826,7 @@ pub struct BookGenre { } impl FromVecExpression for BookGenre { - fn from_vec_expression(value: &Vec) -> BookGenre { + fn from_vec_expression(value: &[Expression]) -> BookGenre { BookGenre { book_id: match &value[1] { sql_parse::Expression::Integer(v) => v.0, diff --git a/src/updater.rs b/src/updater.rs index e579727..4b2a1fb 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -140,32 +140,28 @@ where let mut issues = Vec::new(); let ast = parse_statement(&line, &mut issues, &parse_options); - match ast { - Some(Statement::InsertReplace( - i @ InsertReplace { - type_: InsertReplaceType::Insert(_), - .. - }, - )) => { - for value in i.values.into_iter() { - for t_value in value.1.into_iter() { - let value = T::from_vec_expression(&t_value); - let client = pool.get().await.unwrap(); + if let Some(Statement::InsertReplace( + i @ InsertReplace { + type_: InsertReplaceType::Insert(_), + .. + }, + )) = ast { + for value in i.values.into_iter() { + for t_value in value.1.into_iter() { + let value = T::from_vec_expression(&t_value); + let client = pool.get().await.unwrap(); - match value.update(&client, source_id).await { - Ok(_) => { - // log::info!("{:?}", value); - - } - Err(err) => { - log::error!("Update error: {:?} : {:?}", value, err); - return Err(err) - }, + match value.update(&client, source_id).await { + Ok(_) => { + // log::info!("{:?}", value); } + Err(err) => { + log::error!("Update error: {:?} : {:?}", value, err); + return Err(err) + }, } } } - _ => (), } }