identity_core/common/
url.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::ops::Deref;
use core::ops::DerefMut;
use core::str::FromStr;

use serde;
use serde::Deserialize;
use serde::Serialize;

use crate::common::KeyComparable;
use crate::error::Error;
use crate::error::Result;

/// A parsed URL.
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct Url(::url::Url);

impl Url {
  /// Parses an absolute [`Url`] from the given input string.
  pub fn parse(input: impl AsRef<str>) -> Result<Self> {
    ::url::Url::parse(input.as_ref()).map_err(Error::InvalidUrl).map(Self)
  }

  /// Consumes the [`Url`] and returns the value as a `String`.
  pub fn into_string(self) -> String {
    self.0.to_string()
  }

  /// Parses the given input string as a [`Url`], with `self` as the base Url.
  pub fn join(&self, input: impl AsRef<str>) -> Result<Self> {
    self.0.join(input.as_ref()).map_err(Error::InvalidUrl).map(Self)
  }
}

impl Debug for Url {
  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
    f.write_fmt(format_args!("Url({})", self.0.as_str()))
  }
}

impl Display for Url {
  fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
    f.write_str(self.0.as_str())
  }
}

impl Deref for Url {
  type Target = ::url::Url;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl DerefMut for Url {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl From<::url::Url> for Url {
  fn from(other: ::url::Url) -> Self {
    Self(other)
  }
}

impl FromStr for Url {
  type Err = Error;

  fn from_str(string: &str) -> Result<Self, Self::Err> {
    Self::parse(string)
  }
}

impl<T> PartialEq<T> for Url
where
  T: AsRef<str> + ?Sized,
{
  fn eq(&self, other: &T) -> bool {
    self.as_str() == other.as_ref()
  }
}

impl KeyComparable for Url {
  type Key = Url;

  #[inline]
  fn key(&self) -> &Self::Key {
    self
  }
}