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
use crate::{
	context::InvalidContext, Container, ContainerKind, Direction, LenientLangTagBuf, Nullable,
};
use iref::IriRefBuf;

pub trait TryFromJson: Sized {
	type Error;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, Self::Error>;
}

impl crate::TryFromJson for bool {
	type Error = crate::Unexpected;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, Self::Error> {
		match value {
			json_syntax::Value::Boolean(b) => Ok(b),
			unexpected => Err(crate::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::Boolean],
			)),
		}
	}
}

impl TryFromJson for IriRefBuf {
	type Error = InvalidContext;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::String(s) => match IriRefBuf::new(s.into_string()) {
				Ok(iri_ref) => Ok(iri_ref),
				Err(e) => Err(InvalidContext::InvalidIriRef(e.0)),
			},
			unexpected => Err(InvalidContext::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::String],
			)),
		}
	}
}

impl TryFromJson for LenientLangTagBuf {
	type Error = InvalidContext;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::String(s) => {
				let (lang, _) = LenientLangTagBuf::new(s.into_string());
				Ok(lang)
			}
			unexpected => Err(InvalidContext::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::String],
			)),
		}
	}
}

impl TryFromJson for Direction {
	type Error = InvalidContext;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::String(s) => match Direction::try_from(s.as_str()) {
				Ok(d) => Ok(d),
				Err(_) => Err(InvalidContext::InvalidDirection),
			},
			unexpected => Err(InvalidContext::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::String],
			)),
		}
	}
}

impl<T: TryFromJson> TryFromJson for Nullable<T> {
	type Error = T::Error;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, Self::Error> {
		match value {
			json_syntax::Value::Null => Ok(Self::Null),
			some => T::try_from_json(some).map(Self::Some),
		}
	}
}

impl TryFromJson for Container {
	type Error = InvalidContext;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::Array(a) => {
				let mut container = Vec::new();

				for item in a {
					container.push(ContainerKind::try_from_json(item)?)
				}

				Ok(Self::Many(container))
			}
			other => ContainerKind::try_from_json(other).map(Into::into),
		}
	}
}

impl TryFromJson for ContainerKind {
	type Error = InvalidContext;

	fn try_from_json(value: json_syntax::Value) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::String(s) => match ContainerKind::try_from(s.as_str()) {
				Ok(t) => Ok(t),
				Err(_) => Err(InvalidContext::InvalidTermDefinition),
			},
			unexpected => Err(InvalidContext::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::String],
			)),
		}
	}
}