sophia_term 0.6.1

A Rust toolkit for RDF and Linked Data - Implementations of RDF terms
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Public exported by parent-module `literal`.
//!

use super::*;
use crate::{term_to_string, Result, TermError};
use sophia_api::ns::xsd;
use std::error::Error;
use std::fmt;
use std::hash;
use std::str::FromStr;

/// A native datatype that maps to an RDF datatype.
pub trait DataType {
    /// Static IRI referencing the datatype of this Rust-type.
    fn iri() -> SimpleIri<'static>;

    /// Datatype of `self`.
    ///
    /// This can be used for `enum`s where each variant may has its own
    /// datatype.
    fn datatype(&self) -> SimpleIri<'static> {
        Self::iri()
    }
}

/// A native datatype that converts to an RDF literal.
pub trait AsLiteral: DataType {
    /// The term type to which this native type can convert.
    type Term: TTerm;
    /// Create an RDF literal, representing `self`.
    fn as_literal(&self) -> Self::Term;
}

/// A native datatype that can be constructed from an RDF literal.
pub trait TryConvertTerm: DataType + FromStr
where
    <Self as FromStr>::Err: Error + 'static,
{
    /// Try to convert any term to this native type.
    fn try_convert<T>(term: &T) -> Result<Self>
    where
        T: TTerm + ?Sized,
    {
        match term.datatype() {
            None => Err(TermError::UnsupportedKind(term_to_string(term))),
            Some(dt) => {
                if Self::iri() != dt {
                    Err(TermError::UnsupportedDatatype(term_to_string(term)))
                } else {
                    term.value_raw().0.parse::<Self>().map_err(|err| {
                        TermError::InvalidLexicalValue {
                            lex: term.value_raw().0.to_string(),
                            dt: term_to_string(&dt),
                            source: Box::new(err),
                        }
                    })
                }
            }
        }
    }
}

/// This trait is to [`TryConvertTerm`]
/// what `Into` is to `From`.
/// It is automatically implemented by any implementation of [`TTerm`].
///
/// [`TryConvertTerm`]: ./trait.TryConvertTerm.html
/// [`TTerm`]: ./trait.TTerm.html
pub trait ConvertibleTerm: TTerm {
    /// Try to convert this term into a native type
    fn try_converted<T>(&self) -> Result<T>
    where
        T: TryConvertTerm,
        <T as FromStr>::Err: Error + 'static;
}

impl<T> ConvertibleTerm for T
where
    T: TTerm + ?Sized,
{
    fn try_converted<U>(&self) -> Result<U>
    where
        U: TryConvertTerm,
        <U as FromStr>::Err: Error + 'static,
    {
        U::try_convert(self)
    }
}

/// An RDF literal representation of a native value.
#[derive(Clone, Debug)]
pub struct NativeLiteral<T, U = Box<str>>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
{
    /// Lexical value
    pub(crate) lexval: U,
    /// Phantom data to link to the original datatype
    _phantom: std::marker::PhantomData<T>,
}

impl<T, U> NativeLiteral<T, U>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
{
    /// Construct a SimpleLiteral.
    ///
    /// # Pre-conditions
    /// This constructor does not check that `lexval`
    /// is a valid lexical value for this literal's datatype.
    /// It is the user's responsibility to ensure that.
    fn new(lexval: U) -> Self {
        Self {
            lexval,
            _phantom: std::marker::PhantomData,
        }
    }
}

impl<T, U> TTerm for NativeLiteral<T, U>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
{
    fn kind(&self) -> TermKind {
        TermKind::Literal
    }

    fn value_raw(&self) -> RawValue {
        self.lexval.as_ref().into()
    }

    fn datatype(&self) -> Option<SimpleIri> {
        Some(T::iri())
    }

    fn as_dyn(&self) -> &dyn TTerm {
        self
    }
}

impl<T, U> fmt::Display for NativeLiteral<T, U>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        term_format(self, f)
    }
}

