RagEntity

Trait RagEntity 

Source
pub trait RagEntity:
    CandidType
    + Serialize
    + Clone {
    // Required methods
    fn entity_type() -> &'static str
       where Self: Sized;
    fn entity_id(&self) -> String;
    fn to_context_map(&self) -> Vec<(String, String)>;
    fn relationships(&self) -> Vec<EntityRelationship>;

    // Provided methods
    fn to_text(&self) -> String { ... }
    fn to_summary(&self, max_length: usize) -> String { ... }
}
Expand description

Trait that marks a struct as a RAG entity

Implement this trait for your canister data structures to enable automatic context building and relationship mapping.

§Example

use contrag_core::prelude::*;
use candid::{CandidType, Deserialize};
use serde::Serialize;
 
#[derive(CandidType, Deserialize, Serialize, Clone)]
pub struct User {
    pub id: String,
    pub name: String,
    pub email: String,
}
 
impl RagEntity for User {
    fn entity_type() -> &'static str {
        "User"
    }
     
    fn entity_id(&self) -> String {
        self.id.clone()
    }
     
    fn to_context_map(&self) -> Vec<(String, String)> {
        vec![
            ("id".to_string(), self.id.clone()),
            ("name".to_string(), self.name.clone()),
            ("email".to_string(), self.email.clone()),
        ]
    }
     
    fn relationships(&self) -> Vec<EntityRelationship> {
        vec![]
    }
}

Required Methods§

Source

fn entity_type() -> &'static str
where Self: Sized,

Returns the entity type identifier

Source

fn entity_id(&self) -> String

Returns the unique identifier for this entity instance

Source

fn to_context_map(&self) -> Vec<(String, String)>

Converts the entity to a flat key-value representation

Use dot notation for nested fields: “profile.age”

Source

fn relationships(&self) -> Vec<EntityRelationship>

Returns relationships to other entities

Provided Methods§

Source

fn to_text(&self) -> String

Converts the entity to a human-readable text representation

Override this for custom formatting. Default implementation joins the context map entries.

Source

fn to_summary(&self, max_length: usize) -> String

Returns a summary of the entity (first N characters)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§