use crate::error::InvalidArgumentError;
use super::error::PikeBuilderError;
use super::{Agent, AlternateId, Organization, OrganizationMetadata, Role};
#[derive(Clone, Debug, Default)]
pub struct AgentBuilder {
public_key: Option<String>,
org_id: Option<String>,
active: Option<bool>,
metadata: Option<Vec<u8>>,
roles: Option<Vec<String>>,
start_commit_num: Option<i64>,
end_commit_num: Option<i64>,
service_id: Option<String>,
last_updated: Option<i64>,
}
impl AgentBuilder {
pub fn new() -> Self {
AgentBuilder::default()
}
pub fn with_public_key(mut self, public_key: String) -> Self {
self.public_key = Some(public_key);
self
}
pub fn with_org_id(mut self, org_id: String) -> Self {
self.org_id = Some(org_id);
self
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = Some(active);
self
}
pub fn with_metadata(mut self, metadata: Vec<u8>) -> Self {
self.metadata = Some(metadata);
self
}
pub fn with_roles(mut self, roles: Vec<String>) -> Self {
self.roles = Some(roles);
self
}
pub fn with_start_commit_num(mut self, start_commit_num: i64) -> Self {
self.start_commit_num = Some(start_commit_num);
self
}
pub fn with_end_commit_num(mut self, end_commit_num: i64) -> Self {
self.end_commit_num = Some(end_commit_num);
self
}
pub fn with_service_id(mut self, service_id: String) -> Self {
self.service_id = Some(service_id);
self
}
pub fn with_last_updated(mut self, last_updated: i64) -> Self {
self.last_updated = Some(last_updated);
self
}
pub fn build(self) -> Result<Agent, PikeBuilderError> {
let public_key = self
.public_key
.ok_or_else(|| PikeBuilderError::MissingRequiredField("public_key".to_string()))?;
let org_id = self
.org_id
.ok_or_else(|| PikeBuilderError::MissingRequiredField("org_id".to_string()))?;
let active = self
.active
.ok_or_else(|| PikeBuilderError::MissingRequiredField("active".to_string()))?;
let metadata = self.metadata.unwrap_or_default();
let roles = self.roles.unwrap_or_default();
let start_commit_num = self.start_commit_num.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("start_commit_num".to_string())
})?;
let end_commit_num = self
.end_commit_num
.ok_or_else(|| PikeBuilderError::MissingRequiredField("end_commit_num".to_string()))?;
if start_commit_num >= end_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"start_commit_num".to_string(),
"argument cannot be greater than or equal to `end_commit_num`".to_string(),
),
));
}
if end_commit_num <= start_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"end_commit_num".to_string(),
"argument cannot be less than or equal to `start_commit_num`".to_string(),
),
));
}
let service_id = self.service_id;
let last_updated = self.last_updated;
Ok(Agent {
public_key,
org_id,
active,
metadata,
roles,
start_commit_num,
end_commit_num,
service_id,
last_updated,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct RoleBuilder {
name: Option<String>,
org_id: Option<String>,
description: Option<String>,
active: Option<bool>,
permissions: Option<Vec<String>>,
allowed_organizations: Option<Vec<String>>,
inherit_from: Option<Vec<String>>,
start_commit_num: Option<i64>,
end_commit_num: Option<i64>,
service_id: Option<String>,
last_updated: Option<i64>,
}
impl RoleBuilder {
pub fn new() -> Self {
RoleBuilder::default()
}
pub fn with_name(mut self, name: String) -> Self {
self.name = Some(name);
self
}
pub fn with_org_id(mut self, org_id: String) -> Self {
self.org_id = Some(org_id);
self
}
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
pub fn with_active(mut self, active: bool) -> Self {
self.active = Some(active);
self
}
pub fn with_permissions(mut self, permissions: Vec<String>) -> Self {
self.permissions = Some(permissions);
self
}
pub fn with_allowed_organizations(mut self, allowed_organizations: Vec<String>) -> Self {
self.allowed_organizations = Some(allowed_organizations);
self
}
pub fn with_inherit_from(mut self, inherit_from: Vec<String>) -> Self {
self.inherit_from = Some(inherit_from);
self
}
pub fn with_start_commit_num(mut self, start_commit_num: i64) -> Self {
self.start_commit_num = Some(start_commit_num);
self
}
pub fn with_end_commit_num(mut self, end_commit_num: i64) -> Self {
self.end_commit_num = Some(end_commit_num);
self
}
pub fn with_service_id(mut self, service_id: String) -> Self {
self.service_id = Some(service_id);
self
}
pub fn with_last_updated(mut self, last_updated: i64) -> Self {
self.last_updated = Some(last_updated);
self
}
pub fn build(self) -> Result<Role, PikeBuilderError> {
let name = self
.name
.ok_or_else(|| PikeBuilderError::MissingRequiredField("name".to_string()))?;
let org_id = self
.org_id
.ok_or_else(|| PikeBuilderError::MissingRequiredField("org_id".to_string()))?;
let description = self.description.unwrap_or_default();
let active = self
.active
.ok_or_else(|| PikeBuilderError::MissingRequiredField("active".to_string()))?;
let permissions = self.permissions.unwrap_or_default();
let allowed_organizations = self.allowed_organizations.unwrap_or_default();
let inherit_from = self.inherit_from.unwrap_or_default();
let start_commit_num = self.start_commit_num.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("start_commit_num".to_string())
})?;
let end_commit_num = self
.end_commit_num
.ok_or_else(|| PikeBuilderError::MissingRequiredField("end_commit_num".to_string()))?;
if start_commit_num >= end_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"start_commit_num".to_string(),
"argument cannot be greater than or equal to `end_commit_num`".to_string(),
),
));
}
if end_commit_num <= start_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"end_commit_num".to_string(),
"argument cannot be less than or equal to `start_commit_num`".to_string(),
),
));
}
let service_id = self.service_id;
let last_updated = self.last_updated;
Ok(Role {
name,
org_id,
description,
active,
permissions,
allowed_organizations,
inherit_from,
start_commit_num,
end_commit_num,
service_id,
last_updated,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct OrganizationBuilder {
org_id: Option<String>,
name: Option<String>,
locations: Option<Vec<String>>,
alternate_ids: Option<Vec<AlternateId>>,
metadata: Option<Vec<OrganizationMetadata>>,
start_commit_num: Option<i64>,
end_commit_num: Option<i64>,
service_id: Option<String>,
last_updated: Option<i64>,
}
impl OrganizationBuilder {
pub fn new() -> Self {
OrganizationBuilder::default()
}
pub fn with_org_id(mut self, org_id: String) -> Self {
self.org_id = Some(org_id);
self
}
pub fn with_name(mut self, name: String) -> Self {
self.name = Some(name);
self
}
pub fn with_locations(mut self, locations: Vec<String>) -> Self {
self.locations = Some(locations);
self
}
pub fn with_alternate_ids(mut self, alternate_ids: Vec<AlternateId>) -> Self {
self.alternate_ids = Some(alternate_ids);
self
}
pub fn with_metadata(mut self, metadata: Vec<OrganizationMetadata>) -> Self {
self.metadata = Some(metadata);
self
}
pub fn with_start_commit_num(mut self, start_commit_num: i64) -> Self {
self.start_commit_num = Some(start_commit_num);
self
}
pub fn with_end_commit_num(mut self, end_commit_num: i64) -> Self {
self.end_commit_num = Some(end_commit_num);
self
}
pub fn with_service_id(mut self, service_id: String) -> Self {
self.service_id = Some(service_id);
self
}
pub fn with_last_updated(mut self, last_updated: i64) -> Self {
self.last_updated = Some(last_updated);
self
}
pub fn build(self) -> Result<Organization, PikeBuilderError> {
let org_id = self
.org_id
.ok_or_else(|| PikeBuilderError::MissingRequiredField("org_id".to_string()))?;
let name = self
.name
.ok_or_else(|| PikeBuilderError::MissingRequiredField("name".to_string()))?;
let locations = self.locations.unwrap_or_default();
let alternate_ids = self.alternate_ids.unwrap_or_default();
let metadata = self.metadata.unwrap_or_default();
let start_commit_num = self.start_commit_num.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("start_commit_num".to_string())
})?;
let end_commit_num = self
.end_commit_num
.ok_or_else(|| PikeBuilderError::MissingRequiredField("end_commit_num".to_string()))?;
if start_commit_num >= end_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"start_commit_num".to_string(),
"argument cannot be greater than or equal to `end_commit_num`".to_string(),
),
));
}
if end_commit_num <= start_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"end_commit_num".to_string(),
"argument cannot be less than or equal to `start_commit_num`".to_string(),
),
));
}
let service_id = self.service_id;
let last_updated = self.last_updated;
Ok(Organization {
org_id,
name,
locations,
alternate_ids,
metadata,
start_commit_num,
end_commit_num,
service_id,
last_updated,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct AlternateIdBuilder {
org_id: Option<String>,
alternate_id_type: Option<String>,
alternate_id: Option<String>,
start_commit_num: Option<i64>,
end_commit_num: Option<i64>,
service_id: Option<String>,
}
impl AlternateIdBuilder {
pub fn new() -> Self {
AlternateIdBuilder::default()
}
pub fn with_org_id(mut self, org_id: String) -> Self {
self.org_id = Some(org_id);
self
}
pub fn with_alternate_id_type(mut self, alternate_id_type: String) -> Self {
self.alternate_id_type = Some(alternate_id_type);
self
}
pub fn with_alternate_id(mut self, alternate_id: String) -> Self {
self.alternate_id = Some(alternate_id);
self
}
pub fn with_start_commit_num(mut self, start_commit_num: i64) -> Self {
self.start_commit_num = Some(start_commit_num);
self
}
pub fn with_end_commit_num(mut self, end_commit_num: i64) -> Self {
self.end_commit_num = Some(end_commit_num);
self
}
pub fn with_service_id(mut self, service_id: String) -> Self {
self.service_id = Some(service_id);
self
}
pub fn build(self) -> Result<AlternateId, PikeBuilderError> {
let org_id = self
.org_id
.ok_or_else(|| PikeBuilderError::MissingRequiredField("org_id".to_string()))?;
let alternate_id_type = self.alternate_id_type.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("alternate_id_type".to_string())
})?;
let alternate_id = self
.alternate_id
.ok_or_else(|| PikeBuilderError::MissingRequiredField("alternate_id".to_string()))?;
let start_commit_num = self.start_commit_num.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("start_commit_num".to_string())
})?;
let end_commit_num = self
.end_commit_num
.ok_or_else(|| PikeBuilderError::MissingRequiredField("end_commit_num".to_string()))?;
if start_commit_num >= end_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"start_commit_num".to_string(),
"argument cannot be greater than or equal to `end_commit_num`".to_string(),
),
));
}
if end_commit_num <= start_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"end_commit_num".to_string(),
"argument cannot be less than or equal to `start_commit_num`".to_string(),
),
));
}
let service_id = self.service_id;
Ok(AlternateId {
org_id,
alternate_id_type,
alternate_id,
start_commit_num,
end_commit_num,
service_id,
})
}
}
#[derive(Clone, Debug, Default)]
pub struct OrganizationMetadataBuilder {
key: Option<String>,
value: Option<String>,
start_commit_num: Option<i64>,
end_commit_num: Option<i64>,
service_id: Option<String>,
}
impl OrganizationMetadataBuilder {
pub fn new() -> Self {
OrganizationMetadataBuilder::default()
}
pub fn with_key(mut self, key: String) -> Self {
self.key = Some(key);
self
}
pub fn with_value(mut self, value: String) -> Self {
self.value = Some(value);
self
}
pub fn with_start_commit_num(mut self, start_commit_num: i64) -> Self {
self.start_commit_num = Some(start_commit_num);
self
}
pub fn with_end_commit_num(mut self, end_commit_num: i64) -> Self {
self.end_commit_num = Some(end_commit_num);
self
}
pub fn with_service_id(mut self, service_id: String) -> Self {
self.service_id = Some(service_id);
self
}
pub fn build(self) -> Result<OrganizationMetadata, PikeBuilderError> {
let key = self
.key
.ok_or_else(|| PikeBuilderError::MissingRequiredField("key".to_string()))?;
let value = self
.value
.ok_or_else(|| PikeBuilderError::MissingRequiredField("value".to_string()))?;
let start_commit_num = self.start_commit_num.ok_or_else(|| {
PikeBuilderError::MissingRequiredField("start_commit_num".to_string())
})?;
let end_commit_num = self
.end_commit_num
.ok_or_else(|| PikeBuilderError::MissingRequiredField("end_commit_num".to_string()))?;
if start_commit_num >= end_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"start_commit_num".to_string(),
"argument cannot be greater than or equal to `end_commit_num`".to_string(),
),
));
}
if end_commit_num <= start_commit_num {
return Err(PikeBuilderError::InvalidArgumentError(
InvalidArgumentError::new(
"end_commit_num".to_string(),
"argument cannot be less than or equal to `start_commit_num`".to_string(),
),
));
}
let service_id = self.service_id;
Ok(OrganizationMetadata {
key,
value,
start_commit_num,
end_commit_num,
service_id,
})
}
}