impl<T, U, V> PartialEq<V> for NativeLiteral<T, U>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
    V: TTerm + ?Sized,
{
    fn eq(&self, other: &V) -> bool {
        term_eq(self, other)
    }
}

impl<T, U> hash::Hash for NativeLiteral<T, U>
where
    T: DataType + ?Sized,
    U: AsRef<str>,
{
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        term_hash(self, state)
    }
}

macro_rules! impl_datatype {
    ($ty:ty, $iri:expr) => {
        impl $crate::literal::convert::DataType for $ty {
            fn iri() -> sophia_api::term::SimpleIri<'static> {
                $iri
            }
        }
    };
}

impl_datatype!(u8, xsd::unsignedByte);
impl_datatype!(u16, xsd::unsignedShort);
impl_datatype!(u32, xsd::unsignedInt);
impl_datatype!(u64, xsd::unsignedLong);
impl_datatype!(i8, xsd::byte);
impl_datatype!(i16, xsd::short);
impl_datatype!(i32, xsd::int);
impl_datatype!(i64, xsd::long);
impl_datatype!(f32, xsd::float);
impl_datatype!(f64, xsd::double);
impl_datatype!(bool, xsd::boolean);
impl_datatype!(String, xsd::string);
impl_datatype!(str, xsd::string);
impl_datatype!(&str, xsd::string);

macro_rules! impl_as_literal {
    ($ty:ty) => {
        impl $crate::literal::convert::AsLiteral for $ty
        where
            Self: std::string::ToString,
        {
            type Term = $crate::literal::convert::NativeLiteral<Self>;
            fn as_literal(&self) -> Self::Term {
                $crate::literal::convert::NativeLiteral::new(self.to_string().into_boxed_str())
            }
        }
    };
}

impl_as_literal!(u8);
impl_as_literal!(u16);
impl_as_literal!(u32);
impl_as_literal!(u64);
impl_as_literal!(i8);
impl_as_literal!(i16);
impl_as_literal!(i32);
impl_as_literal!(i64);
impl_as_literal!(f32);
impl_as_literal!(f64);
impl_as_literal!(bool);
impl_as_literal!(String);

impl<'a> AsLiteral for &'a str {
    type Term = NativeLiteral<&'a str, &'a str>;
    fn as_literal(&self) -> Self::Term {
        NativeLiteral::new(*self)
    }
}

impl TryConvertTerm for u8 {}
impl TryConvertTerm for u16 {}
impl TryConvertTerm for u32 {}
impl TryConvertTerm for u64 {}
impl TryConvertTerm for i8 {}
impl TryConvertTerm for i16 {}
impl TryConvertTerm for i32 {}
impl TryConvertTerm for i64 {}
impl TryConvertTerm for f32 {}
impl TryConvertTerm for f64 {}
impl TryConvertTerm for bool {}
impl TryConvertTerm for String {}

#[cfg(test)]
mod test {
    use super::*;
    use test_case::test_case;

    #[derive(Debug, Clone, PartialEq, Eq)]
    enum Failed {
        InvLexVal,
        NotALit,
        WrongDt,
        Unknown,
    }

    impl From<TermError> for Failed {
        fn from(other: TermError) -> Self {
            match other {
                TermError::InvalidLexicalValue { .. } => Failed::InvLexVal,
                TermError::UnsupportedKind(_) => Failed::NotALit,
                TermError::UnsupportedDatatype(_) => Failed::WrongDt,
                _ => Failed::Unknown,
            }
        }
    }

