use tracing::instrument;
use url::Url;
use crate::{
HttpError, authentication::oauth::qrcode::SecureChannelError, http_client::HttpClient,
};
mod msc_4108;
pub(super) struct InboundChannelCreationResult {
pub channel: RendezvousChannel,
#[allow(dead_code)]
pub initial_message: Vec<u8>,
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum RendezvousInfo<'a> {
Msc4108 { rendezvous_url: &'a Url },
}
pub(super) enum RendezvousChannel {
Msc4108(msc_4108::Channel),
}
impl RendezvousChannel {
pub(super) async fn create_outbound(
client: HttpClient,
rendezvous_server: &Url,
) -> Result<Self, HttpError> {
Ok(Self::Msc4108(msc_4108::Channel::create_outbound(client, rendezvous_server).await?))
}
pub(super) async fn create_inbound(
client: HttpClient,
rendezvous_url: &Url,
) -> Result<InboundChannelCreationResult, HttpError> {
let msc_4108::InboundChannelCreationResult { channel, initial_message } =
msc_4108::Channel::create_inbound(client, rendezvous_url).await?;
Ok(InboundChannelCreationResult { channel: Self::Msc4108(channel), initial_message })
}
pub(super) fn rendezvous_info(&self) -> RendezvousInfo<'_> {
match self {
RendezvousChannel::Msc4108(channel) => {
RendezvousInfo::Msc4108 { rendezvous_url: channel.rendezvous_url() }
}
}
}
#[instrument(skip_all)]
pub(super) async fn send(&mut self, message: String) -> Result<(), HttpError> {
match self {
RendezvousChannel::Msc4108(channel) => channel.send(message.into_bytes()).await,
}
}
pub(super) async fn receive(&mut self) -> Result<String, SecureChannelError> {
let message = match self {
RendezvousChannel::Msc4108(channel) => channel.receive().await?,
};
Ok(String::from_utf8(message).map_err(|e| e.utf8_error())?)
}
}