global: snapshot + core: impl Display for bytes structs

This commit is contained in:
nym21
2025-03-04 12:29:19 +01:00
parent fc6f12fb22
commit 0d0edd7917
36 changed files with 782 additions and 331 deletions

View File

@@ -38,7 +38,8 @@ impl Index {
]
}
pub fn self_to_ids(&self) -> &[&str] {
pub fn possible_values(&self) -> &[&str] {
// Always have the "correct" id at the end
match self {
Self::Dateindex => &["d", "date", "dateindex"],
Self::Height => &["h", "height"],
@@ -56,10 +57,10 @@ impl Index {
}
}
pub fn possible_values() -> Vec<String> {
pub fn all_possible_values() -> Vec<String> {
Self::all()
.iter()
.flat_map(|i| i.self_to_ids().iter().map(|s| s.to_string()))
.flat_map(|i| i.possible_values().iter().map(|s| s.to_string()))
.collect::<Vec<_>>()
}
}
@@ -68,19 +69,19 @@ impl TryFrom<&str> for Index {
type Error = color_eyre::Report;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Ok(match value {
v if (Self::Dateindex).self_to_ids().contains(&v) => Self::Dateindex,
v if (Self::Height).self_to_ids().contains(&v) => Self::Height,
v if (Self::Txindex).self_to_ids().contains(&v) => Self::Txindex,
v if (Self::Txinindex).self_to_ids().contains(&v) => Self::Txinindex,
v if (Self::Txoutindex).self_to_ids().contains(&v) => Self::Txoutindex,
v if (Self::Addressindex).self_to_ids().contains(&v) => Self::Addressindex,
v if (Self::P2PK33index).self_to_ids().contains(&v) => Self::P2PK33index,
v if (Self::P2PK65index).self_to_ids().contains(&v) => Self::P2PK65index,
v if (Self::P2PKHindex).self_to_ids().contains(&v) => Self::P2PKHindex,
v if (Self::P2SHindex).self_to_ids().contains(&v) => Self::P2SHindex,
v if (Self::P2TRindex).self_to_ids().contains(&v) => Self::P2TRindex,
v if (Self::P2WPKHindex).self_to_ids().contains(&v) => Self::P2WPKHindex,
v if (Self::P2WSHindex).self_to_ids().contains(&v) => Self::P2WSHindex,
v if (Self::Dateindex).possible_values().contains(&v) => Self::Dateindex,
v if (Self::Height).possible_values().contains(&v) => Self::Height,
v if (Self::Txindex).possible_values().contains(&v) => Self::Txindex,
v if (Self::Txinindex).possible_values().contains(&v) => Self::Txinindex,
v if (Self::Txoutindex).possible_values().contains(&v) => Self::Txoutindex,
v if (Self::Addressindex).possible_values().contains(&v) => Self::Addressindex,
v if (Self::P2PK33index).possible_values().contains(&v) => Self::P2PK33index,
v if (Self::P2PK65index).possible_values().contains(&v) => Self::P2PK65index,
v if (Self::P2PKHindex).possible_values().contains(&v) => Self::P2PKHindex,
v if (Self::P2SHindex).possible_values().contains(&v) => Self::P2SHindex,
v if (Self::P2TRindex).possible_values().contains(&v) => Self::P2TRindex,
v if (Self::P2WPKHindex).possible_values().contains(&v) => Self::P2WPKHindex,
v if (Self::P2WSHindex).possible_values().contains(&v) => Self::P2WSHindex,
_ => return Err(eyre!("Bad index")),
})
}

View File

@@ -1,6 +1,7 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc = include_str!("../README.md")]
#![doc = "\n## Example\n\n```rust"]
#![doc = include_str!("main.rs")]
#![doc = include_str!("../examples/main.rs")]
#![doc = "```"]
use brk_computer::Computer;
@@ -61,10 +62,15 @@ impl<'a> Query<'a> {
let tuples = ids
.iter()
.map(|s| {
(
s.to_owned(),
self.vecid_to_index_to_vec.get(&s.to_lowercase().replace("_", "-")),
)
let mut id = s.to_lowercase().replace("_", "-");
let mut res = self.vecid_to_index_to_vec.get(&id);
if res.is_none() {
if let Ok(index) = Index::try_from(id.as_str()) {
id = index.possible_values().last().unwrap().to_string();
res = self.vecid_to_index_to_vec.get(&id)
}
}
(id, res)
})
.filter(|(_, opt)| opt.is_some())
.map(|(id, vec)| (id, vec.unwrap()))
@@ -77,7 +83,9 @@ impl<'a> Query<'a> {
let mut values = tuples
.iter()
.flat_map(|(_, i_to_v)| i_to_v.get(&index))
.map(|vec| -> brk_vec::Result<Vec<serde_json::Value>> { vec.collect_range_values(from, to) })
.map(|vec| -> brk_vec::Result<Vec<serde_json::Value>> {
vec.collect_range_values(from, to)
})
.collect::<brk_vec::Result<Vec<_>>>()?;
if values.is_empty() {
@@ -88,7 +96,11 @@ impl<'a> Query<'a> {
Ok(match format {
Some(Format::CSV) | Some(Format::TSV) => {
let delimiter = if format == Some(Format::CSV) { ',' } else { '\t' };
let delimiter = if format == Some(Format::CSV) {
','
} else {
'\t'
};
let mut text = tuples
.into_iter()
@@ -120,7 +132,8 @@ impl<'a> Query<'a> {
}
}
Some(Format::MD) => {
let mut table = values.to_table(ids.iter().map(|s| s.to_string()).collect::<Vec<_>>());
let mut table =
values.to_table(tuples.iter().map(|(s, _)| s.to_owned()).collect::<Vec<_>>());
table.with(Style::markdown());

View File

@@ -1,25 +0,0 @@
use std::path::Path;
use brk_computer::Computer;
use brk_indexer::Indexer;
use brk_query::{Index, Query};
pub fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let outputs_dir = Path::new("../../_outputs");
let mut indexer = Indexer::new(&outputs_dir.join("indexed"))?;
indexer.import_vecs()?;
let mut computer = Computer::new(&outputs_dir.join("computed"));
computer.import_vecs()?;
let query = Query::build(&indexer, &computer);
dbg!(query.search(Index::Height, &["date"], Some(-1), None, None)?);
dbg!(query.search(Index::Height, &["date"], Some(-10), None, None)?);
dbg!(query.search(Index::Height, &["date", "timestamp"], Some(-10), None, None)?);
Ok(())
}

View File

@@ -5,7 +5,7 @@ use crate::{Format, Index};
#[derive(Debug, Deserialize, Parser)]
pub struct Params {
#[clap(short, long, value_parser = PossibleValuesParser::new(Index::possible_values()))]
#[clap(short, long, value_parser = PossibleValuesParser::new(Index::all_possible_values()))]
/// Index of the values requested
pub index: String,
#[clap(short, long, value_delimiter = ' ', num_args = 1..)]