lychee_lib/types/
request.rs1use std::{convert::TryFrom, fmt::Display};
2
3use crate::{BasicAuthCredentials, ErrorKind, Uri};
4
5use super::InputSource;
6
7#[derive(Debug, PartialEq, Eq, Hash, Clone)]
9pub struct Request {
10 pub uri: Uri,
13
14 pub source: InputSource,
16
17 pub element: Option<String>,
21
22 pub attribute: Option<String>,
24
25 pub credentials: Option<BasicAuthCredentials>,
27}
28
29impl Request {
30 #[inline]
32 #[must_use]
33 pub const fn new(
34 uri: Uri,
35 source: InputSource,
36 element: Option<String>,
37 attribute: Option<String>,
38 credentials: Option<BasicAuthCredentials>,
39 ) -> Self {
40 Request {
41 uri,
42 source,
43 element,
44 attribute,
45 credentials,
46 }
47 }
48}
49
50impl Display for Request {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 write!(f, "{} ({})", self.uri, self.source)
53 }
54}
55
56impl TryFrom<Uri> for Request {
57 type Error = ErrorKind;
58
59 fn try_from(uri: Uri) -> Result<Self, Self::Error> {
60 Ok(Request::new(
61 uri.clone(),
62 InputSource::RemoteUrl(Box::new(uri.url)),
63 None,
64 None,
65 None,
66 ))
67 }
68}
69
70impl TryFrom<String> for Request {
71 type Error = ErrorKind;
72
73 fn try_from(s: String) -> Result<Self, Self::Error> {
74 let uri = Uri::try_from(s.as_str())?;
75 Ok(Request::new(uri, InputSource::String(s), None, None, None))
76 }
77}
78
79impl TryFrom<&str> for Request {
80 type Error = ErrorKind;
81
82 fn try_from(s: &str) -> Result<Self, Self::Error> {
83 let uri = Uri::try_from(s)?;
84 Ok(Request::new(
85 uri,
86 InputSource::String(s.to_owned()),
87 None,
88 None,
89 None,
90 ))
91 }
92}