    #[test]
    fn borrow_str() {
        let _: NativeLiteral<&str, &str> = "test".as_literal();
        let string = "test2".to_string();
        let l1 = string.as_str().as_literal();
        let l2 = string.as_literal();
        assert_eq!(l1, l2);
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Ok(-10)                ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Err(Failed::InvLexVal) ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_i8(lex: &str) -> Result<i8, Failed> {
        let dt = Iri::<&str>::from(i8::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: i8 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Ok(-10)                ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Err(Failed::InvLexVal) ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_i16(lex: &str) -> Result<i16, Failed> {
        let dt = Iri::<&str>::from(i16::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: i16 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Ok(-10)                ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Ok(1000000)            ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_i32(lex: &str) -> Result<i32, Failed> {
        let dt = Iri::<&str>::from(i32::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: i32 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Ok(-10)                ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Ok(1000000)            ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_i64(lex: &str) -> Result<i64, Failed> {
        let dt = Iri::<&str>::from(i64::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: i64 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Err(Failed::InvLexVal) ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Err(Failed::InvLexVal) ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_u8(lex: &str) -> Result<u8, Failed> {
        let dt = Iri::<&str>::from(u8::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: u8 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Err(Failed::InvLexVal) ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Err(Failed::InvLexVal) ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_u16(lex: &str) -> Result<u16, Failed> {
        let dt = Iri::<&str>::from(u16::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: u16 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Err(Failed::InvLexVal) ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Ok(1000000)            ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_u32(lex: &str) -> Result<u32, Failed> {
        let dt = Iri::<&str>::from(u32::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: u32 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0)                  ; "zero")]
    #[test_case("-10"       => Err(Failed::InvLexVal) ; "minus ten")]
    #[test_case("10"        => Ok(10)                 ; "ten")]
    #[test_case("1000000"   => Ok(1000000)            ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_u64(lex: &str) -> Result<u64, Failed> {
        let dt = Iri::<&str>::from(u64::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: u64 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0.0)                ; "zero")]
    #[test_case("-10"       => Ok(-10.0)              ; "minus ten")]
    #[test_case("10"        => Ok(10.0)               ; "ten")]
    #[test_case("1000000"   => Ok(1000000.0)          ; "million")]
    #[test_case("314e-2"    => Ok(3.14)               ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_f32(lex: &str) -> Result<f32, Failed> {
        let dt = Iri::<&str>::from(f32::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: f32 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok(0.0)                ; "zero")]
    #[test_case("-10"       => Ok(-10.0)              ; "minus ten")]
    #[test_case("10"        => Ok(10.0)               ; "ten")]
    #[test_case("1000000"   => Ok(1000000.0)          ; "million")]
    #[test_case("314e-2"    => Ok(3.14)               ; "float")]
    #[test_case("true"      => Err(Failed::InvLexVal) ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_f64(lex: &str) -> Result<f64, Failed> {
        let dt = Iri::<&str>::from(f64::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: f64 = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Err(Failed::InvLexVal) ; "zero")]
    #[test_case("-10"       => Err(Failed::InvLexVal) ; "minus ten")]
    #[test_case("10"        => Err(Failed::InvLexVal) ; "ten")]
    #[test_case("1000000"   => Err(Failed::InvLexVal) ; "million")]
    #[test_case("314e-2"    => Err(Failed::InvLexVal) ; "float")]
    #[test_case("true"      => Ok(true)               ; "bool")]
    #[test_case("something" => Err(Failed::InvLexVal) ; "string")]
    fn convert_bool(lex: &str) -> Result<bool, Failed> {
        let dt = Iri::<&str>::from(bool::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: bool = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }

    #[test_case("0"         => Ok("0"        .to_string()) ; "zero")]
    #[test_case("-10"       => Ok("-10"      .to_string()) ; "minus ten")]
    #[test_case("10"        => Ok("10"       .to_string()) ; "ten")]
    #[test_case("1000000"   => Ok("1000000"  .to_string()) ; "million")]
    #[test_case("314e-2"    => Ok("314e-2"   .to_string()) ; "float")]
    #[test_case("true"      => Ok("true"     .to_string()) ; "bool")]
    #[test_case("something" => Ok("something".to_string()) ; "string")]
    fn convert_string(lex: &str) -> Result<String, Failed> {
        let dt = Iri::<&str>::from(String::iri());
        let lit = Literal::<&str>::new_dt(lex, dt);
        let native: String = lit.try_converted()?;
        let lit2 = native.as_literal();
        assert_eq!(lit.datatype(), lit2.datatype());
        Ok(native)
    }
}