use serde::Deserialize;
use serde::Serialize;
use crate::query::ExpandQuery;
use crate::Action;
use crate::BoxTryStream;
use crate::DecodeError;
use crate::EntityTypeRef;
use crate::Expandable;
use crate::FilterQuery;
use crate::HttpPushUriUpdateRequest;
use crate::ModificationResponse;
use crate::ODataETag;
use crate::ODataId;
use crate::Repair;
use crate::RepairError;
use crate::SessionCreateResponse;
use std::error::Error as StdError;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use futures_core::Stream;
use crate::MultipartUpdateRequest;
use crate::UploadReader;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum BmcErrorClass {
HttpResponse {
status: u16,
},
ResponseParse,
Transport,
Other,
}
pub trait BmcError: StdError + Send + Sync {
fn error_class(&self) -> BmcErrorClass {
BmcErrorClass::Other
}
fn response_body(&self) -> Option<&str> {
None
}
fn into_decode_error(self) -> Result<DecodeError, Self>
where
Self: Sized,
{
Err(self)
}
}
pub trait Bmc: Send + Sync {
type Error: BmcError + 'static;
fn expand<T: Expandable>(
&self,
id: &ODataId,
query: ExpandQuery,
) -> impl Future<Output = Result<Arc<T>, Self::Error>> + Send;
fn get<T: EntityTypeRef + for<'de> Deserialize<'de> + 'static>(
&self,
id: &ODataId,
) -> impl Future<Output = Result<Arc<T>, Self::Error>> + Send;
fn filter<T: EntityTypeRef + for<'de> Deserialize<'de> + 'static>(
&self,
id: &ODataId,
query: FilterQuery,
) -> impl Future<Output = Result<Arc<T>, Self::Error>> + Send;
fn get_repaired<T: EntityTypeRef + for<'de> Deserialize<'de> + 'static>(
&self,
id: &ODataId,
repair: &impl Repair,
) -> impl Future<Output = Result<Arc<T>, RepairError<Self::Error>>> + Send;
fn expand_repaired<T: Expandable>(
&self,
id: &ODataId,
query: ExpandQuery,
repair: &impl Repair,
) -> impl Future<Output = Result<Arc<T>, RepairError<Self::Error>>> + Send;
fn filter_repaired<T: EntityTypeRef + for<'de> Deserialize<'de> + 'static>(
&self,
id: &ODataId,
query: FilterQuery,
repair: &impl Repair,
) -> impl Future<Output = Result<Arc<T>, RepairError<Self::Error>>> + Send;
fn create<V: Send + Sync + Serialize, R: Send + Sync + for<'de> Deserialize<'de>>(
&self,
id: &ODataId,
query: &V,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send;
fn create_session<V: Send + Sync + Serialize, R: Send + Sync + for<'de> Deserialize<'de>>(
&self,
id: &ODataId,
query: &V,
) -> impl Future<Output = Result<SessionCreateResponse<R>, Self::Error>> + Send;
fn update<V: Sync + Send + Serialize, R: Send + Sync + Sized + for<'de> Deserialize<'de>>(
&self,
id: &ODataId,
etag: Option<&ODataETag>,
update: &V,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send;
fn delete<R: EntityTypeRef + for<'de> Deserialize<'de>>(
&self,
id: &ODataId,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send;
fn action<T: Send + Sync + Serialize, R: Send + Sync + Sized + for<'de> Deserialize<'de>>(
&self,
action: &Action<T, R>,
params: &T,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send;
fn poll<R: Send + Sync + Sized + for<'de> Deserialize<'de>>(
&self,
uri: &ODataId,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send;
fn multipart_update<U, V, R>(
&self,
uri: &str,
request: MultipartUpdateRequest<'_, U, V>,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send
where
U: UploadReader,
R: Send + Sync + for<'de> Deserialize<'de>,
V: Send + Sync + Serialize;
fn http_push_uri_update<U, R>(
&self,
uri: &str,
request: HttpPushUriUpdateRequest<U>,
) -> impl Future<Output = Result<ModificationResponse<R>, Self::Error>> + Send
where
U: UploadReader,
R: Send + Sync + for<'de> Deserialize<'de>;
fn stream<T: Sized + for<'de> Deserialize<'de> + Send + 'static>(
&self,
uri: &str,
) -> impl Future<Output = Result<BoxTryStream<T, Self::Error>, Self::Error>> + Send;
fn stream_events<T: Sized + for<'de> Deserialize<'de> + Send + 'static>(
&self,
uri: &str,
last_event_id: Option<&str>,
) -> impl Future<Output = Result<BoxTryStream<StreamEvent<T>, Self::Error>, Self::Error>> + Send
where
Self::Error: 'static,
{
let _ = last_event_id;
async move { self.stream::<T>(uri).await.map(without_event_ids) }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamEvent<T> {
pub last_event_id: Option<String>,
pub data: T,
}
#[must_use]
pub fn without_event_ids<T, E>(stream: BoxTryStream<T, E>) -> BoxTryStream<StreamEvent<T>, E>
where
T: 'static,
E: 'static,
{
Box::pin(WithoutEventIds(stream))
}
struct WithoutEventIds<T, E>(BoxTryStream<T, E>);
impl<T, E> Stream for WithoutEventIds<T, E> {
type Item = Result<StreamEvent<T>, E>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.0.as_mut().poll_next(cx).map(|item| {
item.map(|result| {
result.map(|data| StreamEvent {
last_event_id: None,
data,
})
})
})
}
}
#[cfg(test)]
mod tests {
use std::task::Waker;
use std::vec::IntoIter;
use super::*;
struct Items(IntoIter<Result<u8, &'static str>>);
impl Stream for Items {
type Item = Result<u8, &'static str>;
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Poll::Ready(self.0.next())
}
}
#[test]
fn without_ids_keeps_items_and_errors_and_attaches_no_id() {
let items = Items(vec![Ok(1), Err("lost"), Ok(2)].into_iter());
let mut events = without_event_ids(Box::pin(items));
let mut cx = Context::from_waker(Waker::noop());
let event = |data| {
Poll::Ready(Some(Ok(StreamEvent {
last_event_id: None,
data,
})))
};
assert_eq!(events.as_mut().poll_next(&mut cx), event(1));
assert_eq!(
events.as_mut().poll_next(&mut cx),
Poll::Ready(Some(Err("lost")))
);
assert_eq!(events.as_mut().poll_next(&mut cx), event(2));
assert_eq!(events.as_mut().poll_next(&mut cx), Poll::Ready(None));
}
}