openai_openapi_http/
lib.rs

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