core_crypto/mls/session/
config.rs1use core_crypto_keystore::Database;
2use typed_builder::TypedBuilder;
3
4use crate::{
5 ClientId,
6 mls::{
7 ciphersuite::MlsCiphersuite,
8 error::{Error, Result},
9 },
10};
11
12#[derive(Debug, Clone, TypedBuilder)]
16pub struct SessionConfig {
17 pub database: Database,
19 #[builder(default, setter(strip_option(fallback = client_id_opt)))]
23 pub client_id: Option<ClientId>,
24 #[builder(default, setter(transform = |iter: impl IntoIterator<Item = MlsCiphersuite>| iter.into_iter().collect()))]
26 pub ciphersuites: Vec<MlsCiphersuite>,
27}
28
29#[derive(Debug)]
33pub struct ValidatedSessionConfig {
34 pub(super) database: Database,
35 pub(super) client_id: Option<ClientId>,
36 pub(super) ciphersuites: Vec<MlsCiphersuite>,
37}
38
39impl SessionConfig {
40 pub fn validate(self) -> Result<ValidatedSessionConfig> {
44 let Self {
45 database,
46 client_id,
47 ciphersuites,
48 } = self;
49 if let Some(client_id) = &client_id
50 && client_id.is_empty()
51 {
52 return Err(Error::MalformedIdentifier("client_id"));
53 }
54
55 if client_id.is_some() && ciphersuites.is_empty() {
56 return Err(Error::MalformedIdentifier(
57 "ciphersuites must be non-empty if initializing (i.e. client_id is set)",
58 ));
59 }
60
61 Ok(ValidatedSessionConfig {
62 database,
63 client_id,
64 ciphersuites,
65 })
66 }
67}
68
69impl TryFrom<SessionConfig> for ValidatedSessionConfig {
70 type Error = Error;
71
72 fn try_from(value: SessionConfig) -> std::result::Result<Self, Self::Error> {
73 value.validate()
74 }
75}