1use crate::errors;
6use crate::model::Uuid;
7use bytes::Bytes;
8
9use crate::descriptors::Typedesc;
10use crate::encoding::Input;
11use crate::errors::DecodeError;
12use crate::features::ProtocolVersion;
13
14pub use crate::client_message::{InputLanguage, IoFormat};
15
16#[derive(Debug, Copy, Clone, PartialEq, Eq)]
17pub enum Cardinality {
18 NoResult = 0x6e,
19 AtMostOne = 0x6f,
20 One = 0x41,
21 Many = 0x6d,
22 AtLeastOne = 0x4d,
23}
24
25bitflags::bitflags! {
26 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
27 pub struct Capabilities: u64 {
28 const MODIFICATIONS = 0b00000001;
29 const SESSION_CONFIG = 0b00000010;
30 const TRANSACTION = 0b00000100;
31 const DDL = 0b00001000;
32 const PERSISTENT_CONFIG = 0b00010000;
33 const ALL = 0b00011111;
34 }
35}
36
37bitflags::bitflags! {
38 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
39 pub struct CompilationFlags: u64 {
40 const INJECT_OUTPUT_TYPE_IDS = 0b00000001;
41 const INJECT_OUTPUT_TYPE_NAMES = 0b00000010;
42 const INJECT_OUTPUT_OBJECT_IDS = 0b00000100;
43 }
44}
45
46bitflags::bitflags! {
47 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
48 pub struct DumpFlags: u64 {
49 const DUMP_SECRETS = 0b00000001;
50 }
51}
52
53#[derive(Debug, Clone)]
54pub struct CompilationOptions {
55 pub implicit_limit: Option<u64>,
56 pub implicit_typenames: bool,
57 pub implicit_typeids: bool,
58 pub allow_capabilities: Capabilities,
59 pub explicit_objectids: bool,
60 pub io_format: IoFormat,
61 pub expected_cardinality: Cardinality,
62 pub input_language: InputLanguage,
63}
64
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct State {
67 pub typedesc_id: Uuid,
68 pub data: Bytes,
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct RawTypedesc {
73 pub proto: ProtocolVersion,
74 pub id: Uuid,
75 pub data: Bytes,
76}
77
78impl RawTypedesc {
79 pub fn uninitialized() -> RawTypedesc {
80 RawTypedesc {
81 proto: ProtocolVersion::current(),
82 id: Uuid::from_u128(0),
83 data: Bytes::new(),
84 }
85 }
86 pub fn decode(&self) -> Result<Typedesc, DecodeError> {
87 let cur = &mut Input::new(self.proto.clone(), self.data.clone());
88 Typedesc::decode_with_id(self.id, cur)
89 }
90}
91
92impl std::convert::TryFrom<u8> for Cardinality {
93 type Error = errors::DecodeError;
94 fn try_from(cardinality: u8) -> Result<Cardinality, errors::DecodeError> {
95 match cardinality {
96 0x6e => Ok(Cardinality::NoResult),
97 0x6f => Ok(Cardinality::AtMostOne),
98 0x41 => Ok(Cardinality::One),
99 0x6d => Ok(Cardinality::Many),
100 0x4d => Ok(Cardinality::AtLeastOne),
101 _ => Err(errors::InvalidCardinality { cardinality }.build()),
102 }
103 }
104}
105
106impl Cardinality {
107 pub fn is_optional(&self) -> bool {
108 use Cardinality::*;
109 match self {
110 NoResult => true,
111 AtMostOne => true,
112 One => false,
113 Many => true,
114 AtLeastOne => false,
115 }
116 }
117}
118
119impl std::convert::TryFrom<u8> for InputLanguage {
120 type Error = errors::DecodeError;
121 fn try_from(input_language: u8) -> Result<Self, errors::DecodeError> {
122 match input_language {
123 0x45 => Ok(InputLanguage::EdgeQL),
124 0x53 => Ok(InputLanguage::SQL),
125 _ => Err(errors::InvalidInputLanguage { input_language }.build()),
126 }
127 }
128}
129
130impl State {
131 pub fn empty() -> State {
132 State {
133 typedesc_id: Uuid::from_u128(0),
134 data: Bytes::new(),
135 }
136 }
137 pub fn descriptor_id(&self) -> Uuid {
138 self.typedesc_id
139 }
140}
141
142impl CompilationOptions {
143 pub fn flags(&self) -> CompilationFlags {
144 let mut cflags = CompilationFlags::empty();
145 if self.implicit_typenames {
146 cflags |= CompilationFlags::INJECT_OUTPUT_TYPE_NAMES;
147 }
148 if self.implicit_typeids {
149 cflags |= CompilationFlags::INJECT_OUTPUT_TYPE_IDS;
150 }
151 cflags
153 }
154}