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
//! Labels stuff.
use crate::prelude::*;
impl BetterUrl {
/// If it has a domain labels.
pub fn has_domain_labels(&self) -> bool {
self.domain_details().is_some()
}
/// The [`Range`] of the domain labels.
fn domain_labels_thing(&self) -> Option<Range<usize>> {
let hs = self.host_start ()?;
let ha = self.host_after ()?;
let dd = self.domain_details()?;
Some(hs .. ha - dd.is_fqdn() as usize)
}
/// The domain labels as a [`str`].
pub fn domain_labels_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.domain_labels_thing()?)})
}
/// The domain labels as a [`DomainSegments`].
pub fn domain_labels(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.domain_labels_str()?)})
}
/// The domain labels segments as [`str`]s.
pub fn domain_labels_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.domain_labels_str()?)))
}
/// The domain labels segments as a [`DomainSegmentsIter`].
pub fn domain_labels_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.domain_labels_segment_strs()?)})
}
/// The `index`th domain labels segment as a [`str`].
pub fn domain_labels_segment_str(&self, index: isize) -> Option<&str> {
self.domain_labels_segment_strs()?.neg_nth(index)
}
/// The `index`th domain labels segment as a [`DomainSegment`].
pub fn domain_labels_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.domain_labels_segments()?.neg_nth(index)
}
/// The range of the domain labels segments as a [`str`].
pub fn domain_labels_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.domain_labels_segments()?.range_str(range)
}
/// The range of the domain labels segments as a [`DomainSegments`].
pub fn domain_labels_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.domain_labels_segments()?.range(range)
}
/// [`DomainHost::set_labels`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_labels`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_labels<'b, T: TryInto<DomainSegments<'b>>>(&mut self, value: T) -> Result<bool, SetHostError> where SetDomainError: From<T::Error> {
let mut domain = self.domain().ok_or(NoDomain)?;
if domain.set_labels(value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_labels_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_labels_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_labels_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_labels_segment(index, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_labels_range`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_labels_range`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_labels_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_labels_range(range, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::insert_labels_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_labels_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_labels_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_labels_segment(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
/// [`DomainHost::insert_labels_segments`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_labels_segments`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_labels_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_labels_segments(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
}