use crate::{
error,
params::{ImportParams, SharedParams},
CliConfiguration,
};
use clap::Parser;
use pezsc_client_api::HeaderBackend;
use pezsc_service::chain_ops::import_blocks;
use pezsp_runtime::traits::Block as BlockT;
use std::{
fmt::Debug,
fs,
io::{self, Read},
path::PathBuf,
sync::Arc,
};
#[derive(Debug, Parser)]
pub struct ImportBlocksCmd {
#[arg()]
pub input: Option<PathBuf>,
#[arg(long, value_name = "COUNT")]
pub default_heap_pages: Option<u32>,
#[arg(long)]
pub binary: bool,
#[allow(missing_docs)]
#[clap(flatten)]
pub shared_params: SharedParams,
#[allow(missing_docs)]
#[clap(flatten)]
pub import_params: ImportParams,
}
impl ImportBlocksCmd {
pub async fn run<B, C, IQ>(&self, client: Arc<C>, import_queue: IQ) -> error::Result<()>
where
C: HeaderBackend<B> + Send + Sync + 'static,
B: BlockT + for<'de> serde::Deserialize<'de>,
IQ: pezsc_service::ImportQueue<B> + 'static,
{
let file: Box<dyn Read + Send> = match &self.input {
Some(filename) => Box::new(fs::File::open(filename)?),
None => Box::new(io::stdin()),
};
import_blocks(client, import_queue, file, false, self.binary)
.await
.map_err(Into::into)
}
}
impl CliConfiguration for ImportBlocksCmd {
fn shared_params(&self) -> &SharedParams {
&self.shared_params
}
fn import_params(&self) -> Option<&ImportParams> {
Some(&self.import_params)
}
}