use crate::alloc::sync::Arc;
use crate::callback::SessionCallback;
use crate::config::SASLConfig;
use crate::error::SASLError;
use crate::registry::Registry;
use core::fmt::{Debug, Formatter};
#[derive(Clone)]
pub struct ConfigBuilder<State = WantMechanisms> {
pub(crate) state: State,
}
impl<State: Debug> Debug for ConfigBuilder<State> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ConfigBuilder<_>")
.field("state", &self.state)
.finish()
}
}
#[derive(Clone, Debug)]
#[doc(hidden)]
pub struct WantMechanisms(());
impl ConfigBuilder {
pub(crate) const fn new() -> Self {
Self {
state: WantMechanisms(()),
}
}
#[must_use]
pub fn with_defaults(self) -> ConfigBuilder<WantCallback> {
ConfigBuilder {
state: WantCallback {
mechanisms: Registry::default(),
},
}
}
#[must_use]
pub const fn with_registry(self, mechanisms: Registry) -> ConfigBuilder<WantCallback> {
ConfigBuilder {
state: WantCallback { mechanisms },
}
}
#[must_use]
pub fn with_default_mechanisms(self) -> ConfigBuilder<WantCallback> {
self.with_registry(Registry::default())
}
pub(crate) fn with_credentials_mechanisms(self, authzid: bool) -> ConfigBuilder<WantCallback> {
self.with_registry(Registry::credentials(authzid))
}
}
#[derive(Clone)]
#[doc(hidden)]
pub struct WantCallback {
mechanisms: Registry,
}
impl ConfigBuilder<WantCallback> {
pub fn with_callback<CB: SessionCallback + 'static>(
self,
callback: CB,
) -> Result<Arc<SASLConfig>, SASLError> {
SASLConfig::new(callback, self.state.mechanisms)
}
}