1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! # axum HTTP streaming body support for different formats:
//! - JSON array stream format
//! - JSON Lines (NL/NewLines) format
//! - CSV stream format
//! - Protobuf len-prefixed stream format
//!
//! [JSON Streaming](https://en.wikipedia.org/wiki/JSON_streaming) is a term referring to streaming a
//! stream of element as independent JSON objects as a continuous HTTP request or response.
//!
//! This type of responses are useful when you are reading huge stream of objects from some source (such as database, file, etc)
//! and want to avoid huge memory allocations to store on the server side.
//!
//! # Example
//!
//! ```rust
//! use axum::{
//! Router,
//! routing::get,
//! http::{StatusCode, header::CONTENT_TYPE},
//! response::{Response, IntoResponse},
//! };
//! use futures::Stream;
//! use axum_streams::*;
//! use serde::Serialize;
//!
//! #[derive(Debug, Clone, Serialize)]
//! struct MyTestStructure {
//! some_test_field: String
//! }
//!
//! // Your possibly stream of objects
//! fn my_source_stream() -> impl Stream<Item=MyTestStructure> {
//! // Simulating a stream with a plain vector and throttling to show how it works
//! use tokio_stream::StreamExt;
//! futures::stream::iter(vec![
//! MyTestStructure {
//! some_test_field: "test1".to_string()
//! }; 1000
//! ]).throttle(std::time::Duration::from_millis(50))
//! }
//!
//! // Route implementation:
//! async fn test_json_array_stream() -> impl IntoResponse {
//! StreamBodyAs::json_array(my_source_stream())
//! }
//! async fn test_json_nl_stream() -> impl IntoResponse {
//! StreamBodyAs::json_nl(my_source_stream())
//! }
//!
//! async fn test_csv_stream() -> impl IntoResponse {
//! StreamBodyAs::csv(my_source_stream())
//! // Which is the same as:
//! // StreamBodyWith::new(CsvStreamFormat::new(
//! // true, // with_header
//! // b',' // CSV delimiter
//! //), my_source_stream())
//! }
//!
//! ```
//! ## Need client support?
//! There is the same functionality for:
//! - [reqwest-streams](https://github.com/abdolence/reqwest-streams-rs).
//!
pub use StreamBodyAs;
pub use JsonArrayStreamFormat;
pub use JsonNewLineStreamFormat;
pub use CsvStreamFormat;
pub use ProtobufStreamFormat;