Skip to main content

aranya_client/config/
team.rs

1//! Team configuration for creating new teams or adding existing ones.
2//!
3//! This module provides builders for configuring team operations with support
4//! for multiple transport mechanisms.
5//!
6//! # Overview
7//!
8//! There are two primary operations:
9//! - **Create Team**: Establish a new team with [`CreateTeamConfig`]
10//! - **Add Team**: Add an existing team with [`AddTeamConfig`]
11//!
12//! Both operations support optional transport configuration.
13
14use crate::{client::TeamId, error::InvalidArg, util::ApiConv as _, ConfigError, Result};
15
16pub mod quic_sync;
17pub use quic_sync::{
18    AddTeamQuicSyncConfig, CreateTeamQuicSyncConfig, CreateTeamQuicSyncConfigBuilder,
19};
20
21/// Builder for [`CreateTeamConfig`].
22#[derive(Debug, Default)]
23pub struct CreateTeamConfigBuilder {
24    quic_sync: Option<CreateTeamQuicSyncConfig>,
25}
26
27impl CreateTeamConfigBuilder {
28    /// Configures the quic_sync config..
29    ///
30    /// This is an optional field that configures how the team
31    /// synchronizes data over QUIC connections.
32    pub fn quic_sync(mut self, cfg: CreateTeamQuicSyncConfig) -> Self {
33        self.quic_sync = Some(cfg);
34        self
35    }
36
37    /// Builds the configuration for creating a new team.
38    pub fn build(self) -> Result<CreateTeamConfig> {
39        Ok(CreateTeamConfig {
40            quic_sync: self.quic_sync,
41        })
42    }
43}
44
45/// Builder for [`AddTeamConfig`].
46#[derive(Debug, Default)]
47pub struct AddTeamConfigBuilder {
48    id: Option<TeamId>,
49    quic_sync: Option<AddTeamQuicSyncConfig>,
50}
51
52impl AddTeamConfigBuilder {
53    /// Sets the ID of the team to add.
54    pub fn team_id(mut self, id: TeamId) -> Self {
55        self.id = Some(id);
56        self
57    }
58
59    /// Configures the quic_sync config..
60    ///
61    /// This is an optional field that configures how the team
62    /// synchronizes data over QUIC connections.
63    pub fn quic_sync(mut self, cfg: AddTeamQuicSyncConfig) -> Self {
64        self.quic_sync = Some(cfg);
65        self
66    }
67
68    /// Attempts to build an [`AddTeamConfig`] using the provided parameters.
69    pub fn build(self) -> Result<AddTeamConfig> {
70        let id = self.id.ok_or_else(|| {
71            ConfigError::InvalidArg(InvalidArg::new(
72                "id",
73                "Missing `id` field when calling `AddTeamConfigBuilder::build`",
74            ))
75        })?;
76
77        Ok(AddTeamConfig {
78            id,
79            quic_sync: self.quic_sync,
80        })
81    }
82}
83
84/// Configuration for creating a new team.
85#[derive(Clone, Debug)]
86pub struct CreateTeamConfig {
87    quic_sync: Option<CreateTeamQuicSyncConfig>,
88}
89
90impl CreateTeamConfig {
91    /// Creates a default [`CreateTeamConfigBuilder`].
92    pub fn builder() -> CreateTeamConfigBuilder {
93        CreateTeamConfigBuilder::default()
94    }
95}
96
97impl From<CreateTeamConfig> for aranya_daemon_api::CreateTeamConfig {
98    fn from(value: CreateTeamConfig) -> Self {
99        Self {
100            quic_sync: value.quic_sync.map(Into::into),
101        }
102    }
103}
104
105/// Configuration for joining an existing team.
106#[derive(Clone, Debug)]
107pub struct AddTeamConfig {
108    id: TeamId,
109    quic_sync: Option<AddTeamQuicSyncConfig>,
110}
111
112impl AddTeamConfig {
113    /// Creates a default [`AddTeamConfigBuilder`].
114    pub fn builder() -> AddTeamConfigBuilder {
115        AddTeamConfigBuilder::default()
116    }
117}
118
119impl From<AddTeamConfig> for aranya_daemon_api::AddTeamConfig {
120    fn from(value: AddTeamConfig) -> Self {
121        Self {
122            team_id: value.id.into_api(),
123            quic_sync: value.quic_sync.map(Into::into),
124        }
125    }
126}