use alloc::string::String;
use core::future::Future;
use futures_core::Stream;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::SdkError;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ConversationId(String);
impl ConversationId {
#[must_use]
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl From<String> for ConversationId {
fn from(value: String) -> Self {
Self::new(value)
}
}
impl From<&'static str> for ConversationId {
fn from(value: &'static str) -> Self {
Self::new(value)
}
}
#[derive(Debug)]
pub enum ConversationEvent {
Opened {
conversation_id: ConversationId,
},
Message {
conversation_id: ConversationId,
},
Closing {
conversation_id: ConversationId,
},
Closed {
conversation_id: ConversationId,
},
Error {
conversation_id: ConversationId,
error: SdkError,
},
}
pub trait ConversationHandle: core::fmt::Debug + Send + Sync {
type ReceiveFuture<'a, M>: Future<Output = Result<M, SdkError>> + 'a
where
Self: 'a,
M: DeserializeOwned + 'a;
type LifecycleStream: Stream<Item = ConversationEvent>;
fn send<M>(&self, message: M) -> Result<(), SdkError>
where
M: Serialize;
fn receive<M>(&self) -> Self::ReceiveFuture<'_, M>
where
M: DeserializeOwned;
fn lifecycle(&self) -> Self::LifecycleStream;
}