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
// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.


/// NOTE: At some future point, Atom may become a wrapper around a string cache value
#[derive(Default, Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Atom(String);

impl Deref for Atom
{
	type Target = str;
	
	fn deref(&self) -> &Self::Target
	{
		&self.0
	}
}

impl ToCss for Atom
{
	#[inline(always)]
	fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result
	{
		serialize_identifier(&self.0, dest)
	}
}

impl Display for Atom
{
	#[inline(always)]
	fn fmt(&self, f: &mut Formatter) -> fmt::Result
	{
		self.0.fmt(f)
	}
}

impl From<String> for Atom
{
	#[inline(always)]
	fn from(value: String) -> Self
	{
		Atom(value)
	}
}

impl<'a> From<&'a str> for Atom
{
	#[inline(always)]
	fn from(value: &'a str) -> Self
	{
		Atom(value.to_owned())
	}
}

impl<'i> From<CowRcStr<'i>> for Atom
{
	#[inline(always)]
	fn from(value: CowRcStr<'i>) -> Self
	{
		Atom::from(value.as_ref())
	}
}

impl<'a> From<Cow<'a, str>> for Atom
{
	#[inline(always)]
	fn from(value: Cow<'a, str>) -> Self
	{
		Atom(value.into_owned())
	}
}

impl<'a, 'i> From<&'a CowRcStr<'i>> for Atom
{
	#[inline(always)]
	fn from(value: &'a CowRcStr<'i>) -> Self
	{
		Atom::from(value.as_ref())
	}
}

impl FromStr for Atom
{
	type Err = ();
	
	fn from_str(s: &str) -> Result<Self, Self::Err>
	{
		Ok(Atom(s.to_owned()))
	}
}

impl PrecomputedHash for Atom
{
	#[inline(always)]
	fn precomputed_hash(&self) -> u32
	{
		let mut state = DefaultHasher::new();
		self.0.hash(&mut state);
		state.finish() as u32
	}
}

impl Atom
{
	#[inline(always)]
	pub fn is_ascii(&self) -> bool
	{
		self.0.is_ascii()
	}
	
	#[inline(always)]
	pub fn eq_ignore_ascii_case(&self, name: &str) -> bool
	{
		self.deref().eq_ignore_ascii_case(name)
	}
}