nv_redfish_core/
upload.rs1use core::pin::Pin;
17use std::error::Error as StdError;
18use std::fmt;
19use std::time::Duration;
20
21use futures_io::AsyncRead;
22
23const OEM_PREFIX: &str = "Oem";
24
25pub trait UploadReader: AsyncRead + Send + 'static {}
27
28impl<T> UploadReader for T where T: AsyncRead + Send + 'static {}
29
30pub struct DataStream<R> {
32 pub name: String,
34
35 pub reader: R,
37
38 pub content_length: Option<u64>,
40}
41
42impl<R> DataStream<R> {
43 #[must_use]
45 pub fn new(name: impl Into<String>, reader: R) -> Self {
46 Self {
47 name: name.into(),
48 reader,
49 content_length: None,
50 }
51 }
52
53 #[must_use]
55 pub const fn with_content_length(mut self, content_length: u64) -> Self {
56 self.content_length = Some(content_length);
57 self
58 }
59}
60
61pub struct UploadStream<R> {
63 pub reader: R,
65
66 pub content_length: Option<u64>,
68}
69
70impl<R> UploadStream<R> {
71 #[must_use]
73 pub const fn new(reader: R) -> Self {
74 Self {
75 reader,
76 content_length: None,
77 }
78 }
79
80 #[must_use]
82 pub const fn with_content_length(mut self, content_length: u64) -> Self {
83 self.content_length = Some(content_length);
84 self
85 }
86}
87
88#[derive(Debug, Clone, PartialEq, Eq)]
90pub struct OemMultipartPartNameError {
91 pub name: String,
93}
94
95impl fmt::Display for OemMultipartPartNameError {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(
98 f,
99 "OEM multipart part name must start with Oem: {}",
100 self.name
101 )
102 }
103}
104
105impl StdError for OemMultipartPartNameError {}
106
107pub type OemMultipartPartReader = Pin<Box<dyn AsyncRead + Send + 'static>>;
109
110pub struct OemMultipartPart {
112 pub name: String,
114
115 pub reader: OemMultipartPartReader,
117
118 pub content_type: Option<String>,
120
121 pub content_length: Option<u64>,
123}
124
125impl OemMultipartPart {
126 #[must_use]
128 pub fn is_name_valid(&self) -> bool {
129 self.name.starts_with(OEM_PREFIX)
130 }
131}
132
133impl OemMultipartPart {
134 pub fn new(
140 name: impl Into<String>,
141 reader: impl UploadReader,
142 ) -> Result<Self, OemMultipartPartNameError> {
143 let name = name.into();
144
145 if !name.starts_with(OEM_PREFIX) {
146 return Err(OemMultipartPartNameError { name });
147 }
148
149 Ok(Self {
150 name,
151 reader: Box::pin(reader),
152 content_type: None,
153 content_length: None,
154 })
155 }
156
157 #[must_use]
159 pub fn with_content_type(mut self, content_type: impl Into<String>) -> Self {
160 self.content_type = Some(content_type.into());
161 self
162 }
163
164 #[must_use]
166 pub const fn with_content_length(mut self, content_length: u64) -> Self {
167 self.content_length = Some(content_length);
168 self
169 }
170}
171
172pub struct MultipartUpdateRequest<'a, U, V> {
174 pub update_parameters: &'a V,
176
177 pub update_stream: DataStream<U>,
179
180 pub oem_parts: Vec<OemMultipartPart>,
182
183 pub upload_timeout: Duration,
185}
186
187pub struct HttpPushUriUpdateRequest<U> {
189 pub update_stream: UploadStream<U>,
191
192 pub upload_timeout: Duration,
194}