use crate::DomainRef;
#[must_use]
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Domain {
name: String,
}
impl Domain {
pub fn localhost() -> Self {
DomainRef::LOCALHOST.to_domain()
}
pub fn example() -> Self {
DomainRef::EXAMPLE.to_domain()
}
}
impl Domain {
pub unsafe fn new_unchecked<S>(name: S) -> Self
where
S: Into<String>,
{
let name: String = name.into();
debug_assert!(Self::is_valid_name_str(name.as_str(), false));
Self { name }
}
}
impl From<Domain> for String {
fn from(domain: Domain) -> Self {
domain.name
}
}
impl From<Domain> for Vec<u8> {
fn from(domain: Domain) -> Self {
domain.name.into_bytes()
}
}
impl<'a> PartialEq<DomainRef<'a>> for Domain {
fn eq(&self, other: &DomainRef<'a>) -> bool {
self.to_ref() == *other
}
}
impl Domain {
#[must_use]
pub fn name(&self) -> &str {
self.name.as_str()
}
}
#[cfg(test)]
mod tests {
use crate::{Domain, DomainRef};
#[test]
fn specials() {
assert_eq!(Domain::localhost().name, "localhost");
assert_eq!(Domain::example().name, "example.com");
}
#[test]
fn deconstruction() {
let domain: Domain = Domain::localhost();
let result: String = domain.into();
let expected: &str = "localhost";
assert_eq!(result, expected);
let domain: Domain = Domain::localhost();
let result: Vec<u8> = domain.into();
let expected: &[u8] = b"localhost";
assert_eq!(result, expected);
}
#[test]
fn equality() {
let domain: Domain = Domain::localhost();
assert_eq!(domain, DomainRef::LOCALHOST);
assert_ne!(domain, DomainRef::EXAMPLE);
}
#[test]
fn properties() {
let domain: Domain = Domain::localhost();
assert_eq!(domain.name(), "localhost");
}
}