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

pub trait TryFromJson<M>: Sized {
	type Error;

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

pub trait TryFromStrippedJson<M>: Sized {
	fn try_from_stripped_json(value: json_syntax::Value<M>) -> Result<Self, InvalidContext>;
}

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

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

impl<M> TryFromStrippedJson<M> for IriRefBuf {
	fn try_from_stripped_json(value: json_syntax::Value<M>) -> Result<Self, InvalidContext> {
		match value {
			json_syntax::Value::String(s) => match IriRefBuf::from_string(s.into_string()) {
				Ok(iri_ref) => Ok(iri_ref),
				Err((e, _)) => Err(InvalidContext::InvalidIriRef(e)),
			},
			unexpected => Err(InvalidContext::Unexpected(
				unexpected.kind(),
				&[json_syntax::Kind::String],
			)),
		}
	}
}

impl<M> TryFromJson<M> for IriRefBuf {
	type Error = InvalidContext;

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

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

impl<M> TryFromStrippedJson<M> for Direction {
	fn try_from_stripped_json(value: json_syntax::Value<M>) -> 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<M, T: TryFromStrippedJson<M>> TryFromJson<M> for Nullable<T> {
	type Error = InvalidContext;

	fn try_from_json(
		Meta(value, meta): Meta<json_syntax::Value<M>, M>,
	) -> Result<Meta<Self, M>, Meta<InvalidContext, M>> {
		match value {
			json_syntax::Value::Null => Ok(Meta(Self::Null, meta)),
			some => match T::try_from_stripped_json(some) {
				Ok(some) => Ok(Meta(Self::Some(some), meta)),
				Err(e) => Err(Meta(e, meta)),
			},
		}
	}
}

impl<M> TryFromJson<M> for Container<M> {
	type Error = InvalidContext;

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

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

				Ok(Meta(Self::Many(container), meta))
			}
			other => ContainerKind::try_from_json(other).map(Meta::cast),
		}
	}
}

impl<M> TryFromJson<M> for ContainerKind {
	type Error = InvalidContext;

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