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
//! Suffix stuff.
use crate::prelude::*;
impl BetterUrl {
/// If it has a domain suffix.
pub fn has_domain_suffix(&self) -> bool {
self.domain_details().is_some()
}
/// The [`Range`] of the domain suffix.
fn domain_suffix_thing(&self) -> Option<Range<usize>> {
let hs = self.host_start ()?;
let ha = self.host_after ()?;
let dd = self.domain_details()?;
Some(hs + dd.suffix_start() .. ha - dd.is_fqdn() as usize)
}
/// The domain suffix as a [`str`].
pub fn domain_suffix_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.domain_suffix_thing()?)})
}
/// The domain suffix as a [`DomainSegments`].
pub fn domain_suffix(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.domain_suffix_str()?)})
}
/// The domain suffix segments as [`str`]s.
pub fn domain_suffix_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.domain_suffix_str()?)))
}
/// The domain suffix [`DomainSegmentsIter`].
pub fn domain_suffix_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.domain_suffix_segment_strs()?)})
}
/// The `index`th domain suffix segment as a [`str`].
pub fn domain_suffix_segment_str(&self, index: isize) -> Option<&str> {
self.domain_suffix_segment_strs()?.neg_nth(index)
}
/// The `index`th domain suffix segment as a [`DomainSegment`].
pub fn domain_suffix_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.domain_suffix_segments()?.neg_nth(index)
}
/// The range of domain suffix segments as a [`str`].
pub fn domain_suffix_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.domain_suffix_segments()?.range_str(range)
}
/// The range of domain suffix segments as a [`DomainSegments`].
pub fn domain_suffix_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.domain_suffix_segments()?.range(range)
}
/// [`DomainHost::set_suffix`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_suffix`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_suffix<'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_suffix(value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_suffix_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_suffix_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_suffix_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_suffix_segment(index, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_suffix_range`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_suffix_range`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_suffix_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_suffix_range(range, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::insert_suffix_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_suffix_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_suffix_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_suffix_segment(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
/// [`DomainHost::insert_suffix_segments`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_suffix_segments`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_suffix_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_suffix_segments(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
}