use ibridge_auth::{AuthError, Credentials, ValidationReport, Validator};
#[derive(Debug)]
pub(crate) struct Combined<A, B> {
pub(crate) mail: A,
pub(crate) dav: B,
}
impl<A: Validator + Sync, B: Validator + Sync> Validator for Combined<A, B> {
async fn validate(&self) -> Result<ValidationReport, AuthError> {
let mail = self.mail.validate().await?;
let dav = self.dav.validate().await?;
Ok(ValidationReport {
mail: mail.mail,
calendar: dav.calendar,
contacts: dav.contacts,
})
}
}
#[cfg(any(not(feature = "mail"), not(feature = "calendar")))]
#[derive(Debug)]
pub(crate) struct Skipped;
#[cfg(any(not(feature = "mail"), not(feature = "calendar")))]
#[expect(
clippy::unused_async_trait_impl,
reason = "the stand-in answers without waiting"
)]
impl Validator for Skipped {
async fn validate(&self) -> Result<ValidationReport, AuthError> {
use ibridge_auth::ServiceCheck;
let off = |name: &str| ServiceCheck::Failed {
reason: format!("{name} sign-in is not available in this build"),
};
Ok(ValidationReport {
mail: off("mail"),
calendar: off("calendar"),
contacts: off("contacts"),
})
}
}
#[cfg(feature = "mail")]
type MailCheck = crate::mail::ImapValidator;
#[cfg(not(feature = "mail"))]
type MailCheck = Skipped;
#[cfg(feature = "calendar")]
type DavCheck = ibridge_dav::DavValidator<ibridge_dav::DavClient>;
#[cfg(not(feature = "calendar"))]
type DavCheck = Skipped;
#[cfg_attr(
not(any(feature = "mail", feature = "calendar")),
expect(
clippy::unnecessary_wraps,
reason = "builds with a service can fail, and every build shares one signature"
)
)]
pub(crate) fn validator(
#[cfg_attr(
not(any(feature = "mail", feature = "calendar")),
expect(unused_variables, reason = "no service in this build reads them")
)]
credentials: &Credentials,
) -> anyhow::Result<Combined<MailCheck, DavCheck>> {
#[cfg(feature = "mail")]
let mail = crate::mail::imap_validator(credentials)?;
#[cfg(not(feature = "mail"))]
let mail = Skipped;
#[cfg(feature = "calendar")]
let dav = ibridge_dav::DavValidator::new(credentials.duplicate())?;
#[cfg(not(feature = "calendar"))]
let dav = Skipped;
Ok(Combined { mail, dav })
}
#[cfg(feature = "hosted")]
pub(crate) fn hosted_validator(
credentials: &Credentials,
) -> anyhow::Result<impl Validator + Sync + use<>> {
let Combined { mail, dav } = validator(credentials)?;
Ok(ibridge_hosted::Combined { mail, dav })
}