mod account;
mod flow;
mod journal;
mod network;
mod patterns;
mod temporal;
pub use account::*;
pub use flow::*;
pub use journal::*;
pub use network::*;
pub use patterns::*;
pub use temporal::*;
use rkyv::{Archive, Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Archive, Serialize, Deserialize)]
#[archive(compare(PartialEq))]
#[repr(C)]
pub struct Decimal128 {
pub mantissa: i128,
pub scale: u8,
}
impl Decimal128 {
pub const ZERO: Self = Self {
mantissa: 0,
scale: 2,
};
pub fn new(mantissa: i128, scale: u8) -> Self {
Self { mantissa, scale }
}
pub fn zero() -> Self {
Self::ZERO
}
pub fn from_cents(cents: i64) -> Self {
Self {
mantissa: cents as i128,
scale: 2,
}
}
pub fn from_f64(value: f64) -> Self {
Self {
mantissa: (value * 100.0).round() as i128,
scale: 2,
}
}
pub fn to_f64(&self) -> f64 {
self.mantissa as f64 / 10f64.powi(self.scale as i32)
}
pub fn abs(&self) -> Self {
Self {
mantissa: self.mantissa.abs(),
scale: self.scale,
}
}
pub fn is_zero(&self) -> bool {
self.mantissa == 0
}
pub fn is_positive(&self) -> bool {
self.mantissa > 0
}
pub fn is_negative(&self) -> bool {
self.mantissa < 0
}
}
impl std::ops::Add for Decimal128 {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self {
mantissa: self.mantissa + rhs.mantissa,
scale: self.scale,
}
}
}
impl std::ops::Sub for Decimal128 {
type Output = Self;
fn sub(self, rhs: Self) -> Self {
Self {
mantissa: self.mantissa - rhs.mantissa,
scale: self.scale,
}
}
}
impl std::ops::Neg for Decimal128 {
type Output = Self;
fn neg(self) -> Self {
Self {
mantissa: -self.mantissa,
scale: self.scale,
}
}
}
impl Default for Decimal128 {
fn default() -> Self {
Self::ZERO
}
}
impl std::fmt::Display for Decimal128 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let value = self.to_f64();
write!(f, "${:.2}", value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Archive, Serialize, Deserialize)]
#[archive(compare(PartialEq))]
#[repr(C)]
pub struct HybridTimestamp {
pub physical: u64,
pub logical: u32,
pub node_id: u32,
}
impl HybridTimestamp {
pub fn now() -> Self {
use std::time::{SystemTime, UNIX_EPOCH};
let physical = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock before UNIX epoch")
.as_millis() as u64;
Self {
physical,
logical: 0,
node_id: 0,
}
}
pub fn with_node_id(node_id: u32) -> Self {
use std::time::{SystemTime, UNIX_EPOCH};
let physical = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock before UNIX epoch")
.as_millis() as u64;
Self {
physical,
logical: 0,
node_id,
}
}
pub fn new(physical: u64, logical: u32) -> Self {
Self {
physical,
logical,
node_id: 0,
}
}
pub fn zero() -> Self {
Self {
physical: 0,
logical: 0,
node_id: 0,
}
}
}
impl Default for HybridTimestamp {
fn default() -> Self {
Self::zero()
}
}