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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Normal stuff.
use crate::prelude::*;
impl BetterUrl {
/// If it has a domain normal.
pub fn has_domain_normal(&self) -> bool {
self.domain_details().is_some()
}
/// The [`Range`] of the domain normal.
fn domain_normal_thing(&self) -> Option<Range<usize>> {
let hs = self.host_start ()?;
let ha = self.host_after ()?;
let dd = self.domain_details()?;
Some(match dd.prefix_is_www() {
false => hs .. ha - dd.is_fqdn() as usize,
true => hs + (dd.data1 & (isize::MAX as usize)) .. ha - dd.is_fqdn() as usize,
})
}
/// The domain normal as a [`str`].
pub fn domain_normal_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.domain_normal_thing()?)})
}
/// The domain normal as a [`DomainSegments`].
pub fn domain_normal(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.domain_normal_str()?)})
}
/// The domain normal segments as [`str`]s.
pub fn domain_normal_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.domain_normal_str()?)))
}
/// The domain normal segments as [`DomainSegment`]s.
pub fn domain_normal_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.domain_normal_segment_strs()?)})
}
/// The `index`th domain normal segment as a [`str`].
pub fn domain_normal_segment_str(&self, index: isize) -> Option<&str> {
self.domain_normal_segment_strs()?.neg_nth(index)
}
/// The `index`th domain normal segment as a [`DomainSegment`].
pub fn domain_normal_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.domain_normal_segments()?.neg_nth(index)
}
/// The range of the domain normal segments as a [`str`].
pub fn domain_normal_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.domain_normal_segments()?.range_str(range)
}
/// The range of the domain normal segments as a [`DomainSegments`].
pub fn domain_normal_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.domain_normal_segments()?.range(range)
}
/// [`DomainHost::set_normal`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_normal`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_normal<'b, T: TryInto<DomainSegments<'b>>>(&mut self, value: Option<T>) -> Result<bool, SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
if domain.set_normal(value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_normal_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_normal_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_normal_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: Option<T>) -> Result<bool, SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
if domain.set_normal_segment(index, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_normal_range`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_normal_range`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_normal_range<'b, T: TryInto<DomainSegments<'b>>, B: RangeBounds<isize>>(&mut self, range: B, value: Option<T>) -> Result<bool, SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
if domain.set_normal_range(range, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::insert_normal_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_normal_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_normal_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
domain.insert_normal_segment(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
/// [`DomainHost::insert_normal_segments`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_normal_segments`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_normal_segments<'b, T: TryInto<DomainSegments<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
domain.insert_normal_segments(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
}