From bb86830db45fe61f885a95dd8e66f3a1c3ac85f8 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 14 Jul 2020 10:08:30 +0200 Subject: [PATCH] Create a new trait --- src/api_data/account_export_api.rs | 1 + src/api_data/entities_constructor.rs | 16 ++++++++++++++++ src/api_data/mod.rs | 3 ++- src/api_data/survey_response_api.rs | 11 +++++------ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 src/api_data/entities_constructor.rs diff --git a/src/api_data/account_export_api.rs b/src/api_data/account_export_api.rs index 2fd47fe..fee458a 100644 --- a/src/api_data/account_export_api.rs +++ b/src/api_data/account_export_api.rs @@ -8,6 +8,7 @@ use serde::Serialize; use crate::api_data::comment_api::CommentAPI; use crate::api_data::conversation_api::ConversationAPI; use crate::api_data::conversation_message_api::ConversationMessageAPI; +use crate::api_data::entities_constructor::EntitiesConstructor; use crate::api_data::friend_api::FriendAPI; use crate::api_data::group_api::GroupApi; use crate::api_data::movie_api::MovieAPI; diff --git a/src/api_data/entities_constructor.rs b/src/api_data/entities_constructor.rs new file mode 100644 index 0000000..ac92df6 --- /dev/null +++ b/src/api_data/entities_constructor.rs @@ -0,0 +1,16 @@ +//! # Entities constructor +//! +//! This trait simplify the construction of new items + +pub trait EntitiesConstructor { + type Item; + + /// Turn a server item into an API entry + fn new(i: &Self::Item) -> Self; + + /// Parse a list of it + fn for_list(l: &Vec) -> Vec + where Self: std::marker::Sized { + l.iter().map(Self::new).collect() + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 4d25b0c..fc7039d 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -49,4 +49,5 @@ pub mod res_get_security_questions; pub mod res_check_security_answers; pub mod account_export_api; pub mod user_like_api; -pub mod survey_response_api; \ No newline at end of file +pub mod survey_response_api; +pub mod entities_constructor; \ No newline at end of file diff --git a/src/api_data/survey_response_api.rs b/src/api_data/survey_response_api.rs index 930eb9c..abd1fc6 100644 --- a/src/api_data/survey_response_api.rs +++ b/src/api_data/survey_response_api.rs @@ -3,6 +3,7 @@ //! @author Pierre Hubert use serde::Serialize; +use crate::api_data::entities_constructor::EntitiesConstructor; use crate::data::survey_response::SurveyResponse; #[derive(Serialize)] @@ -15,8 +16,10 @@ pub struct SurveyResponseAPI { choiceID: u64, } -impl SurveyResponseAPI { - pub fn new(r: &SurveyResponse) -> SurveyResponseAPI { +impl EntitiesConstructor for SurveyResponseAPI { + type Item = SurveyResponse; + + fn new(r: &Self::Item) -> Self { SurveyResponseAPI { id: r.id, time_sent: r.time_sent, @@ -25,8 +28,4 @@ impl SurveyResponseAPI { choiceID: r.choice_id, } } - - pub fn for_list(l: &Vec) -> Vec { - l.iter().map(Self::new).collect() - } } \ No newline at end of file