rust-query 0.9.1

A query builder using rust concepts.
Documentation
use std::{borrow::Cow, collections::BTreeSet, convert::Infallible, str::FromStr};

use crate::schema::check_constraint::Parsed;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u64)]
pub enum ColumnType {
    Integer = 0,
    Real = 1,
    Text = 2,
    Blob = 3,
    Unknown(String),
}

impl std::hash::Hash for ColumnType {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        if let ColumnType::Unknown(_) = self {
            unreachable!()
        }
        core::mem::discriminant(self).hash(state);
    }
}

impl FromStr for ColumnType {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "INTEGER" | "INT" => ColumnType::Integer,
            "TEXT" => ColumnType::Text,
            "REAL" => ColumnType::Real,
            "BLOB" => ColumnType::Blob,
            _ => ColumnType::Unknown(s.to_owned()),
        })
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Column {
    pub typ: ColumnType,
    pub nullable: bool,
    pub fk: Option<(String, String)>,
    pub check: Option<Parsed>,
}

impl std::hash::Hash for Column {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.typ.hash(state);
        self.nullable.hash(state);
        self.fk.hash(state);
        // for backwards compatibility
        if let Some(check) = &self.check {
            Some(check.to_string()).hash(state);
        }
    }
}

// TODO: remove redundant unique constraints
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Unique {
    pub columns: BTreeSet<Cow<'static, str>>,
}

impl std::hash::Hash for Unique {
    fn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {
        self.columns.hash(hasher);
        true.hash(hasher); // for backwards compatibility
    }
}

#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
#[cfg(feature = "dev")]
pub struct Table {
    pub columns: std::collections::BTreeMap<String, Column>,
    pub indices: BTreeSet<Unique>,
}

#[derive(Debug, Hash, Default, PartialEq, Eq)]
#[cfg(feature = "dev")]
pub struct Schema {
    pub tables: std::collections::BTreeMap<String, Table>,
}