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
use iref::IriRefBuf;
use langtag::LanguageTagBuf;
use std::borrow::{Borrow, BorrowMut};
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

/// RDF Literal.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Literal {
	/// Untyped string literal.
	String(StringLiteral),

	/// Typed string literal.
	TypedString(StringLiteral, IriRefBuf),

	/// Language string.
	LangString(StringLiteral, LanguageTagBuf),
}

/// String literal, without type or language tag.
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct StringLiteral(String);

impl StringLiteral {
	pub fn new() -> Self {
		Self::default()
	}
}

impl PartialEq<String> for StringLiteral {
	fn eq(&self, other: &String) -> bool {
		self.as_str().eq(other.as_str())
	}
}

impl PartialEq<str> for StringLiteral {
	fn eq(&self, other: &str) -> bool {
		self.as_str().eq(other)
	}
}

impl PartialEq<StringLiteral> for String {
	fn eq(&self, other: &StringLiteral) -> bool {
		self.as_str().eq(other.as_str())
	}
}

impl PartialEq<StringLiteral> for str {
	fn eq(&self, other: &StringLiteral) -> bool {
		self.eq(other.as_str())
	}
}

impl From<String> for StringLiteral {
	fn from(s: String) -> Self {
		Self(s)
	}
}

impl From<StringLiteral> for String {
	fn from(s: StringLiteral) -> Self {
		s.0
	}
}

impl FromStr for StringLiteral {
	type Err = std::convert::Infallible;

	fn from_str(s: &str) -> Result<Self, std::convert::Infallible> {
		Ok(Self(s.to_owned()))
	}
}

impl Deref for StringLiteral {
	type Target = String;

	fn deref(&self) -> &String {
		&self.0
	}
}

impl DerefMut for StringLiteral {
	fn deref_mut(&mut self) -> &mut String {
		&mut self.0
	}
}

impl Borrow<str> for StringLiteral {
	fn borrow(&self) -> &str {
		self.0.as_str()
	}
}

impl BorrowMut<str> for StringLiteral {
	fn borrow_mut(&mut self) -> &mut str {
		self.0.as_mut_str()
	}
}

impl AsRef<str> for StringLiteral {
	fn as_ref(&self) -> &str {
		self.0.as_str()
	}
}

impl AsMut<str> for StringLiteral {
	fn as_mut(&mut self) -> &mut str {
		self.0.as_mut_str()
	}
}

impl fmt::Display for StringLiteral {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "\"")?;

		for c in self.0.chars() {
			match c {
				'"' => write!(f, "\\u0022"),
				'\\' => write!(f, "\\u005c"),
				'\n' => write!(f, "\\n"),
				'\r' => write!(f, "\\r"),
				'\t' => write!(f, "\\t"),
				'\u{08}' => write!(f, "\\b"),
				'\u{0c}' => write!(f, "\\f"),
				c => c.fmt(f),
			}?
		}

		write!(f, "\"")
	}
}

impl fmt::Display for Literal {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::String(s) => s.fmt(f),
			Self::TypedString(s, ty) => write!(f, "{}^^<{}>", s, ty),
			Self::LangString(s, tag) => write!(f, "{}@{}", s, tag),
		}
	}
}