1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
//! This module holds the high level model representation
//!
//! It adds:
//! - [`db_type`]: a type level version of [`imr::DbType`] to be used in generic type bound checks
//! - [`annotations`]: a type level version of [`imr::Annotation`] to be used in generic type bound checks
//!
//! These features are split into different submodules to avoid name conflicts.
//!
//! [`imr::DbType`]: crate::imr::DbType
//! [`imr::Annotation`]: crate::imr::Annotation
use crate::imr;
/// A type level version of [`imr::DbType`] to be used in generic type bound checks
///
/// [`imr::DbType`]: crate::imr::DbType
pub mod db_type {
use crate::imr;
/// Trait to associate the type-level db types with their runtime db types
pub trait DbType: 'static {
/// Equivalent runtime db type
const IMR: imr::DbType;
}
macro_rules! impl_db_types {
($(#[doc = $doc:literal] $type:ident,)*) => {
$(
#[doc = $doc]
pub struct $type;
impl DbType for $type {
const IMR: imr::DbType = imr::DbType::$type;
}
)*
};
}
impl_db_types!(
/// Type level version of [`imr::DbType::VarChar`]
VarChar,
/// Type level version of [`imr::DbType::VarBinary`]
VarBinary,
/// Type level version of [`imr::DbType::Int8`]
Int8,
/// Type level version of [`imr::DbType::Int16`]
Int16,
/// Type level version of [`imr::DbType::Int32`]
Int32,
/// Type level version of [`imr::DbType::Int64`]
Int64,
/// Type level version of [`imr::DbType::Float`]
Float,
/// Type level version of [`imr::DbType::Double`]
Double,
/// Type level version of [`imr::DbType::Boolean`]
Boolean,
/// Type level version of [`imr::DbType::Date`]
Date,
/// Type level version of [`imr::DbType::DateTime`]
DateTime,
/// Type level version of [`imr::DbType::Timestamp`]
Timestamp,
/// Type level version of [`imr::DbType::Time`]
Time,
/// Type level version of [`imr::DbType::Choices`]
Choices,
);
}
/// A type level version of [`imr::Annotation`] to be used in generic type bound checks
///
/// [`imr::Annotation`]: crate::imr::Annotation
pub mod annotations {
use crate::imr;
macro_rules! impl_annotations {
($($(#[doc = $doc:literal])* $anno:ident $(($data:ty))?,)*) => {
$(
$(#[doc = $doc])*
pub struct $anno$((
/// The annotation's data
pub $data
))?;
impl AsImr for $anno {
type Imr = imr::Annotation;
fn as_imr(&self) -> imr::Annotation {
imr::Annotation::$anno$(({
let data: &$data = &self.0;
data.as_imr()
}))?
}
}
)*
};
}
impl_annotations!(
/// Will set the current time of the database when a row is created.
AutoCreateTime,
/// Will set the current time of the database when a row is updated.
AutoUpdateTime,
/// AUTO_INCREMENT constraint
AutoIncrement,
/// A list of choices to set
Choices(&'static [&'static str]),
/// DEFAULT constraint
DefaultValue(DefaultValueData),
/// Create an index. The optional [IndexData] can be used, to build more complex indexes.
Index(Option<IndexData>),
/// Only for VARCHAR. Specifies the maximum length of the column's content.
MaxLength(i32),
/// The annotated column will be used as primary key
PrimaryKey,
/// UNIQUE constraint
Unique,
);
/// Action to take on a foreign key in case of on delete
pub type OnDelete = imr::ReferentialAction;
/// Action take on a foreign key in case of an update
pub type OnUpdate = imr::ReferentialAction;
/// Represents a complex index
pub struct IndexData {
/// Name of the index. Can be used multiple times in a model to create an
/// index with multiple columns.
pub name: &'static str,
/// The order to put the columns in while generating an index.
/// Only useful if multiple columns with the same name are present.
pub priority: Option<i32>,
}
/// A column's default value which is any non object / array json value
pub enum DefaultValueData {
/// Use hexadecimal to represent binary data
String(&'static str),
/// i64 is used as it can represent any integer defined in DbType
Integer(i64),
/// Ordered float is used as f64 does not Eq and Order which are needed for Hash
Float(f64),
/// Just a bool. Nothing interesting here.
Boolean(bool),
}
/// Trait for converting a hmr type into a imr one
pub trait AsImr {
/// Imr type to convert to
type Imr;
/// Convert to imr type
fn as_imr(&self) -> Self::Imr;
}
/// [`Index`]'s data
impl AsImr for Option<IndexData> {
type Imr = Option<imr::IndexValue>;
fn as_imr(&self) -> Self::Imr {
self.as_ref().map(|data| imr::IndexValue {
name: data.name.to_string(),
priority: data.priority,
})
}
}
/// [`DefaultValue`]'s data
impl AsImr for DefaultValueData {
type Imr = imr::DefaultValue;
fn as_imr(&self) -> Self::Imr {
match self {
DefaultValueData::String(string) => imr::DefaultValue::String(string.to_string()),
DefaultValueData::Integer(integer) => imr::DefaultValue::Integer(*integer),
DefaultValueData::Float(float) => imr::DefaultValue::Float((*float).into()),
DefaultValueData::Boolean(boolean) => imr::DefaultValue::Boolean(*boolean),
}
}
}
/// [`MaxLength`]'s data
impl AsImr for i32 {
type Imr = i32;
fn as_imr(&self) -> Self::Imr {
*self
}
}
/// [`Choices`]' data
impl AsImr for &'static [&'static str] {
type Imr = Vec<String>;
fn as_imr(&self) -> Self::Imr {
self.iter().map(ToString::to_string).collect()
}
}
}
/// Location in the source code a model or field originates from
/// Used for better error messages in the migration tool
#[derive(Copy, Clone)]
pub struct Source {
/// Filename of the source code of the model or field
pub file: &'static str,
/// Line of the model or field
pub line: usize,
/// Column of the model or field
pub column: usize,
}
impl From<Source> for imr::Source {
fn from(source: Source) -> Self {
imr::Source {
file: source.file.to_string(),
line: source.line,
column: source.column,
}
}
}