1use crate::db::raw_def::IndexType;
2use crate::relation::{FieldName, Header};
3use crate::{buffer, AlgebraicType, AlgebraicValue};
4use derive_more::Display;
5use spacetimedb_primitives::{ColId, ColList, TableId};
6use spacetimedb_sats::product_value::InvalidFieldError;
7use spacetimedb_sats::satn::Satn as _;
8use std::fmt;
9use std::string::FromUtf8Error;
10use thiserror::Error;
11
12#[derive(Error, Debug)]
13pub enum TypeError {
14 #[error("The type of `{{value.to_satns()}}` cannot be inferred")]
15 CannotInferType { value: AlgebraicValue },
16}
17
18#[derive(Error, Debug, Clone)]
19pub enum DecodeError {
20 #[error("Decode UTF8: {0}")]
21 Utf8(#[from] FromUtf8Error),
22 #[error("AlgebraicType::decode: Unknown: {0}")]
23 AlgebraicTypeUnknown(u8),
24 #[error("AlgebraicType::decode: Byte array has invalid length: {0:?}")]
25 AlgebraicType(usize),
26 #[error("SumType::decode: Byte array has invalid length: {0:?}")]
27 SumType(usize),
28 #[error("ProductType::decode: Byte array has invalid length: {0:?}")]
29 ProductType(usize),
30 #[error("ProductTypeElement::decode: Byte array has invalid length: {0:?}")]
31 ProductTypeElement(usize),
32 #[error("AlgebraicValue::decode: byte array length not long enough to decode {0:?}")]
33 AlgebraicValue(AlgebraicType),
34 #[error("AlgebraicValue::decode: byte array length not long enough to get length of {0:?}")]
35 AlgebraicValueGetLength(AlgebraicType),
36 #[error(
37 "AlgebraicValue::decode: buffer has no room to decode any more elements from this {kind:?}. (len: {len} <= read:{read})"
38 )]
39 AlgebraicValueRoom {
40 kind: AlgebraicType,
41 len: usize,
42 read: usize,
43 },
44 #[error("AlgebraicValue::decode: Cannot decode {kind:?}, buffer not long enough. (len: {len}, read:{read})")]
45 TypeBufferSmall {
46 kind: AlgebraicType,
47 len: usize,
48 read: usize,
49 },
50 #[error(
51 "AlgebraicValue::decode: byte array length not long enough to decode {kind:?}. (expect: {expect}, read:{read})"
52 )]
53 TypeTooSmall {
54 kind: AlgebraicType,
55 expect: usize,
56 read: usize,
57 },
58 #[error("EnumValue::decode: Byte array length is invalid.")]
59 EnumValue,
60}
61
62#[derive(Error, Debug, Clone)]
63pub enum LibError {
64 #[error("DecodeError: {0}")]
65 Decode(#[from] DecodeError),
66 #[error("BufferError: {0}")]
67 Buffer(#[from] buffer::DecodeError),
68 #[error(transparent)]
69 TupleFieldInvalid(#[from] InvalidFieldError),
70}
71
72#[derive(thiserror::Error, Debug)]
73pub enum AuthError {
74 #[error("Table `{named}` is private")]
75 TablePrivate { named: String },
76 #[error("Index `{named}` is private")]
77 IndexPrivate { named: String },
78 #[error("Sequence `{named}` is private")]
79 SequencePrivate { named: String },
80 #[error("Only the database owner can perform the requested operation")]
81 OwnerRequired,
82 #[error("Constraint `{named}` is private")]
83 ConstraintPrivate { named: String },
84}
85
86#[derive(thiserror::Error, Debug)]
87pub enum RelationError {
88 #[error("Field `{1}` not found. Must be one of {0}")]
89 FieldNotFound(Header, FieldName),
90 #[error("Field `{0}` fail to infer the type: {1}")]
91 TypeInference(FieldName, TypeError),
92 #[error("Field with value `{}` was not a `bool`", val.to_satn())]
93 NotBoolValue { val: AlgebraicValue },
94 #[error("Field `{field}` was expected to be `bool` but is `{}`", ty.to_satn())]
95 NotBoolType { field: FieldName, ty: AlgebraicType },
96 #[error("Field declaration only support `table.field` or `field`. It gets instead `{0}`")]
97 FieldPathInvalid(String),
98}
99
100#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Display)]
101pub enum DefType {
102 Column,
103 Index,
104 Sequence,
105 Constraint,
106}
107
108#[derive(thiserror::Error, Debug, PartialEq)]
109pub enum SchemaError {
110 #[error("Multiple primary columns defined for table: {table} columns: {pks:?}")]
111 MultiplePrimaryKeys { table: Box<str>, pks: Vec<String> },
112 #[error("table id `{table_id}` should have name")]
113 EmptyTableName { table_id: TableId },
114 #[error("{ty} {name} columns `{columns:?}` not found in table `{table}`")]
115 ColumnsNotFound {
116 name: Box<str>,
117 table: Box<str>,
118 columns: Vec<ColId>,
119 ty: DefType,
120 },
121 #[error("table `{table}` {ty} should have name. {ty} id: {id}")]
122 EmptyName { table: Box<str>, ty: DefType, id: u32 },
123 #[error("table `{table}` have `Constraints::unset()` for columns: {columns:?}")]
124 ConstraintUnset {
125 table: Box<str>,
126 name: Box<str>,
127 columns: ColList,
128 },
129 #[error("Attempt to define a column with more than 1 auto_inc sequence: Table: `{table}`, Field: `{field}`")]
130 OneAutoInc { table: Box<str>, field: Box<str> },
131 #[error("Only Btree Indexes are supported: Table: `{table}`, Index: `{index}` is a `{index_type}`")]
132 OnlyBtree {
133 table: Box<str>,
134 index: Box<str>,
135 index_type: IndexType,
136 },
137}
138
139#[derive(thiserror::Error, Debug, PartialEq)]
140pub struct SchemaErrors(pub Vec<SchemaError>);
141
142impl fmt::Display for SchemaErrors {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 f.debug_list().entries(self.0.iter()).finish()
145 }
146}