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
use std::sync::Arc;
use std::default::Default;
use snafu::{Snafu, ensure};
use edgedb_errors::{Error, ErrorKind, ProtocolEncodingError};
use crate::codec::{Codec, build_codec};
use crate::errors::{self, DecodeError};
use crate::descriptors::{Descriptor, TypePos};
#[non_exhaustive]
pub struct Decoder {
pub has_implicit_tid: bool,
pub has_implicit_tname: bool,
}
impl Default for Decoder {
fn default() -> Decoder {
Decoder {
has_implicit_tid: false,
has_implicit_tname: false,
}
}
}
pub trait Queryable: Sized {
fn decode(decoder: &Decoder, buf: &[u8])
-> Result<Self, DecodeError>;
fn decode_optional(decoder: &Decoder, buf: Option<&[u8]>)
-> Result<Self, DecodeError>
{
ensure!(buf.is_some(), errors::MissingRequiredElement);
Self::decode(decoder, buf.unwrap())
}
fn check_descriptor(ctx: &DescriptorContext, type_pos: TypePos)
-> Result<(), DescriptorMismatch>;
}
#[derive(Snafu, Debug)]
#[non_exhaustive]
pub enum DescriptorMismatch {
#[snafu(display("unexpected type {}, expected {}", unexpected, expected))]
WrongType { unexpected: String, expected: String },
#[snafu(display("unexpected field {}, expected {}", unexpected, expected))]
WrongField { unexpected: String, expected: String },
#[snafu(display("expected {} fields, got {}", expected, unexpected))]
FieldNumber { unexpected: usize, expected: usize },
#[snafu(display("expected {}", expected))]
Expected { expected: String },
#[snafu(display("invalid type descriptor"))]
InvalidDescriptor,
}
pub struct DescriptorContext<'a> {
pub has_implicit_tid: bool,
pub has_implicit_tname: bool,
descriptors: &'a [Descriptor],
}
impl DescriptorContext<'_> {
pub(crate) fn new(descriptors: &[Descriptor]) -> DescriptorContext {
DescriptorContext {
descriptors,
has_implicit_tid: false,
has_implicit_tname: false,
}
}
pub fn build_codec(&self, root_pos: TypePos)
-> Result<Arc<dyn Codec>, Error>
{
build_codec(Some(root_pos), self.descriptors)
.map_err(ProtocolEncodingError::with_source)
}
pub fn get(&self, type_pos: TypePos)
-> Result<&Descriptor, DescriptorMismatch>
{
self.descriptors.get(type_pos.0 as usize)
.ok_or(DescriptorMismatch::InvalidDescriptor)
}
pub fn wrong_type(&self, descriptor: &Descriptor, expected: &str)
-> DescriptorMismatch
{
DescriptorMismatch::WrongType {
unexpected: format!("{:?}", descriptor),
expected: expected.into(),
}
}
pub fn field_number(&self, expected: usize, unexpected: usize)
-> DescriptorMismatch
{
DescriptorMismatch::FieldNumber { expected, unexpected }
}
pub fn wrong_field(&self, expected: &str, unexpected: &str)
-> DescriptorMismatch
{
DescriptorMismatch::WrongField {
expected: expected.into(),
unexpected: unexpected.into(),
}
}
pub fn expected(&self, expected: &str)
-> DescriptorMismatch
{
DescriptorMismatch::Expected { expected: expected.into() }
}
}