1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Create a new trait

This commit is contained in:
Pierre HUBERT 2020-07-14 10:08:30 +02:00
parent 161e969966
commit bb86830db4
4 changed files with 24 additions and 7 deletions

View File

@ -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;

View File

@ -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<Self::Item>) -> Vec<Self>
where Self: std::marker::Sized {
l.iter().map(Self::new).collect()
}
}

View File

@ -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;
pub mod survey_response_api;
pub mod entities_constructor;

View File

@ -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<SurveyResponse>) -> Vec<Self> {
l.iter().map(Self::new).collect()
}
}