feat: add serde support

This commit is contained in:
Jindřich Moravec 2024-01-27 19:02:11 +01:00
parent f398faa6aa
commit 5d925290e3
8 changed files with 46 additions and 34 deletions

View file

@ -6,4 +6,5 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bimap = "0.6.3"
bimap = { version = "0.6.3", features = ["serde"] }
serde = { version = "1.0.196", features = ["derive"] }

View file

@ -1,7 +1,8 @@
use crate::type_system::{IndexableValue, Uuid};
use std::collections::{BTreeMap, HashSet};
use serde::{Deserialize, Serialize};
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct ColumnIndex {
index: BTreeMap<IndexableValue, HashSet<Uuid>>,
}

View file

@ -1,10 +1,11 @@
use crate::type_system::Value;
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;
use serde::{Deserialize, Serialize};
pub type ColumnPosition = usize;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Row(Vec<Value>);
impl<Idx> Index<Idx> for Row

View file

@ -1,4 +1,5 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use serde::{Deserialize, Serialize};
use crate::error::Error;
use crate::internals::column_index::ColumnIndex;
@ -7,7 +8,7 @@ use crate::schema::{ColumnName, TableSchema, TableName};
use crate::result::DbResult;
use crate::type_system::{IndexableValue, Uuid, Value};
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Table {
schema: TableSchema,
rows: Rows, // TODO: Consider wrapping this in a lock. Also consider if we need to have the

View file

@ -6,13 +6,14 @@ use crate::operation::{ColumnSelection, Condition, Operation};
use crate::result::DbResult;
use crate::type_system::{DbType, IndexableValue, Value};
use bimap::BiMap;
use serde::{Deserialize, Serialize};
// Use `TablePosition` as index
pub type Tables = Vec<Table>;
pub type TablePosition = usize;
// ==============Interpreter================
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct State {
table_name_position_mapping: BiMap<TableName, TablePosition>,
tables: Tables,

View file

@ -5,10 +5,11 @@ use crate::result::DbResult;
use crate::type_system::{DbType, IndexableValue, Uuid, Value};
use bimap::BiMap;
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
// Note that it is nice to split metadata from the data because
// then you can give the metadata to the parser without giving it the data.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize)]
pub struct TableSchema {
table_name: TableName, // used for descriptive errors
primary_key: ColumnPosition,

View file

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};
// ==============Types================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DbType {
String,
Int,
@ -13,14 +15,14 @@ pub type Uuid = u64;
// TODO: What about nulls? I would rather not have that in SQL, it sucks.
// I would rather have non-nullable values by default,
// and something like an explicit Option type for nulls.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Value {
Number(f64), // TODO: Can't put floats as keys in maps, since they don't implement Eq. What to
// do?
Indexable(IndexableValue),
}
#[derive(Debug, Ord, Eq, Clone, PartialOrd, PartialEq)]
#[derive(Debug, Ord, Eq, Clone, PartialOrd, PartialEq, Serialize, Deserialize)]
pub enum IndexableValue {
String(String),
Int(u64),