spider_util/
response_type.rs1use crate::response::Response;
6#[cfg(feature = "stream")]
7use crate::stream_response::StreamResponse;
8use std::fmt::Debug;
9
10#[derive(Debug)]
13pub enum ResponseType {
14 Regular(Response),
16
17 #[cfg(feature = "stream")]
19 Stream(StreamResponse),
20}
21
22impl ResponseType {
23 pub fn from_regular(response: Response) -> Self {
25 ResponseType::Regular(response)
26 }
27
28 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 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 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 #[cfg(feature = "stream")]
57 pub fn from_stream(response: StreamResponse) -> Self {
58 ResponseType::Stream(response)
59 }
60
61 #[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 #[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 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