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
use std::convert::{TryFrom, TryInto};
use std::fmt;
use json::JsonValue;
use iref::Iri;
use crate::{
syntax::TermLike,
util
};
#[derive(PartialEq, Eq, Clone, Hash)]
pub enum Lenient<T> {
Ok(T),
Unknown(String)
}
impl<T> Lenient<T> {
pub fn cast<U>(self) -> Lenient<U> where U: From<T> {
match self {
Lenient::Ok(t) => Lenient::Ok(t.into()),
Lenient::Unknown(t) => Lenient::Unknown(t)
}
}
pub fn try_cast<U>(self) -> Result<Lenient<U>, U::Error> where U: TryFrom<T> {
match self {
Lenient::Ok(t) => Ok(Lenient::Ok(t.try_into()?)),
Lenient::Unknown(t) => Ok(Lenient::Unknown(t))
}
}
}
impl<T: PartialEq> PartialEq<T> for Lenient<T> {
fn eq(&self, other: &T) -> bool {
match self {
Lenient::Ok(t) => t == other,
_ => false
}
}
}
impl<T: TermLike> Lenient<T> {
pub fn as_iri(&self) -> Option<Iri> {
match self {
Lenient::Ok(term) => term.as_iri(),
Lenient::Unknown(_) => None
}
}
pub fn as_str(&self) -> &str {
match self {
Lenient::Ok(term) => term.as_str(),
Lenient::Unknown(unknown) => unknown.as_str()
}
}
}
impl<T> From<T> for Lenient<T> {
fn from(t: T) -> Lenient<T> {
Lenient::Ok(t)
}
}
impl<T: fmt::Display> fmt::Display for Lenient<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Lenient::Ok(t) => t.fmt(f),
Lenient::Unknown(u) => u.fmt(f)
}
}
}
impl<T: util::AsJson> util::AsJson for Lenient<T> {
fn as_json(&self) -> JsonValue {
match self {
Lenient::Ok(t) => t.as_json(),
Lenient::Unknown(u) => u.as_json()
}
}
}