Skip to main content

timvw_rust_runtime/
lib.rs

1#![cfg_attr(
2    all(feature = "reqwest", feature = "retry", feature = "tracing"),
3    doc = include_str!("../README.md")
4)]
5#![cfg_attr(
6    not(all(feature = "reqwest", feature = "retry", feature = "tracing")),
7    doc = "Runtime for Rust clients generated by [openapi-generator-timvw-rust](https://github.com/timvw/openapi-generator-timvw-rust): a tower-based HTTP client. See the README for the guide."
8)]
9#![cfg_attr(docsrs, feature(doc_cfg))]
10// On WebAssembly the shared pieces of bodies and uploads (`Arc`s) hold streams that are not `Send`: there is one
11// thread, and an `Arc` keeps the code the same as natively.
12#![cfg_attr(target_family = "wasm", allow(clippy::arc_with_non_send_sync))]
13
14mod body;
15mod client;
16#[cfg(all(feature = "fs", not(target_family = "wasm")))]
17mod download;
18mod error;
19mod local_service;
20mod maybe;
21pub mod multipart;
22pub mod provider;
23mod request;
24mod request_options;
25#[cfg(feature = "reqwest")]
26mod reqwest_service;
27mod response;
28#[cfg(feature = "retry")]
29pub mod retry;
30pub mod sse;
31#[cfg(feature = "time")]
32pub mod timeout;
33#[cfg(feature = "time")]
34mod timer;
35#[cfg(feature = "tracing")]
36pub mod trace;
37mod upload;
38#[cfg(feature = "webhooks")]
39pub mod webhooks;
40
41pub use body::Body;
42pub use client::{Client, HttpService};
43pub use error::Error;
44pub use local_service::LocalBoxCloneService;
45pub use maybe::{MaybeSend, MaybeSync};
46pub use provider::ProviderLayer;
47pub use request::RequestBuilder;
48pub use request_options::RequestOptions;
49#[cfg(feature = "reqwest")]
50pub use reqwest_service::ReqwestService;
51pub use response::Response;
52pub use upload::Upload;
53
54pub use http;
55pub use http::{header, HeaderMap, Method, StatusCode};
56/// Credentials of a generated `Configuration` are [`SecretString`]s: their `Debug` is redacted, their memory is
57/// zeroed when dropped, and [`ExposeSecret::expose_secret`] reads them.
58pub use secrecy;
59pub use secrecy::{ExposeSecret, SecretString};
60pub use tower;
61
62/// The error type services and layers return: any error that can cross threads.
63pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
64
65/// Natively, what the crate hands out stays `Send` (and `Sync` where it was): [`MaybeSend`] relaxes bounds on
66/// WebAssembly only.
67#[cfg(all(test, not(target_family = "wasm")))]
68mod send_tests {
69    use super::*;
70
71    fn send_sync<T: Send + Sync>() {}
72    fn send<T: Send>(_: &T) {}
73
74    #[test]
75    fn natively_clients_bodies_and_futures_are_send() {
76        send_sync::<Client>();
77        send_sync::<HttpService>();
78        send_sync::<Body>();
79        send_sync::<Response>();
80        send_sync::<RequestBuilder>();
81        send_sync::<Upload>();
82        send_sync::<multipart::Form>();
83        send_sync::<ProviderLayer>();
84        let request = Client::default().request(Method::GET, "https://example.com/");
85        send(&request.send());
86        let response = Response::from(http::Response::new(Body::empty()));
87        let events = Response::from(http::Response::new(Body::empty())).events();
88        send(&events);
89        send(&events.json::<serde_json::Value>());
90        send(&response.bytes_stream());
91        #[cfg(feature = "time")]
92        send(
93            &sse::Events::from_bytes(futures_util::stream::empty::<Result<&'static [u8], Error>>())
94                .idle_timeout(std::time::Duration::from_secs(1)),
95        );
96    }
97}