lychee_lib/types/
request.rs1use std::{borrow::Cow, convert::TryFrom, fmt::Display};
2
3use crate::{BasicAuthCredentials, ErrorKind, Uri};
4
5use super::ResolvedInputSource;
6
7#[derive(Debug, PartialEq, Eq, Hash, Clone)]
9pub struct Request {
10 pub uri: Uri,
13
14 pub source: ResolvedInputSource,
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: ResolvedInputSource,
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 ResolvedInputSource::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(
76 uri,
77 ResolvedInputSource::String(Cow::Owned(s)),
78 None,
79 None,
80 None,
81 ))
82 }
83}
84
85impl TryFrom<&str> for Request {
86 type Error = ErrorKind;
87
88 fn try_from(s: &str) -> Result<Self, Self::Error> {
89 let uri = Uri::try_from(s)?;
90 Ok(Request::new(
91 uri,
92 ResolvedInputSource::String(Cow::Owned(s.to_owned())),
93 None,
94 None,
95 None,
96 ))
97 }
98}