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
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    #[error(transparent)]
32    UnexpectedContentType(#[from] UnexpectedContentTypeError),
33}
34
35impl<C, B> Error<C, B> {
36    pub fn boxed(self) -> Box<dyn std::error::Error + Send + Sync + 'static>
37    where
38        C: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
39        B: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
40    {
41        match self {
42            Self::Client(e) => e.into(),
43            Self::Body(e) => e.into(),
44            Self::Http(e) => e.into(),
45            Self::Json(e) => e.into(),
46            Self::Urlencode(e) => e.into(),
47            Self::Utf8(e) => e.into(),
48            Self::Api(e) => e.into(),
49            Self::UnexpectedContentType(e) => e.into(),
50        }
51    }
52}
53
54#[derive(Debug, thiserror::Error)]
55#[error("{message}")]
56pub struct ApiError {
57    pub code: Option<String>,
58    pub message: String,
59    pub param: Option<String>,
60    pub r#type: String,
61}
62
63#[derive(Debug, thiserror::Error)]
64#[error("unexpected content-type: expected = {expected:?}, actual = {actual:?}")]
65pub struct UnexpectedContentTypeError {
66    pub expected: mime::Mime,
67    pub actual: Option<mime::Mime>,
68}
69
70#[cfg(test)]
71mod tests;
72
73macro_rules! future {
74    ($ident:ident, $fut:ty, $output:ty) => {
75        #[pin_project::pin_project]
76        pub struct $ident<Fut, B, E>(
77            #[pin]
78            #[allow(clippy::type_complexity)]
79            $fut,
80        )
81        where
82            Fut: Future<Output = Result<http::Response<B>, E>>,
83            B: http_body::Body;
84
85        impl<Fut, B, E> Future for $ident<Fut, B, E>
86        where
87            Fut: Future<Output = Result<http::Response<B>, E>>,
88            B: http_body::Body,
89        {
90            type Output = Result<$output, crate::Error<E, B::Error>>;
91            fn poll(
92                self: std::pin::Pin<&mut Self>,
93                cx: &mut std::task::Context<'_>,
94            ) -> std::task::Poll<Self::Output> {
95                self.project().0.poll(cx)
96            }
97        }
98    };
99}
100mod generated;