Skip to content

Commit

Permalink
Use strong types (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jan 19, 2022
1 parent 9e5cc10 commit 3fcb800
Show file tree
Hide file tree
Showing 20 changed files with 756 additions and 252 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ autotests = false

[dependencies]
bitcoin = "0.27.1"
derive_more = "0.99.17"
dirs = "4.0.0"
env_logger = "0.9.0"
executable-path = "1.0.0"
Expand Down
7 changes: 6 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ log := '0'

export RUST_LOG := log

ci: clippy
ci: clippy forbid
cargo fmt -- --check
cargo test --release
cargo test

forbid:
./bin/forbid

fmt:
cargo fmt

clippy:
cargo clippy
Expand Down
39 changes: 0 additions & 39 deletions src/arguments.rs

This file was deleted.

47 changes: 47 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use super::*;

mod epochs;
mod find;
mod range;
mod supply;
mod traits;

#[derive(StructOpt)]
pub(crate) enum Command {
Epochs,
Find {
#[structopt(long)]
blocksdir: Option<PathBuf>,
ordinal: Ordinal,
height: u64,
},
Name {
name: String,
},
Range {
#[structopt(long)]
name: bool,
height: Height,
},
Supply,
Traits {
ordinal: Ordinal,
},
}

impl Command {
pub(crate) fn run(self) -> Result<()> {
match self {
Self::Epochs => epochs::run(),
Self::Find {
blocksdir,
ordinal,
height,
} => find::run(blocksdir.as_deref(), ordinal, height),
Self::Name { name } => name::run(&name),
Self::Range { height, name } => range::run(height, name),
Self::Supply => supply::run(),
Self::Traits { ordinal } => traits::run(ordinal),
}
}
}
9 changes: 9 additions & 0 deletions src/command/epochs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use super::*;

pub(crate) fn run() -> Result {
for ordinal in Epoch::STARTING_ORDINALS {
println!("{}", ordinal);
}

Ok(())
}
6 changes: 3 additions & 3 deletions src/find.rs → src/command/find.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::*;

pub(crate) fn run(blocksdir: Option<&Path>, ordinal: u64, at_height: u64) -> Result<()> {
pub(crate) fn run(blocksdir: Option<&Path>, ordinal: Ordinal, at_height: u64) -> Result<()> {
let index = Index::new(blocksdir)?;

let height = ordinal / (50 * 100_000_000);
let height = ordinal.height().n();
assert!(height < 100);
assert!(at_height == height);

let block = index.block(height)?;

let position = ordinal % (50 * 100_000_000);
let position = ordinal.subsidy_position();

let mut ordinal = 0;
for (i, output) in block.txdata[0].output.iter().enumerate() {
Expand Down
31 changes: 31 additions & 0 deletions src/command/range.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::*;

pub(crate) fn run(height: Height, name_range: bool) -> Result {
let mut start = 0;

for n in 0..height.n() {
let subsidy = Height(n).subsidy();

if subsidy == 0 {
break;
}

start += subsidy;
}

let end = start + height.subsidy();

if name_range {
let (start, end) = match (Ordinal::new_checked(start), Ordinal::new_checked(end)) {
(Some(start), Some(end)) => (start.name(), end.name()),
(Some(start), None) => (start.name(), start.name()),
(None, None) => (Ordinal::LAST.name(), Ordinal::LAST.name()),
(None, Some(_)) => unreachable!(),
};
println!("[{},{})", start, end);
} else {
println!("[{},{})", start, end);
}

Ok(())
}
6 changes: 3 additions & 3 deletions src/supply.rs → src/command/supply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ pub(crate) fn run() -> Result {
let mut last = 0;

loop {
if subsidy(last + 1) == 0 {
if Height(last + 1).subsidy() == 0 {
break;
}
last += 1;
}

println!("supply: {}", SUPPLY);
println!("supply: {}", Ordinal::SUPPLY);
println!("first: {}", 0);
println!("last: {}", SUPPLY - 1);
println!("last: {}", Ordinal::SUPPLY - 1);
println!("last mined in block: {}", last);

Ok(())
Expand Down
68 changes: 68 additions & 0 deletions src/command/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use super::*;

pub(crate) fn run(ordinal: Ordinal) -> Result {
let n = ordinal.n();

if n % 2 == 0 {
println!("even");
} else {
println!("odd");
}

let isqrt = n.integer_sqrt();
if isqrt * isqrt == n {
println!("square");
}

let icbrt = n.integer_cbrt();
if icbrt * icbrt * icbrt == n {
println!("cube");
}

let digits = n.to_string().chars().collect::<Vec<char>>();

let pi = std::f64::consts::PI.to_string().replace('.', "");
let s = n.to_string();
if s == pi[..s.len()] {
println!("pi");
}

if digits.chunks(2).all(|chunk| chunk == ['6', '9']) {
println!("nice");
}

if digits.iter().all(|c| *c == '7') {
println!("angelic");
}

println!(
"luck: {}/{}",
digits.iter().filter(|c| **c == '8').count() as i64
- digits.iter().filter(|c| **c == '4').count() as i64,
digits.len()
);

println!("population: {}", ordinal.population());

println!("name: {}", ordinal.name());

if let Some(character) = char::from_u32((n % 0x110000) as u32) {
println!("character: {:?}", character);
}

println!("height: {}", ordinal.height());

if ordinal.subsidy_position() == 0 {
println!("shiny");
}

if ordinal.height() == 124724 {
if ordinal == 623624999999999 {
println!("illusive");
} else {
println!("cursed");
}
}

Ok(())
}
Loading

0 comments on commit 3fcb800

Please sign in to comment.