openai_openapi_http/
lib.rs

1mod event_stream;
2mod json;
3mod send;
4
5mod __combinators {
6    pub(crate) use crate::event_stream::EventStream;
7    pub(crate) use crate::json::Json;
8    pub(crate) use crate::send::Send;
9}
10
11use bytes::Bytes;
12pub use event_stream::EventStream;
13pub use generated::*;
14use openai_openapi_types as __types;
15use std::fmt;
16
17#[derive(Debug, thiserror::Error)]
18pub enum Error<C, B> {
19    #[error(transparent)]
20    Client(C),
21    #[error(transparent)]
22    Body(B),
23    #[error(transparent)]
24    Http(#[from] http::Error),
25    #[error(transparent)]
26    Json(#[from] serde_json::Error),
27    #[error(transparent)]
28    Urlencode(#[from] serde_urlencoded::ser::Error),
29    #[error(transparent)]
30    Utf8(#[from] std::str::Utf8Error),
31    #[error(transparent)]
32    ContentType(#[from] ContentTypeError),
33    #[error(transparent)]
34    EventStream(#[from] EventStreamError),
35    #[error(transparent)]
36    Status(#[from] StatusError),
37}
38
39impl<C, B> Error<C, B> {
40    pub fn boxed(self) -> Box<dyn std::error::Error + Send + Sync + 'static>
41    where
42        C: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
43        B: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
44    {
45        match self {
46            Self::Client(e) => e.into(),
47            Self::Body(e) => e.into(),
48            Self::Http(e) => e.into(),
49            Self::Json(e) => e.into(),
50            Self::Urlencode(e) => e.into(),
51            Self::Utf8(e) => e.into(),
52            Self::ContentType(e) => e.into(),
53            Self::EventStream(e) => e.into(),
54            Self::Status(e) => e.into(),
55        }
56    }
57}
58
59#[derive(Debug, thiserror::Error)]
60pub struct ContentTypeError(pub http::Response<Bytes>);
61
62impl fmt::Display for ContentTypeError {
63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64        write!(f, "invalid content-type: {:?}", send::content_type(&self.0))
65    }
66}
67
68#[derive(Debug, thiserror::Error)]
69#[error("error in event stream: {0:?}")]
70pub struct EventStreamError(pub String);
71
72#[derive(Debug, thiserror::Error)]
73pub struct StatusError(pub http::Response<Bytes>);
74
75impl fmt::Display for StatusError {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "invalid status: {}", self.0.status())
78    }
79}
80
81#[cfg(test)]
82mod tests;
83
84macro_rules! future {
85    ($ident:ident, $fut:ty, $output:ty) => {
86        #[pin_project::pin_project]
87        pub struct $ident<Fut, B, E>(
88            #[pin]
89            #[allow(clippy::type_complexity)]
90            $fut,
91        )
92        where
93            Fut: Future<Output = Result<http::Response<B>, E>>,
94            B: http_body::Body;
95
96        impl<Fut, B, E> Future for $ident<Fut, B, E>
97        where
98            Fut: Future<Output = Result<http::Response<B>, E>>,
99            B: http_body::Body,
100        {
101            type Output = Result<$output, crate::Error<E, B::Error>>;
102            fn poll(
103                self: std::pin::Pin<&mut Self>,
104                cx: &mut std::task::Context<'_>,
105            ) -> std::task::Poll<Self::Output> {
106                self.project().0.poll(cx)
107            }
108        }
109    };
110}
111mod generated;