Skip to main content

spider_util/
response_type.rs

1//! Response types for the spider-lib framework.
2//!
3//! This module provides abstractions for handling both regular and stream responses.
4
5use crate::response::Response;
6#[cfg(feature = "stream")]
7use crate::stream_response::StreamResponse;
8use std::fmt::Debug;
9
10/// An enum that represents either a regular response or a stream response.
11/// This allows the framework to handle both response types in a unified way.
12#[derive(Debug)]
13pub enum ResponseType {
14    /// A regular response with the full body loaded in memory.
15    Regular(Response),
16
17    /// A stream response where the body is processed as a stream.
18    #[cfg(feature = "stream")]
19    Stream(StreamResponse),
20}
21
22impl ResponseType {
23    /// Converts a regular response to the enum.
24    pub fn from_regular(response: Response) -> Self {
25        ResponseType::Regular(response)
26    }
27
28    /// Gets the URL from the response regardless of type.
29    pub fn url(&self) -> &url::Url {
30        match self {
31            ResponseType::Regular(response) => &response.url,
32            #[cfg(feature = "stream")]
33            ResponseType::Stream(response) => &response.url,
34        }
35    }
36
37    /// Gets the status code from the response regardless of type.
38    pub fn status(&self) -> http::StatusCode {
39        match self {
40            ResponseType::Regular(response) => response.status,
41            #[cfg(feature = "stream")]
42            ResponseType::Stream(response) => response.status,
43        }
44    }
45
46    /// Gets the headers from the response regardless of type.
47    pub fn headers(&self) -> &reqwest::header::HeaderMap {
48        match self {
49            ResponseType::Regular(response) => &response.headers,
50            #[cfg(feature = "stream")]
51            ResponseType::Stream(response) => &response.headers,
52        }
53    }
54
55    /// Converts a stream response to the enum.
56    #[cfg(feature = "stream")]
57    pub fn from_stream(response: StreamResponse) -> Self {
58        ResponseType::Stream(response)
59    }
60
61    /// Converts the response to a regular response.
62    /// For stream responses, this will consume the stream and collect all data.
63    #[cfg(feature = "stream")]
64    pub async fn to_regular(self) -> Result<Response, crate::error::SpiderError> {
65        match self {
66            ResponseType::Regular(response) => Ok(response),
67            ResponseType::Stream(stream_response) => stream_response
68                .to_response()
69                .await
70                .map_err(|e| crate::error::SpiderError::IoError(e.to_string())),
71        }
72    }
73
74    /// Converts the response to a stream response.
75    /// For regular responses, this will wrap the body in a stream.
76    #[cfg(feature = "stream")]
77    pub async fn to_stream(self) -> Result<StreamResponse, crate::error::SpiderError> {
78        match self {
79            ResponseType::Regular(response) => response
80                .to_stream_response()
81                .await
82                .map_err(|e| crate::error::SpiderError::IoError(e.to_string())),
83            ResponseType::Stream(stream_response) => Ok(stream_response),
84        }
85    }
86
87    /// Gets the request URL from the response regardless of type.
88    pub fn request_url(&self) -> &url::Url {
89        match self {
90            ResponseType::Regular(response) => &response.request_url,
91            #[cfg(feature = "stream")]
92            ResponseType::Stream(response) => &response.request_url,
93        }
94    }
95}
96