Skip to main content

reala_subscription/
util.rs

1//! Some utilities for subscription fetching and parsing.
2
3use std::str::FromStr;
4
5wrapper_lite::wrapper!(
6    #[wrapper_impl(Debug)]
7    #[wrapper_impl(AsRef)]
8    #[wrapper_impl(Deref)]
9    #[wrapper_impl(From)]
10    #[derive(Clone, PartialEq, Eq, Hash)]
11    /// A HTTP URI.
12    pub struct Uri(http::Uri);
13);
14
15impl FromStr for Uri {
16    type Err = http::uri::InvalidUri;
17
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        s.parse().map(Self::const_from)
20    }
21}
22
23impl serde::Serialize for Uri {
24    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
25    where
26        S: serde::Serializer,
27    {
28        serializer.serialize_str(&self.to_string())
29    }
30}
31
32impl<'de> serde::Deserialize<'de> for Uri {
33    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
34    where
35        D: serde::Deserializer<'de>,
36    {
37        <&str>::deserialize(deserializer)?
38            .parse()
39            .map(Self::const_from)
40            .map_err(serde::de::Error::custom)
41    }
42}
43
44wrapper_lite::wrapper!(
45    #[wrapper_impl(Debug)]
46    #[wrapper_impl(AsRef)]
47    #[wrapper_impl(Deref)]
48    #[wrapper_impl(From)]
49    #[derive(Clone, PartialEq, Eq, Hash)]
50    /// A HTTP / Socks proxy for [`ureq`].
51    pub struct Proxy(ureq::Proxy);
52);
53
54impl FromStr for Proxy {
55    type Err = ureq::Error;
56
57    fn from_str(s: &str) -> Result<Self, Self::Err> {
58        ureq::Proxy::new(s).map(Self::const_from)
59    }
60}
61
62impl serde::Serialize for Proxy {
63    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64    where
65        S: serde::Serializer,
66    {
67        serializer.serialize_str(&self.uri().to_string())
68    }
69}
70
71impl<'de> serde::Deserialize<'de> for Proxy {
72    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
73    where
74        D: serde::Deserializer<'de>,
75    {
76        ureq::Proxy::new(<&str>::deserialize(deserializer)?)
77            .map(Self::const_from)
78            .map_err(serde::de::Error::custom)
79    }
80}