Struct AgentDataBuilder

Source
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

Source

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 time
  • mcp_level: None
  • identity_verified: false
  • security_audit_passed: false
  • open_source: false
  • All numeric fields: 0
Source

pub fn created_at(self, created_at: DateTime<Utc>) -> Self

Sets the creation timestamp.

Source

pub fn mcp_level(self, level: u8) -> Self

Sets the MCP level.

Source

pub fn identity_verified(self, verified: bool) -> Self

Sets whether the agent’s identity is verified.

Source

pub fn security_audit_passed(self, passed: bool) -> Self

Sets whether the agent has passed security audit.

Source

pub fn open_source(self, is_open_source: bool) -> Self

Sets whether the agent is open source.

Source

pub fn total_interactions(self, count: u32) -> Self

Sets the total number of interactions.

Source

pub fn total_reviews(self, count: u32) -> Self

Sets the total number of reviews.

Source

pub fn average_rating(self, rating: f64) -> Self

Sets the average rating.

Source

pub fn positive_reviews(self, count: u32) -> Self

Sets the number of positive reviews.

Source

pub fn negative_reviews(self, count: u32) -> Self

Sets the number of negative reviews.

Source

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();
Source

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

Source§

fn clone(&self) -> AgentDataBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AgentDataBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.