use std::collections::HashMap;
use serde::Serialize;
use serde_json::{Value, json};
use crate::helpers::builder_hasmap_for_fields::build_hashmap_from_fields;
pub trait HasMatchKey {
fn get_match_key(&self) -> &str;
}
pub trait HasFields {
fn get_fields(&self,name:&str) -> Value;
fn field_names()->Vec<&'static str>;
}
pub trait GetValuePropertyOfKeyParent {
fn get_value(&self) -> &str;
}
pub trait GetValuePKFieldForSourceSecondary {
fn get_FK_field_of_source_secondary(&self) -> &str;
fn get_field_of_source_secondary(&self) -> &str;
}
pub trait BuildFromSon: HasFields + Sized {
fn build(fields: HashMap<String,Value>) -> Self;
}
pub trait BuildFromParent {
fn build(fields: HashMap<String, Value>) -> Self;
}
pub trait GetValueFieldForSourceParent<TEnfant> :HasFields
where
TEnfant: Serialize
{
fn enfants_key() -> &'static str;
fn get_values_of_property(&self,enfants:Vec<TEnfant>) -> HashMap<String,Value>
where
Self: Sized
{
build_hashmap_from_fields(self,Self::enfants_key(),json!(enfants))
}
}
pub trait GetValueFieldForSourceSecondary: HasFields {
fn get_values_of_property(&self) -> HashMap<String, Value> {
Self::field_names()
.iter()
.map(|&name| (name.to_string(), self.get_fields(name)))
.collect()
}
}