mongo_file_center/
file_data.rs

1use std::io::{self, Cursor};
2
3use crate::tokio_stream::{Stream, StreamExt};
4
5/// To represent the file data retrieved from MongoDB.
6#[derive(Educe)]
7#[educe(Debug)]
8pub enum FileData {
9    Buffer(Vec<u8>),
10    Stream(
11        #[educe(Debug(ignore))]
12        Box<dyn Stream<Item = Result<Cursor<Vec<u8>>, io::Error>> + Unpin + Send>,
13    ),
14}
15
16impl FileData {
17    /// Turn into a `Vec<u8>` instance.
18    #[inline]
19    pub async fn into_vec(self) -> Result<Vec<u8>, io::Error> {
20        match self {
21            FileData::Buffer(v) => Ok(v),
22            FileData::Stream(mut f) => {
23                let mut buffer = Vec::new();
24
25                while let Some(chunk) = f.next().await {
26                    let chunk = chunk?.into_inner();
27
28                    buffer.extend_from_slice(&chunk);
29                }
30
31                Ok(buffer)
32            },
33        }
34    }
35}