mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-07-01 22:39:03 -07:00
computer: part 2
This commit is contained in:
@@ -4,7 +4,7 @@ use serde::Serialize;
|
||||
// use color_eyre::eyre::eyre;
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::Error;
|
||||
use crate::{CheckedSub, Error};
|
||||
|
||||
use super::Date;
|
||||
|
||||
@@ -71,3 +71,9 @@ impl TryFrom<Date> for Dateindex {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Dateindex {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::{Date, Dateindex, Yearindex};
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,36 @@ impl Add<usize> for Decadeindex {
|
||||
Self::from(self.0 + rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Dateindex> for Decadeindex {
|
||||
fn from(value: Dateindex) -> Self {
|
||||
Self::from(Date::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Decadeindex {
|
||||
fn from(value: Date) -> Self {
|
||||
let year = value.year();
|
||||
if year < 2000 {
|
||||
panic!("unsupported")
|
||||
}
|
||||
Self(((year - 2000) / 10) as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Decadeindex {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Yearindex> for Decadeindex {
|
||||
fn from(value: Yearindex) -> Self {
|
||||
let v = usize::from(value);
|
||||
if v == 0 {
|
||||
Self(0)
|
||||
} else {
|
||||
Self((((v - 1) / 10) + 1) as u8)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::Height;
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,15 @@ impl Add<usize> for Difficultyepoch {
|
||||
Self::from(self.0 + rhs as u16)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Height> for Difficultyepoch {
|
||||
fn from(value: Height) -> Self {
|
||||
Self((u32::from(value) / 2016) as u16)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Difficultyepoch {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::Height;
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,15 @@ impl Add<usize> for Halvingepoch {
|
||||
Self::from(self.0 + rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Height> for Halvingepoch {
|
||||
fn from(value: Height) -> Self {
|
||||
Self((u32::from(value) / 210_000) as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Halvingepoch {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ impl Add<usize> for Height {
|
||||
|
||||
impl CheckedSub<Height> for Height {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self::from)
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::{Date, Dateindex, Yearindex};
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,21 @@ impl Add<usize> for Monthindex {
|
||||
Self::from(self.0 + rhs as u16)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Dateindex> for Monthindex {
|
||||
fn from(value: Dateindex) -> Self {
|
||||
Self::from(Date::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Monthindex {
|
||||
fn from(value: Date) -> Self {
|
||||
Self(u16::from(Yearindex::from(value)) * 12 + value.month() as u16 - 1)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Monthindex {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ impl From<usize> for Timestamp {
|
||||
|
||||
impl CheckedSub<Timestamp> for Timestamp {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self::from)
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,3 +94,18 @@ impl Add for Timestamp {
|
||||
Self(self.0 + rhs.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f64> for Timestamp {
|
||||
fn from(value: f64) -> Self {
|
||||
if value < 0.0 || value > u32::MAX as f64 {
|
||||
panic!()
|
||||
}
|
||||
Self(value as u32)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Timestamp> for f64 {
|
||||
fn from(value: Timestamp) -> Self {
|
||||
value.0 as f64
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::{Date, Dateindex};
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,39 @@ impl Add<usize> for Weekindex {
|
||||
Self::from(self.0 + rhs as u16)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Dateindex> for Weekindex {
|
||||
fn from(value: Dateindex) -> Self {
|
||||
Self::from(Date::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Weekindex {
|
||||
fn from(value: Date) -> Self {
|
||||
let date = jiff::civil::Date::from(value).iso_week_date();
|
||||
|
||||
let mut week: u16 = 0;
|
||||
let mut year = 2009;
|
||||
|
||||
while date.year() > year {
|
||||
let d = jiff::civil::Date::new(year, 6, 6).unwrap();
|
||||
let i = d.iso_week_date();
|
||||
let w = i.weeks_in_year();
|
||||
// dbg!(d, w);
|
||||
week += w as u16;
|
||||
year += 1;
|
||||
}
|
||||
|
||||
week += date.week() as u16;
|
||||
|
||||
week -= 1;
|
||||
|
||||
Self(week)
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Weekindex {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ use std::{fmt::Debug, ops::Add};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
|
||||
|
||||
use crate::CheckedSub;
|
||||
|
||||
use super::{Date, Dateindex, Monthindex};
|
||||
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
@@ -46,3 +50,33 @@ impl Add<usize> for Yearindex {
|
||||
Self::from(self.0 + rhs as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Dateindex> for Yearindex {
|
||||
fn from(value: Dateindex) -> Self {
|
||||
Self::from(Date::from(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Date> for Yearindex {
|
||||
fn from(value: Date) -> Self {
|
||||
Self((value.year() - 2009) as u8)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Yearindex> for u16 {
|
||||
fn from(value: Yearindex) -> Self {
|
||||
value.0 as u16
|
||||
}
|
||||
}
|
||||
|
||||
impl CheckedSub for Yearindex {
|
||||
fn checked_sub(self, rhs: Self) -> Option<Self> {
|
||||
self.0.checked_sub(rhs.0).map(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Monthindex> for Yearindex {
|
||||
fn from(value: Monthindex) -> Self {
|
||||
Self((usize::from(value) / 12) as u8)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user