Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Air/AirClient/
DownloadStream.rs

1//! Wrapper for an asynchronous Air download stream. Adapts the tonic
2//! streaming API into a `next().await` iterator that yields
3//! `DownloadStreamChunk::Struct` items. Cfg-gated on `AirIntegration`
4//! because the inner type lives in `AirLibrary::Vine::Generated::air`.
5
6#[cfg(feature = "AirIntegration")]
7use CommonLibrary::Error::CommonError::CommonError;
8
9#[cfg(feature = "AirIntegration")]
10use crate::{Air::AirClient::DownloadStreamChunk, dev_log};
11
12#[cfg(feature = "AirIntegration")]
13pub struct Struct {
14	inner:tonic::codec::Streaming<AirLibrary::Vine::Generated::air::DownloadStreamResponse>,
15}
16
17#[cfg(feature = "AirIntegration")]
18impl Struct {
19	pub fn new(Stream:tonic::codec::Streaming<AirLibrary::Vine::Generated::air::DownloadStreamResponse>) -> Self {
20		Self { inner:Stream }
21	}
22
23	/// Returns the next chunk from the stream. `None` when the stream ends.
24	pub async fn next(&mut self) -> Option<Result<DownloadStreamChunk::Struct, CommonError>> {
25		match futures_util::stream::StreamExt::next(&mut self.inner).await {
26			Some(Ok(Response)) => {
27				Some(Ok(DownloadStreamChunk::Struct {
28					data:Response.chunk,
29					total_size:Response.total_size,
30					downloaded:Response.downloaded,
31					completed:Response.completed,
32					error:Response.error,
33				}))
34			},
35
36			Some(Err(Error)) => {
37				dev_log!("grpc", "error: [DownloadStream] Stream error: {}", Error);
38
39				Some(Err(CommonError::IPCError { Description:format!("Stream error: {}", Error) }))
40			},
41
42			None => None,
43		}
44	}
45}