use crate::prelude::*;
impl DomainHost<'_> {
pub fn has_origin(&self) -> bool {
self.has_middle()
}
fn origin_start(&self) -> Option<usize> {
self.middle_start()
}
fn origin_after(&self) -> Option<usize> {
self.has_middle().then_some(self.suffix_after())
}
pub(crate) fn origin_thing(&self) -> Option<Range<usize>> {
Some(self.origin_start()? .. self.origin_after()?)
}
pub fn origin_str(&self) -> Option<&str> {
Some(unsafe {self.as_str().get_unchecked(self.origin_thing()?)})
}
pub fn origin(&self) -> Option<DomainSegments<'_>> {
Some(unsafe {DomainSegments::new_unchecked(self.origin_str()?)})
}
pub fn origin_segment_strs(&self) -> Option<SplitDots<'_>> {
Some(SplitDots(Some(self.origin_str()?)))
}
pub fn origin_segments(&self) -> Option<DomainSegmentsIter<'_>> {
Some(unsafe {DomainSegmentsIter::new_unchecked(self.origin_segment_strs()?)})
}
pub fn origin_segment_str(&self, index: isize) -> Option<&str> {
self.origin_segment_strs()?.neg_nth(index)
}
pub fn origin_segment(&self, index: isize) -> Option<DomainSegment<'_>> {
self.origin_segments()?.neg_nth(index)
}
pub fn origin_range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&str> {
self.origin_segments()?.range_str(range)
}
pub fn origin_range<B: RangeBounds<isize>>(&self, range: B) -> Option<DomainSegments<'_>> {
self.origin_segments()?.range(range)
}
pub fn set_origin<'b, T: TryInto<DomainSegments<'b>>>(&mut self, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
let replace = self.origin_str().unwrap_or(self.suffix_str());
match value.map(TryInto::try_into).transpose()? {
Some(new) if replace == new => return Ok(false),
Some(new) => match self.host.split_around_substr(replace) {
("", "" ) if new.is_empty () => Err(CantBeEmpty)?,
(_ , "" ) if new.last_is_empty () => Err(NonFqdnCantEndInEmpty)?,
(_ , "" ) if new.ends_in_a_number() => Err(CantEndInANumber)?,
(_ , ".") if new.last_is_a_number() => Err(CantEndInANumber)?,
_ => self.host.replace_substr(replace, new.as_str())
},
None => match self.host.split_around_substr(replace) {
("", "" | ".") => Err(CantBeEmpty)?,
(x , "" ) if ends_in_empty (x) => Err(NonFqdnCantEndInEmpty)?,
(x , "" ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
(x , "." ) if last_is_a_number(x) => Err(CantEndInANumber)?,
(x , "" | ".") => self.host.replace_range(x.len() - 1 .. , ""),
(x , _ ) => self.host.replace_range(x.len() ..= x.len() + replace.len(), ""),
}
}
self.details = DomainHostDetails::parse_unchecked(&self.host);
Ok(true)
}
pub fn set_origin_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
let temp1 = self.origin_str().unwrap_or(self.suffix_str());
let insert_start = temp1.addr () - self.host.addr();
let insert_after = temp1.end_addr() - self.host.addr();
match (temp1.split('.').try_neg_nth(index), value.map(TryInto::try_into).transpose()?) {
(Ok(old), Some(new)) if old == new => return Ok(false),
(Err(1..), Some(_)) => Err(InsertNotFound )?,
(Err(_ ), None ) => return Ok(false),
(Ok(old), Some(new)) => match self.host.split_around_substr(old) {
("", "" ) if new.is_empty () => Err(CantBeEmpty)?,
(_ , "" ) if new.is_empty () => Err(NonFqdnCantEndInEmpty)?,
(_ , "" | ".") if new.is_a_number() => Err(CantEndInANumber)?,
_ => self.host.replace_substr(old, new.as_str()),
},
(Ok(old), None) => match self.host.split_around_substr(old) {
("", "" | ".") => Err(CantBeEmpty)?,
(x , "" ) if ends_in_empty (x) => Err(NonFqdnCantEndInEmpty)?,
(x , "" ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
(x , "." ) if last_is_a_number(x) => Err(CantEndInANumber)?,
(x , "" | ".") => self.host.replace_range(x.len() - 1 .. , ""),
(x , _ ) => self.host.replace_range(x.len() ..= x.len() + old.len(), ""),
},
(Err(0), Some(new)) => match index {
0.. if new.is_a_number() => Err(CantEndInANumber)?,
0.. => self.host.insert_with(insert_after, [".", new.as_str()]),
..0 => self.host.insert_with(insert_start, [new.as_str(), "."]),
},
}
self.details = DomainHostDetails::parse_unchecked(&self.host);
Ok(true)
}
pub fn set_origin_range<'b, T: TryInto<DomainSegments<'b>>, B: RangeBounds<isize>>(&mut self, range: B, value: Option<T>) -> Result<bool, SetDomainError> where SetDomainError: From<T::Error> {
let old = self.origin_range(range).ok_or(RangeNotFound)?;
let new = value.map(TryInto::try_into).transpose()?;
match new {
Some(new) if old == new => return Ok(false),
Some(new) => match self.host.split_around_substr(old.as_str()) {
("", "" ) if new.is_empty() => Err(CantBeEmpty)?,
(_ , "" ) if new.last_is_empty() => Err(NonFqdnCantEndInEmpty)?,
(_ , "" ) if new.ends_in_a_number() => Err(CantEndInANumber)?,
(_ , ".") if new.last_is_a_number() => Err(CantEndInANumber)?,
_ => self.host.replace_substr(old.as_str(), new.as_str())
},
None => match self.host.split_around_substr(old.as_str()) {
("", "" | ".") => Err(CantBeEmpty)?,
(x , "" ) if ends_in_empty (x) => Err(NonFqdnCantEndInEmpty)?,
(x , "" ) if ends_in_a_number(x) => Err(CantEndInANumber)?,
(x , "." ) if last_is_a_number(x) => Err(CantEndInANumber)?,
(x , "" | ".") => self.host.replace_range(x.len() - 1 .. , ""),
(x , _ ) => self.host.replace_range(x.len() ..= x.len() + old.len(), ""),
}
}
self.details = DomainHostDetails::parse_unchecked(&self.host);
Ok(true)
}
pub fn insert_origin_segment<'b, T: TryInto<DomainSegment<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetDomainError> where SetDomainError: From<T::Error> {
let new = value.try_into()?;
let temp1 = self.origin_str().unwrap_or(self.suffix_str());
let insert_start = temp1.addr () - self.host.addr();
let insert_after = temp1.end_addr() - self.host.addr();
let temp2 = temp1.split('.').map(|x| self.host.my_substr_range(x)).try_neg_nth(index);
match (temp2, index) {
(Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.is_empty () => Err(NonFqdnCantEndInEmpty)?,
(Ok(_), -1) | (Err(0), 0..) if new.is_a_number() => Err(CantEndInANumber)?,
(Err(1..), _) => Err(InsertNotFound)?,
(Ok(Range {start, ..}), 0..) => self.host.insert_with(start, [new.as_str(), "."]),
(Ok(Range {end , ..}), ..0) => self.host.insert_with(end , [".", new.as_str()]),
(Err(0), 0..) => self.host.insert_with(insert_after, [".", new.as_str()]),
(Err(0), ..0) => self.host.insert_with(insert_start, [new.as_str(), "."]),
}
self.details = DomainHostDetails::parse_unchecked(&self.host);
Ok(())
}
pub fn insert_origin_segments<'b, T: TryInto<DomainSegments<'b>>>(&mut self, index: isize, value: T) -> Result<(), SetDomainError> where SetDomainError: From<T::Error> {
let new = value.try_into()?;
let temp1 = self.origin_str().unwrap_or(self.suffix_str());
let insert_start = temp1.addr () - self.host.addr();
let insert_after = temp1.end_addr() - self.host.addr();
let temp2 = temp1.split('.').map(|x| self.host.my_substr_range(x)).try_neg_nth(index);
match (temp2, index) {
(Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.last_is_empty () => Err(NonFqdnCantEndInEmpty)?,
(Ok(_), -1) | (Err(0), 0..) if !self.is_fqdn() && new.ends_in_a_number() => Err(CantEndInANumber)?,
(Ok(_), -1) | (Err(0), 0..) if self.is_fqdn() && new.last_is_a_number() => Err(CantEndInANumber)?,
(Err(1..), _) => Err(InsertNotFound)?,
(Ok(Range {start, ..}), 0..) => self.host.insert_with(start, [new.as_str(), "."]),
(Ok(Range {end , ..}), ..0) => self.host.insert_with(end , [".", new.as_str()]),
(Err(0), 0..) => self.host.insert_with(insert_after, [".", new.as_str()]),
(Err(0), ..0) => self.host.insert_with(insert_start, [new.as_str(), "."]),
}
self.details = DomainHostDetails::parse_unchecked(&self.host);
Ok(())
}
}