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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! [`Userinfo`].
use crate::prelude::*;
impl BetterUrl {
/// If it has a visible userinfo.
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// assert!(!BetterUrl::new("https://example.com" ).unwrap().has_visible_userinfo());
/// assert!( BetterUrl::new("https://username@example.com" ).unwrap().has_visible_userinfo());
/// assert!( BetterUrl::new("https://:password@example.com" ).unwrap().has_visible_userinfo());
/// assert!( BetterUrl::new("https://username:password@example.com").unwrap().has_visible_userinfo());
/// ```
pub fn has_visible_userinfo(&self) -> bool {
self.username_start().is_some()
}
/// The [`Range::start`] of the userinfo.
fn userinfo_start(&self) -> Option<usize> {
self.username_start()
}
/// The [`Range::end`] of the userinfo.
fn userinfo_after(&self) -> Option<usize> {
self.password_after().or(self.username_after())
}
/// The [`Range`] of the userinfo.
fn userinfo_range(&self) -> Option<Range<usize>> {
Some(self.userinfo_start()? .. self.userinfo_after()?)
}
/// The visible userinfo as a [`str`].
///
/// If the userinfo is the empty string, and thus doesn't show in the URL, returns [`None`].
///
/// Thus, any [`str`] returned is guaranteed to be a substring of the URL.
pub fn visible_userinfo_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.userinfo_range()?)})
}
/// The userinfo as a [`str`].
///
/// Please note that in the case of an empty userinfo the returned `str` will not be a substring of the URL.
///
/// If you need that property, see [`Self::visible_userinfo_str`].
pub fn userinfo_str(&self) -> &str {
self.visible_userinfo_str().unwrap_or_default()
}
/// The visible [`Userinfo`].
///
/// See [`Self::visible_userinfo_str`] for details.
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let url = BetterUrl::new("https://example.com").unwrap();
///
/// assert!(url.visible_userinfo().is_none());
///
///
/// let url = BetterUrl::new("https://username@example.com").unwrap();
/// let userinfo = url.visible_userinfo().unwrap();
///
/// assert_eq!(userinfo.username_str (), "username");
/// assert_eq!(userinfo.visible_password_str(), None );
/// assert_eq!(userinfo.password_str (), "" );
///
///
/// let url = BetterUrl::new("https://:password@example.com").unwrap();
/// let userinfo = url.visible_userinfo().unwrap();
///
/// assert_eq!(userinfo.username_str (), "" );
/// assert_eq!(userinfo.visible_password_str(), Some("password"));
/// assert_eq!(userinfo.password_str (), "password" );
///
///
/// let url = BetterUrl::new("https://username:password@example.com").unwrap();
/// let userinfo = url.visible_userinfo().unwrap();
///
/// assert_eq!(userinfo.username_str (), "username" );
/// assert_eq!(userinfo.visible_password_str(), Some("password"));
/// assert_eq!(userinfo.password_str (), "password" );
/// ```
pub fn visible_userinfo(&self) -> Option<Userinfo<'_>> {
let raw = self.visible_userinfo_str()?;
let password_start = self.password_start().and_then(|password_start| NonZero::new(password_start - self.username_start()?));
Some(unsafe {
Userinfo::new_unchecked(raw, password_start)
})
}
/// The [`Userinfo`].
///
/// See [`Self::userinfo_str`] for details.
/// # Examples
/// ```
/// use better_url::prelude::*;
///
/// let url = BetterUrl::new("https://example.com").unwrap();
///
/// assert!(url.visible_userinfo().is_none());
///
///
/// let url = BetterUrl::new("https://username@example.com").unwrap();
/// let userinfo = url.userinfo();
///
/// assert_eq!(userinfo.username_str (), "username");
/// assert_eq!(userinfo.visible_password_str(), None );
/// assert_eq!(userinfo.password_str (), "" );
///
///
/// let url = BetterUrl::new("https://:password@example.com").unwrap();
/// let userinfo = url.userinfo();
///
/// assert_eq!(userinfo.username_str (), "" );
/// assert_eq!(userinfo.visible_password_str(), Some("password"));
/// assert_eq!(userinfo.password_str (), "password" );
///
///
/// let url = BetterUrl::new("https://username:password@example.com").unwrap();
/// let userinfo = url.userinfo();
///
/// assert_eq!(userinfo.username_str (), "username" );
/// assert_eq!(userinfo.visible_password_str(), Some("password"));
/// assert_eq!(userinfo.password_str (), "password" );
/// ```
pub fn userinfo(&self) -> Userinfo<'_> {
self.visible_userinfo().unwrap_or(unsafe {Userinfo::new_unchecked("", None)})
}
}