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
//! Prefix stuff.
use crate::prelude::*;
impl BetterUrl {
/// If it has a domain prefix.
pub fn has_domain_prefix(&self) -> bool {
self.domain_details().is_some_and(DomainHostDetails::has_prefix)
}
/// The [`Range`] of the domain prefix.
fn domain_prefix_thing(&self) -> Option<Range<usize>> {
let hs = self.host_start()?;
Some(hs .. self.domain_details()?.prefix_after()? + hs)
}
/// The domain prefix as a [`str`].
pub fn domain_prefix_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.domain_prefix_thing()?)})
}
/// The domain prefix as a [`DomainSegments`].
pub fn domain_prefix(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.domain_prefix_str()?)})
}
/// The domain prefix segments as [`str`]s.
pub fn domain_prefix_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.domain_prefix_str()?)))
}
/// The domain prefix's [`DomainSegmentsIter`].
pub fn domain_prefix_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.domain_prefix_segment_strs()?)})
}
/// The `index`th domain prefix segment as a [`str`].
pub fn domain_prefix_segment_str(&self, index: isize) -> Option<&str> {
self.domain_prefix_segment_strs()?.neg_nth(index)
}
/// The `index`th domain prefix segment as a [`DomainSegment`].
pub fn domain_prefix_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.domain_prefix_segments()?.neg_nth(index)
}
/// The range of domain prefix segments as a [`str`].
pub fn domain_prefix_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.domain_prefix_segments()?.range_str(range)
}
/// The range of domain prefix segments as a [`DomainSegments`].
pub fn domain_prefix_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.domain_prefix_segments()?.range(range)
}
/// [`DomainHost::set_prefix`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_prefix`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_prefix<'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_prefix(value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_prefix_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_prefix_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_prefix_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_prefix_segment(index, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_prefix_range`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_prefix_range`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_prefix_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_prefix_range(range, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::insert_prefix_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_prefix_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_prefix_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_prefix_segment(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
/// [`DomainHost::insert_prefix_segments`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_prefix_segments`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_prefix_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_prefix_segments(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
}