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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use crate::{IriVocabularyMut, RdfDisplay};
use iref::IriBuf;
use langtag::LanguageTagBuf;
use std::borrow::{Borrow, BorrowMut};
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;
#[cfg(feature = "contextual")]
use contextual::DisplayWithContext;
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub enum Literal<S = StringLiteral, I = IriBuf, L = LanguageTagBuf> {
String(S),
TypedString(S, I),
LangString(S, L),
}
impl<S, I, L> Literal<S, I, L> {
pub fn is_typed(&self) -> bool {
matches!(self, Self::TypedString(_, _))
}
pub fn ty(&self) -> Option<&I> {
match self {
Self::TypedString(_, ty) => Some(ty),
_ => None,
}
}
pub fn is_lang_string(&self) -> bool {
matches!(self, Self::LangString(_, _))
}
pub fn lang_tag(&self) -> Option<&L> {
match self {
Self::LangString(_, tag) => Some(tag),
_ => None,
}
}
pub fn string_literal(&self) -> &S {
match self {
Self::String(s) => s,
Self::TypedString(s, _) => s,
Self::LangString(s, _) => s,
}
}
pub fn string_literal_mut(&mut self) -> &mut S {
match self {
Self::String(s) => s,
Self::TypedString(s, _) => s,
Self::LangString(s, _) => s,
}
}
pub fn into_string_literal(self) -> S {
match self {
Self::String(s) => s,
Self::TypedString(s, _) => s,
Self::LangString(s, _) => s,
}
}
}
impl<S, L> Literal<S, IriBuf, L> {
pub fn inserted_into<V: IriVocabularyMut>(&self, vocabulary: &mut V) -> Literal<S, V::Iri, L>
where
S: Clone,
L: Clone,
{
match self {
Self::String(s) => Literal::String(s.clone()),
Self::TypedString(s, t) => {
Literal::TypedString(s.clone(), vocabulary.insert(t.as_iri()))
}
Self::LangString(s, l) => Literal::LangString(s.clone(), l.clone()),
}
}
pub fn insert_into<V: IriVocabularyMut>(self, vocabulary: &mut V) -> Literal<S, V::Iri, L> {
match self {
Self::String(s) => Literal::String(s),
Self::TypedString(s, t) => Literal::TypedString(s, vocabulary.insert(t.as_iri())),
Self::LangString(s, l) => Literal::LangString(s, l),
}
}
}
impl<S: Borrow<str>, I, L> Borrow<str> for Literal<S, I, L> {
fn borrow(&self) -> &str {
self.string_literal().borrow()
}
}
impl<S: BorrowMut<str>, I, L> BorrowMut<str> for Literal<S, I, L> {
fn borrow_mut(&mut self) -> &mut str {
self.string_literal_mut().borrow_mut()
}
}
impl<S: AsRef<str>, I, L> AsRef<str> for Literal<S, I, L> {
fn as_ref(&self) -> &str {
self.string_literal().as_ref()
}
}
impl<S: AsMut<str>, I, L> AsMut<str> for Literal<S, I, L> {
fn as_mut(&mut self) -> &mut str {
self.string_literal_mut().as_mut()
}
}
#[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, "\\\""),
'\\' => write!(f, "\\\\"),
'\n' => write!(f, "\\n"),
'\r' => write!(f, "\\r"),
c => c.fmt(f),
}?
}
write!(f, "\"")
}
}
impl<S: fmt::Display, I: RdfDisplay, L: fmt::Display> fmt::Display for Literal<S, I, L> {
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.rdf_display()),
Self::LangString(s, tag) => write!(f, "{s}@{tag}"),
}
}
}
impl<S: fmt::Display, I: RdfDisplay, L: fmt::Display> RdfDisplay for Literal<S, I, L> {
fn rdf_fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::String(s) => s.fmt(f),
Self::TypedString(s, ty) => write!(f, "{s}^^{}", ty.rdf_display()),
Self::LangString(s, tag) => write!(f, "{s}@{tag}"),
}
}
}
#[cfg(feature = "contextual")]
impl<S: fmt::Display, I, L: fmt::Display, V: crate::IriVocabulary<Iri = I>> DisplayWithContext<V>
for Literal<S, I, L>
{
fn fmt_with(&self, vocabulary: &V, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::String(s) => s.fmt(f),
Self::TypedString(s, ty) => write!(f, "{s}^^<{}>", vocabulary.iri(ty).unwrap()),
Self::LangString(s, tag) => write!(f, "{s}@{tag}"),
}
}
}
#[cfg(feature = "contextual")]
impl<S: fmt::Display, I, L: fmt::Display, V: crate::IriVocabulary<Iri = I>>
crate::RdfDisplayWithContext<V> for Literal<S, I, L>
{
fn rdf_fmt_with(&self, vocabulary: &V, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::String(s) => s.fmt(f),
Self::TypedString(s, ty) => write!(f, "{s}^^<{}>", vocabulary.iri(ty).unwrap()),
Self::LangString(s, tag) => write!(f, "{s}@{tag}"),
}
}
}