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
use crate::value::{Type, TypeError, Value};
use std::convert::{AsRef, TryFrom};
use thiserror::Error;
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Array {
pub(crate) type_: Type,
pub(crate) array: Vec<Value>,
}
#[derive(Debug, PartialEq, Error)]
pub enum ArrayError {
#[error("The type of an array element is different: expected '{0}' got '{1}'")]
TypeMismatch(Type, Type),
#[error("Coult not get type of element: {0}")]
TypeError(#[from] TypeError),
}
impl Array {
pub fn new(array: Vec<Value>, type_: Type) -> Result<Array, ArrayError> {
for v in &array {
let s = v.get_type()?;
if s != type_ {
return Err(ArrayError::TypeMismatch(type_, s));
}
}
let array = Array { type_, array };
Ok(array)
}
#[inline]
pub const fn get_type(&self) -> &Type {
&self.type_
}
}
impl AsRef<[Value]> for Array {
fn as_ref(&self) -> &[Value] {
self.array.as_ref()
}
}
impl From<Array> for Vec<Value> {
fn from(array: Array) -> Self {
array.array
}
}
#[derive(Debug, Clone, PartialOrd, PartialEq)]
pub struct Struct(pub(crate) Vec<Value>);
#[derive(Debug, PartialEq, Error)]
pub enum StructError {
#[error("Strcut cannot be empty")]
Empty,
}
impl AsRef<[Value]> for Struct {
fn as_ref(&self) -> &[Value] {
self.0.as_ref()
}
}
impl TryFrom<Vec<Value>> for Struct {
type Error = StructError;
fn try_from(values: Vec<Value>) -> Result<Self, Self::Error> {
if values.is_empty() {
Err(StructError::Empty)
} else {
Ok(Struct(values))
}
}
}
impl From<Struct> for Vec<Value> {
fn from(struct_: Struct) -> Self {
struct_.0
}
}