generic_relation_helpers 0.2.0

Traits et helpers génériques pour jointures parent/enfant
Documentation
// ✅ APRÈS — plus d'imports projet, uniquement std
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;
}


// ── Implémentation par défaut dans le package ──
pub trait BuildFromSon: HasFields + Sized {
    fn build(fields: HashMap<String,Value>) -> Self;
     // ← build() reste à implémenter car le package
    //   ne connaît pas les noms des propriétés de la struc
}

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))
    }

}


// ── Implémentation par défaut dans le package ──
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()
    }
}