use std::path::PathBuf;
use crate::shim::sector::SectorSize;
use crate::utils::proofs_api::{get_params_default, SectorSizeOpt};
use crate::cli::subcommands::cli_error_and_die;
use crate::cli_shared::read_config;
#[allow(missing_docs)]
#[derive(Debug, clap::Args)]
pub struct FetchCommands {
#[arg(short, long)]
all: bool,
#[arg(short, long)]
keys: bool,
#[arg(short, long)]
dry_run: bool,
params_size: Option<String>,
#[arg(short, long)]
pub config: Option<PathBuf>,
}
impl FetchCommands {
pub async fn run(self) -> anyhow::Result<()> {
let (_, config) = read_config(self.config.as_ref(), None)?;
let sizes = if self.all {
SectorSizeOpt::All
} else if let Some(size) = &self.params_size {
let sector_size = ram_to_int(size)?;
SectorSizeOpt::Size(sector_size)
} else if self.keys {
SectorSizeOpt::Keys
} else {
cli_error_and_die(
"Sector size option must be chosen. Choose between --all, --keys, or <size>",
1,
);
};
get_params_default(&config.client.data_dir, sizes, self.dry_run).await
}
}
fn ram_to_int(size: &str) -> anyhow::Result<SectorSize> {
let size = size.to_lowercase();
let trimmed = size.trim_end_matches('b');
type SectorSize = crate::shim::sector::SectorSize;
match trimmed {
"2048" | "2ki" => Ok(SectorSize::_2KiB),
"8388608" | "8mi" => Ok(SectorSize::_8MiB),
"536870912" | "512mi" => Ok(SectorSize::_512MiB),
"34359738368" | "32gi" => Ok(SectorSize::_32GiB),
"68719476736" | "64gi" => Ok(SectorSize::_64GiB),
_ => Err(anyhow::Error::msg(format!(
"Failed to parse: {size}. Must be a valid sector size"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
type SectorSize = crate::shim::sector::SectorSize;
#[test]
fn ram_str_conversions() {
assert_eq!(ram_to_int("2048").unwrap(), SectorSize::_2KiB);
assert_eq!(ram_to_int("2048B").unwrap(), SectorSize::_2KiB);
assert_eq!(ram_to_int("2kib").unwrap(), SectorSize::_2KiB);
assert_eq!(ram_to_int("8Mib").unwrap(), SectorSize::_8MiB);
assert_eq!(ram_to_int("512MiB").unwrap(), SectorSize::_512MiB);
assert_eq!(ram_to_int("32Gi").unwrap(), SectorSize::_32GiB);
assert_eq!(ram_to_int("32GiB").unwrap(), SectorSize::_32GiB);
assert_eq!(ram_to_int("64Gib").unwrap(), SectorSize::_64GiB);
}
}