mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-06-13 00:13:31 -07:00
server: smart generic vec routes build
This commit is contained in:
@@ -6,25 +6,25 @@ use jiff::{civil::Date as Date_, tz::TimeZone, Span};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromBytes, Immutable, IntoBytes, KnownLayout)]
|
||||
pub struct Date {
|
||||
year: u16,
|
||||
month: u8,
|
||||
day: u8,
|
||||
}
|
||||
pub struct Date(u32);
|
||||
|
||||
impl Date {
|
||||
const INDEX_ZERO: Self = Self {
|
||||
year: 2009,
|
||||
month: 1,
|
||||
day: 3,
|
||||
};
|
||||
const INDEX_ZERO: Self = Self(20090103);
|
||||
const INDEX_ZERO_: Date_ = Date_::constant(2009, 1, 3);
|
||||
const INDEX_ONE: Self = Self {
|
||||
year: 2009,
|
||||
month: 1,
|
||||
day: 9,
|
||||
};
|
||||
const INDEX_ONE: Self = Self(20090109);
|
||||
const INDEX_ONE_: Date_ = Date_::constant(2009, 1, 9);
|
||||
|
||||
pub fn year(&self) -> u16 {
|
||||
(self.0 / 1_00_00) as u16
|
||||
}
|
||||
|
||||
pub fn month(&self) -> u8 {
|
||||
((self.0 % 1_00_00) / 1_00) as u8
|
||||
}
|
||||
|
||||
pub fn day(&self) -> u8 {
|
||||
(self.0 % 1_00) as u8
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Date {
|
||||
@@ -33,31 +33,15 @@ impl Default for Date {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Date {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for Date {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
Date_::from(*self).cmp(&Date_::from(*other))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date_> for Date {
|
||||
fn from(value: Date_) -> Self {
|
||||
Self {
|
||||
year: value.year() as u16,
|
||||
month: value.month() as u8,
|
||||
day: value.day() as u8,
|
||||
}
|
||||
Self(value.year() as u32 * 1_00_00 + value.month() as u32 * 1_00 + value.day() as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Date_ {
|
||||
fn from(value: Date) -> Self {
|
||||
Self::new(value.year as i16, value.month as i8, value.day as i8).unwrap()
|
||||
Self::new(value.year() as i16, value.month() as i8, value.day() as i8).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,33 +51,33 @@ impl From<Timestamp> for Date {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Date> for usize {
|
||||
type Error = color_eyre::Report;
|
||||
fn try_from(value: Date) -> Result<Self, Self::Error> {
|
||||
let value_ = Date_::from(value);
|
||||
if value_ < Date::INDEX_ZERO_ {
|
||||
Err(eyre!("Date is too early"))
|
||||
} else if value == Date::INDEX_ZERO {
|
||||
Ok(0)
|
||||
} else if value_ < Date::INDEX_ONE_ {
|
||||
Err(eyre!("Date is between first and second"))
|
||||
} else if value == Date::INDEX_ONE {
|
||||
Ok(1)
|
||||
} else {
|
||||
Ok(Date_::from(Date::INDEX_ONE).until(value_)?.get_days() as usize + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl TryFrom<Date> for usize {
|
||||
// type Error = color_eyre::Report;
|
||||
// fn try_from(value: Date) -> Result<Self, Self::Error> {
|
||||
// let value_ = Date_::from(value);
|
||||
// if value_ < Date::INDEX_ZERO_ {
|
||||
// Err(eyre!("Date is too early"))
|
||||
// } else if value == Date::INDEX_ZERO {
|
||||
// Ok(0)
|
||||
// } else if value_ < Date::INDEX_ONE_ {
|
||||
// Err(eyre!("Date is between first and second"))
|
||||
// } else if value == Date::INDEX_ONE {
|
||||
// Ok(1)
|
||||
// } else {
|
||||
// Ok(Date_::from(Date::INDEX_ONE).until(value_)?.get_days() as usize + 1)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl From<usize> for Date {
|
||||
fn from(value: usize) -> Self {
|
||||
Self::from(Self::INDEX_ZERO_.checked_add(Span::new().days(value as i64)).unwrap())
|
||||
}
|
||||
}
|
||||
// impl From<usize> for Date {
|
||||
// fn from(value: usize) -> Self {
|
||||
// Self::from(Self::INDEX_ZERO_.checked_add(Span::new().days(value as i64)).unwrap())
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Add<usize> for Date {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: usize) -> Self::Output {
|
||||
Self::from(Date_::from(self).checked_add(Span::new().days(rhs as i64)).unwrap())
|
||||
}
|
||||
}
|
||||
// impl Add<usize> for Date {
|
||||
// type Output = Self;
|
||||
// fn add(self, rhs: usize) -> Self::Output {
|
||||
// Self::from(Date_::from(self).checked_add(Span::new().days(rhs as i64)).unwrap())
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
use std::ops::Add;
|
||||
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromBytes, Immutable, IntoBytes, KnownLayout)]
|
||||
pub struct Dateindex(u16);
|
||||
|
||||
impl From<Dateindex> for usize {
|
||||
fn from(value: Dateindex) -> Self {
|
||||
value.0 as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl From<usize> for Dateindex {
|
||||
fn from(value: usize) -> Self {
|
||||
Self(value as u16)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add<usize> for Dateindex {
|
||||
type Output = Self;
|
||||
fn add(self, rhs: usize) -> Self::Output {
|
||||
Self(self.0 + rhs as u16)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
mod addressindextxoutindex;
|
||||
mod date;
|
||||
mod dateindex;
|
||||
mod feerate;
|
||||
mod unit;
|
||||
|
||||
pub use addressindextxoutindex::*;
|
||||
pub use date::*;
|
||||
pub use dateindex::*;
|
||||
pub use feerate::*;
|
||||
pub use unit::*;
|
||||
|
||||
Reference in New Issue
Block a user