identity_core/common/
url.rs1use core::fmt::Debug;
5use core::fmt::Display;
6use core::fmt::Formatter;
7use core::ops::Deref;
8use core::ops::DerefMut;
9use core::str::FromStr;
10
11use serde;
12use serde::Deserialize;
13use serde::Serialize;
14
15use crate::common::KeyComparable;
16use crate::error::Error;
17use crate::error::Result;
18
19#[derive(Clone, Hash, Eq, PartialOrd, Ord, Deserialize, Serialize)]
21#[repr(transparent)]
22#[serde(transparent)]
23pub struct Url(::url::Url);
24
25impl Url {
26 pub fn parse(input: impl AsRef<str>) -> Result<Self> {
28 ::url::Url::parse(input.as_ref()).map_err(Error::InvalidUrl).map(Self)
29 }
30
31 pub fn into_string(self) -> String {
33 self.0.to_string()
34 }
35
36 pub fn join(&self, input: impl AsRef<str>) -> Result<Self> {
38 self.0.join(input.as_ref()).map_err(Error::InvalidUrl).map(Self)
39 }
40}
41
42impl Debug for Url {
43 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
44 f.write_fmt(format_args!("Url({})", self.0.as_str()))
45 }
46}
47
48impl Display for Url {
49 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
50 f.write_str(self.0.as_str())
51 }
52}
53
54impl Deref for Url {
55 type Target = ::url::Url;
56
57 fn deref(&self) -> &Self::Target {
58 &self.0
59 }
60}
61
62impl DerefMut for Url {
63 fn deref_mut(&mut self) -> &mut Self::Target {
64 &mut self.0
65 }
66}
67
68impl From<::url::Url> for Url {
69 fn from(other: ::url::Url) -> Self {
70 Self(other)
71 }
72}
73
74impl FromStr for Url {
75 type Err = Error;
76
77 fn from_str(string: &str) -> Result<Self, Self::Err> {
78 Self::parse(string)
79 }
80}
81
82impl<T> PartialEq<T> for Url
83where
84 T: AsRef<str> + ?Sized,
85{
86 fn eq(&self, other: &T) -> bool {
87 self.as_str() == other.as_ref()
88 }
89}
90
91impl KeyComparable for Url {
92 type Key = Url;
93
94 #[inline]
95 fn key(&self) -> &Self::Key {
96 self
97 }
98}
99
100impl AsRef<str> for Url {
101 fn as_ref(&self) -> &str {
102 self.as_str()
103 }
104}