pub struct AgentDataBuilder { /* private fields */ }
Expand description
Builder for constructing AgentData
instances with a fluent API.
The builder provides a convenient way to create AgentData
instances with
validation and sensible defaults. All fields except the DID are optional.
§Examples
§Basic Usage
use reputation_types::AgentDataBuilder;
// Simple agent with minimal data
let agent = AgentDataBuilder::new("did:example:123")
.build()
.unwrap();
// Agent with reviews
let agent = AgentDataBuilder::new("did:example:123")
.total_interactions(150)
.with_reviews(100, 4.5)
.mcp_level(2)
.identity_verified(true)
.build()
.unwrap();
§Using the Convenience Constructor
use reputation_types::AgentData;
let agent = AgentData::builder("did:example:456")
.total_interactions(300)
.with_reviews(250, 4.8)
.mcp_level(3)
.identity_verified(true)
.security_audit_passed(true)
.open_source(true)
.build()
.unwrap();
§Setting Individual Review Counts
use reputation_types::AgentDataBuilder;
let agent = AgentDataBuilder::new("did:example:789")
.positive_reviews(180)
.negative_reviews(20)
.total_interactions(500)
.build()
.unwrap();
// The builder will automatically calculate:
// - total_reviews: 200 (180 + 20)
// - average_rating: 4.6 (based on positive/negative ratio)
§Error Handling
use reputation_types::{AgentDataBuilder, BuilderError};
// Empty DID returns an error
let result = AgentDataBuilder::new("")
.build();
assert!(matches!(result, Err(BuilderError::InvalidField(_))));
// Invalid rating returns an error
let result = AgentDataBuilder::new("did:example:123")
.average_rating(6.0)
.build();
assert!(matches!(result, Err(BuilderError::InvalidField(_))));
Implementations§
Source§impl AgentDataBuilder
impl AgentDataBuilder
Sourcepub fn new(did: impl Into<String>) -> Self
pub fn new(did: impl Into<String>) -> Self
Creates a new builder with the required DID field.
All other fields are initialized with sensible defaults:
created_at
: Current UTC timemcp_level
: Noneidentity_verified
: falsesecurity_audit_passed
: falseopen_source
: false- All numeric fields: 0
Sourcepub fn created_at(self, created_at: DateTime<Utc>) -> Self
pub fn created_at(self, created_at: DateTime<Utc>) -> Self
Sets the creation timestamp.
Sourcepub fn identity_verified(self, verified: bool) -> Self
pub fn identity_verified(self, verified: bool) -> Self
Sets whether the agent’s identity is verified.
Sourcepub fn security_audit_passed(self, passed: bool) -> Self
pub fn security_audit_passed(self, passed: bool) -> Self
Sets whether the agent has passed security audit.
Sourcepub fn open_source(self, is_open_source: bool) -> Self
pub fn open_source(self, is_open_source: bool) -> Self
Sets whether the agent is open source.
Sourcepub fn total_interactions(self, count: u32) -> Self
pub fn total_interactions(self, count: u32) -> Self
Sets the total number of interactions.
Sourcepub fn total_reviews(self, count: u32) -> Self
pub fn total_reviews(self, count: u32) -> Self
Sets the total number of reviews.
Sourcepub fn average_rating(self, rating: f64) -> Self
pub fn average_rating(self, rating: f64) -> Self
Sets the average rating.
Sourcepub fn positive_reviews(self, count: u32) -> Self
pub fn positive_reviews(self, count: u32) -> Self
Sets the number of positive reviews.
Sourcepub fn negative_reviews(self, count: u32) -> Self
pub fn negative_reviews(self, count: u32) -> Self
Sets the number of negative reviews.
Sourcepub fn with_reviews(self, total: u32, average_rating: f64) -> Self
pub fn with_reviews(self, total: u32, average_rating: f64) -> Self
Convenience method to set review data based on total reviews and average rating.
This method automatically calculates:
- Positive and negative review counts based on the average rating
- The average rating is clamped between 1.0 and 5.0
§Examples
use reputation_types::AgentDataBuilder;
let agent = AgentDataBuilder::new("did:example:123")
.total_interactions(150)
.with_reviews(100, 4.5) // 87 positive, 13 negative
.build()
.unwrap();
Sourcepub fn build(self) -> Result<AgentData, BuilderError>
pub fn build(self) -> Result<AgentData, BuilderError>
Builds the AgentData
instance with validation.
§Errors
Returns an error if:
- The DID is empty
- The total reviews don’t match positive + negative reviews (if all are set)
- The average rating is outside the valid range (1.0-5.0)
- The creation date is in the future
Trait Implementations§
Source§impl Clone for AgentDataBuilder
impl Clone for AgentDataBuilder
Source§fn clone(&self) -> AgentDataBuilder
fn clone(&self) -> AgentDataBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more