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