ord/subcommand/
find.rs

1use super::*;
2
3#[derive(Debug, Parser)]
4pub(crate) struct Find {
5  #[arg(help = "Find output and offset of <SAT>.")]
6  sat: Sat,
7  #[clap(help = "Find output and offset of all sats in the range [<SAT>, <END>).")]
8  end: Option<Sat>,
9}
10
11#[derive(Debug, PartialEq, Serialize, Deserialize)]
12pub struct Output {
13  pub satpoint: SatPoint,
14}
15
16#[derive(Debug, PartialEq, Serialize, Deserialize)]
17pub struct FindRangeOutput {
18  pub start: u64,
19  pub size: u64,
20  pub satpoint: SatPoint,
21}
22
23impl Find {
24  pub(crate) fn run(self, settings: Settings) -> SubcommandResult {
25    let index = Index::open(&settings)?;
26
27    if !index.has_sat_index() {
28      bail!("find requires index created with `--index-sats` flag");
29    }
30
31    index.update()?;
32
33    match self.end {
34      Some(end) => match index.find_range(self.sat, end)? {
35        Some(mut results) => {
36          results.sort_by_key(|find_range_output| find_range_output.start);
37          Ok(Some(Box::new(results)))
38        }
39        None => Err(anyhow!("range has not been mined as of index height")),
40      },
41      None => match index.find(self.sat)? {
42        Some(satpoint) => Ok(Some(Box::new(Output { satpoint }))),
43        None => Err(anyhow!("sat has not been mined as of index height")),
44      },
45    }
46  }
47}