1use crate::{AsValue, ColumnDef, Entity, TableRef};
2use rust_decimal::Decimal;
3use std::{marker::PhantomData, mem};
4
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub struct FixedDecimal<const WIDTH: u8, const SCALE: u8>(pub Decimal);
7
8impl<const W: u8, const S: u8> From<Decimal> for FixedDecimal<W, S> {
9 fn from(value: Decimal) -> Self {
10 Self(value)
11 }
12}
13
14impl<const W: u8, const S: u8> From<FixedDecimal<W, S>> for Decimal {
15 fn from(value: FixedDecimal<W, S>) -> Self {
16 value.0
17 }
18}
19
20#[derive(Debug, Default)]
21pub enum Passive<T: AsValue> {
22 Set(T),
23 #[default]
24 NotSet,
25}
26
27impl<T: AsValue> Passive<T> {
28 pub fn expect(self, msg: &str) -> T {
29 match self {
30 Passive::Set(v) => v,
31 Passive::NotSet => panic!("{}", msg),
32 }
33 }
34 pub fn unwrap(self) -> T {
35 match self {
36 Passive::Set(v) => v,
37 Passive::NotSet => panic!("called `Passive::unwrap()` on a `NotSet` value"),
38 }
39 }
40}
41
42impl<T: AsValue + PartialEq> PartialEq for Passive<T> {
43 fn eq(&self, other: &Self) -> bool {
44 match (self, other) {
45 (Self::Set(lhs), Self::Set(rhs)) => lhs == rhs,
46 _ => mem::discriminant(self) == mem::discriminant(other),
47 }
48 }
49}
50
51impl<T: AsValue + Clone> Clone for Passive<T>
52where
53 T: Clone,
54{
55 fn clone(&self) -> Self {
56 match self {
57 Self::Set(v) => Self::Set(v.clone()),
58 Self::NotSet => Self::NotSet,
59 }
60 }
61}
62
63impl<T: AsValue> From<T> for Passive<T> {
64 fn from(value: T) -> Self {
65 Self::Set(value)
66 }
67}
68
69pub struct References<T: Entity> {
70 entity: PhantomData<T>,
71 columns: Box<[ColumnDef]>,
72}
73
74impl<T: Entity> References<T> {
75 pub fn new(columns: Box<[ColumnDef]>) -> Self {
76 Self {
77 columns,
78 entity: Default::default(),
79 }
80 }
81 pub fn table_ref(&self) -> TableRef {
82 T::table_ref().clone()
83 }
84 pub fn columns(&self) -> &[ColumnDef] {
85 &self.columns
86 }
87}