ringkernel_accnet/models/
mod.rs1mod account;
7mod flow;
8mod journal;
9mod network;
10mod patterns;
11mod temporal;
12
13pub use account::*;
14pub use flow::*;
15pub use journal::*;
16pub use network::*;
17pub use patterns::*;
18pub use temporal::*;
19
20use rkyv::{Archive, Deserialize, Serialize};
21
22#[derive(Debug, Clone, Copy, PartialEq, Archive, Serialize, Deserialize)]
25#[archive(compare(PartialEq))]
26#[repr(C)]
27pub struct Decimal128 {
28 pub mantissa: i128,
30 pub scale: u8,
32}
33
34impl Decimal128 {
35 pub const ZERO: Self = Self {
37 mantissa: 0,
38 scale: 2,
39 };
40
41 pub fn new(mantissa: i128, scale: u8) -> Self {
43 Self { mantissa, scale }
44 }
45
46 pub fn zero() -> Self {
48 Self::ZERO
49 }
50
51 pub fn from_cents(cents: i64) -> Self {
53 Self {
54 mantissa: cents as i128,
55 scale: 2,
56 }
57 }
58
59 pub fn from_f64(value: f64) -> Self {
61 Self {
62 mantissa: (value * 100.0).round() as i128,
63 scale: 2,
64 }
65 }
66
67 pub fn to_f64(&self) -> f64 {
69 self.mantissa as f64 / 10f64.powi(self.scale as i32)
70 }
71
72 pub fn abs(&self) -> Self {
74 Self {
75 mantissa: self.mantissa.abs(),
76 scale: self.scale,
77 }
78 }
79
80 pub fn is_zero(&self) -> bool {
82 self.mantissa == 0
83 }
84
85 pub fn is_positive(&self) -> bool {
87 self.mantissa > 0
88 }
89
90 pub fn is_negative(&self) -> bool {
92 self.mantissa < 0
93 }
94}
95
96impl std::ops::Add for Decimal128 {
97 type Output = Self;
98 fn add(self, rhs: Self) -> Self {
99 Self {
101 mantissa: self.mantissa + rhs.mantissa,
102 scale: self.scale,
103 }
104 }
105}
106
107impl std::ops::Sub for Decimal128 {
108 type Output = Self;
109 fn sub(self, rhs: Self) -> Self {
110 Self {
111 mantissa: self.mantissa - rhs.mantissa,
112 scale: self.scale,
113 }
114 }
115}
116
117impl std::ops::Neg for Decimal128 {
118 type Output = Self;
119 fn neg(self) -> Self {
120 Self {
121 mantissa: -self.mantissa,
122 scale: self.scale,
123 }
124 }
125}
126
127impl Default for Decimal128 {
128 fn default() -> Self {
129 Self::ZERO
130 }
131}
132
133impl std::fmt::Display for Decimal128 {
134 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135 let value = self.to_f64();
136 write!(f, "${:.2}", value)
137 }
138}
139
140#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Archive, Serialize, Deserialize)]
143#[archive(compare(PartialEq))]
144#[repr(C)]
145pub struct HybridTimestamp {
146 pub physical: u64,
148 pub logical: u32,
150 pub node_id: u32,
152}
153
154impl HybridTimestamp {
155 pub fn now() -> Self {
157 use std::time::{SystemTime, UNIX_EPOCH};
158 let physical = SystemTime::now()
159 .duration_since(UNIX_EPOCH)
160 .unwrap()
161 .as_millis() as u64;
162 Self {
163 physical,
164 logical: 0,
165 node_id: 0,
166 }
167 }
168
169 pub fn with_node_id(node_id: u32) -> Self {
171 use std::time::{SystemTime, UNIX_EPOCH};
172 let physical = SystemTime::now()
173 .duration_since(UNIX_EPOCH)
174 .unwrap()
175 .as_millis() as u64;
176 Self {
177 physical,
178 logical: 0,
179 node_id,
180 }
181 }
182
183 pub fn new(physical: u64, logical: u32) -> Self {
185 Self {
186 physical,
187 logical,
188 node_id: 0,
189 }
190 }
191
192 pub fn zero() -> Self {
194 Self {
195 physical: 0,
196 logical: 0,
197 node_id: 0,
198 }
199 }
200}
201
202impl Default for HybridTimestamp {
203 fn default() -> Self {
204 Self::zero()
205 }
206}