use crate::error::ApiError;
use crate::agent::profile::metadata_types::AgentMetadata;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AgentRole {
Reader,
#[serde(alias = "Synthesis")]
Writer,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Capability {
Read,
Write,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentIdentity {
pub agent_id: String,
pub role: AgentRole,
pub capabilities: Vec<Capability>,
#[serde(default)]
pub metadata: AgentMetadata,
}
impl AgentIdentity {
pub fn new(agent_id: String, role: AgentRole) -> Self {
let capabilities = match role {
AgentRole::Reader => vec![Capability::Read],
AgentRole::Writer => vec![Capability::Read, Capability::Write],
};
Self {
agent_id,
role,
capabilities,
metadata: AgentMetadata::new(),
}
}
pub fn can_read(&self) -> bool {
self.capabilities.contains(&Capability::Read)
}
pub fn can_write(&self) -> bool {
self.capabilities.contains(&Capability::Write)
}
pub fn verify_read(&self) -> Result<(), ApiError> {
if !self.can_read() {
return Err(ApiError::Unauthorized(format!(
"Agent {} (role: {:?}) cannot read",
self.agent_id, self.role
)));
}
Ok(())
}
pub fn verify_write(&self) -> Result<(), ApiError> {
if !self.can_write() {
return Err(ApiError::Unauthorized(format!(
"Agent {} (role: {:?}) cannot write",
self.agent_id, self.role
)));
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct ValidationResult {
pub agent_id: String,
pub checks: Vec<(String, bool)>,
pub errors: Vec<String>,
}
impl ValidationResult {
pub fn new(agent_id: String) -> Self {
Self {
agent_id,
checks: Vec::new(),
errors: Vec::new(),
}
}
pub fn add_check(&mut self, description: &str, passed: bool) {
self.checks.push((description.to_string(), passed));
}
pub fn add_error(&mut self, error: String) {
self.errors.push(error);
}
pub fn is_valid(&self) -> bool {
self.errors.is_empty() && self.checks.iter().all(|(_, passed)| *passed)
}
pub fn total_checks(&self) -> usize {
self.checks.len()
}
pub fn passed_checks(&self) -> usize {
self.checks.iter().filter(|(_, passed)| *passed).count()
}
}