fast_down/http/
mod.rs

1mod prefetch;
2mod puller;
3pub use prefetch::*;
4pub use puller::*;
5
6use bytes::Bytes;
7use fast_pull::ProgressEntry;
8use std::{fmt::Debug, future::Future};
9use url::Url;
10
11pub trait HttpClient: Clone + Send + Sync + Unpin {
12    type RequestBuilder: HttpRequestBuilder;
13    fn get(&self, url: Url, range: Option<ProgressEntry>) -> Self::RequestBuilder;
14}
15pub trait HttpRequestBuilder {
16    type Response: HttpResponse;
17    type RequestError: Send + Debug;
18    fn send(self) -> impl Future<Output = Result<Self::Response, Self::RequestError>> + Send;
19}
20pub trait HttpResponse: Send + Unpin {
21    type Headers: HttpHeaders;
22    type ChunkError: Send + Debug;
23    fn headers(&self) -> &Self::Headers;
24    fn url(&self) -> &Url;
25    fn chunk(&mut self) -> impl Future<Output = Result<Option<Bytes>, Self::ChunkError>> + Send;
26}
27pub trait HttpHeaders {
28    type GetHeaderError: Send + Debug;
29    fn get(&self, header: &str) -> Result<&str, Self::GetHeaderError>;
30}
31
32pub type GetResponse<Client> =
33    <<Client as HttpClient>::RequestBuilder as HttpRequestBuilder>::Response;
34pub type GetRequestError<Client> =
35    <<Client as HttpClient>::RequestBuilder as HttpRequestBuilder>::RequestError;
36pub type GetChunkError<Client> = <GetResponse<Client> as HttpResponse>::ChunkError;
37pub type GetHeader<Client> = <GetResponse<Client> as HttpResponse>::Headers;
38pub type GetHeaderError<Client> = <GetHeader<Client> as HttpHeaders>::GetHeaderError;
39
40#[derive(thiserror::Error, Debug)]
41pub enum HttpError<Client: HttpClient> {
42    Request(GetRequestError<Client>),
43    Chunk(GetChunkError<Client>),
44    GetHeader(GetHeaderError<Client>),
45    Irrecoverable,
46}