1use crate::{ProgressEntry, PullResult, PullStream, Puller, PullerError};
2use futures::stream;
3use std::{sync::Arc, vec::Vec};
4
5#[must_use]
6pub fn build_mock_data(size: usize) -> Vec<u8> {
7 #[allow(clippy::cast_possible_truncation)]
8 (0..size).map(|i| (i % 256) as u8).collect()
9}
10
11#[derive(Debug, Clone)]
12pub struct MockPuller(pub Arc<[u8]>);
13impl MockPuller {
14 #[must_use]
15 pub fn new(data: &[u8]) -> Self {
16 Self(Arc::from(data))
17 }
18}
19impl Puller for MockPuller {
20 type Error = ();
21 async fn pull(
22 &mut self,
23 range: Option<&ProgressEntry>,
24 ) -> PullResult<impl PullStream<Self::Error>, Self::Error> {
25 let data = match range {
26 #[allow(clippy::cast_possible_truncation)]
27 Some(r) => &self.0[r.start as usize..r.end as usize],
28 None => &self.0,
29 };
30 Ok(stream::iter(
31 data.chunks(2).map(|c| Ok(c.iter().copied().collect())),
32 ))
33 }
34}
35impl PullerError for () {}