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
//! Origin stuff.
use crate::prelude::*;
impl BetterUrl {
/// If it has a domain origin.
pub fn has_domain_origin(&self) -> bool {
self.domain_details().is_some_and(DomainHostDetails::has_middle)
}
/// The [`Range`] of the domain origin.
fn domain_origin_thing(&self) -> Option<Range<usize>> {
let hs = self.host_start ()?;
let ha = self.host_after ()?;
let dd = self.domain_details()?;
Some(dd.middle_start()? + hs .. ha - dd.is_fqdn() as usize)
}
/// The domain origin as a [`str`].
pub fn domain_origin_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.domain_origin_thing()?)})
}
/// The domain origin as a [`DomainSegments`].
pub fn domain_origin(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.domain_origin_str()?)})
}
/// The domain origin segments as [`str`]s.
pub fn domain_origin_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.domain_origin_str()?)))
}
/// The domain origin segments as [`DomainSegment`]s.
pub fn domain_origin_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.domain_origin_segment_strs()?)})
}
/// The `index`th domain origin segment as a [`str`].
pub fn domain_origin_segment_str(&self, index: isize) -> Option<&str> {
self.domain_origin_segment_strs()?.neg_nth(index)
}
/// The `index`th domain origin segment as a [`DomainSegment`].
pub fn domain_origin_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.domain_origin_segments()?.neg_nth(index)
}
/// The range of the domain origin segments as a [`str`].
pub fn domain_origin_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.domain_origin_segments()?.range_str(range)
}
/// The range of the domain origin segments as a [`DomainSegments`].
pub fn domain_origin_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.domain_origin_segments()?.range(range)
}
/// [`DomainHost::set_origin`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_origin`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_origin<'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_origin(value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_origin_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_origin_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_origin_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_origin_segment(index, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::set_origin_range`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::set_origin_range`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn set_domain_origin_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_origin_range(range, value)? {
self.set_host(domain.into_owned())?;
Ok(true)
} else {
Ok(false)
}
}
/// [`DomainHost::insert_origin_segment`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_origin_segment`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_origin_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_origin_segment(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
/// [`DomainHost::insert_origin_segments`].
/// # Errors
/// If [`Self::domain`] returns [`None`], returns the error [`NoDomain`].
///
/// If [`DomainHost::insert_origin_segments`] returns an error, that error is returned.
///
/// If [`Self::set_host`] returns an error, that error is returned.
pub fn insert_domain_origin_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_origin_segments(index, value)?;
self.set_host(domain.into_owned())?;
Ok(())
}
}