6 changed files with 116 additions and 6 deletions
@ -0,0 +1,77 @@
|
||||
use crate::commands::{ClapArgs, Configurable, Executable}; |
||||
use crate::config::AppConfig; |
||||
use crate::container::Container; |
||||
use anyhow::Result; |
||||
use async_trait::async_trait; |
||||
use config::ConfigBuilder; |
||||
use config::builder::DefaultState; |
||||
use serde::Deserialize; |
||||
|
||||
#[derive(Debug, Deserialize, Clone)] |
||||
pub struct Config { |
||||
pub show_counts: bool, |
||||
} |
||||
|
||||
#[derive(ClapArgs, Debug, Clone)] |
||||
pub struct ListDictsCmd { |
||||
#[arg(short, long, help = defaults::HELP_LIST_DICTS_COUNTS)] |
||||
pub show_counts: bool, |
||||
} |
||||
|
||||
impl Configurable for ListDictsCmd { |
||||
fn apply_defaults( |
||||
&self, |
||||
builder: ConfigBuilder<DefaultState>, |
||||
) -> Result<ConfigBuilder<DefaultState>> { |
||||
builder |
||||
.set_default("list_dicts.show_counts", defaults::SHOW_COUNTS) |
||||
.map_err(Into::into) |
||||
} |
||||
|
||||
fn apply_overrides( |
||||
&self, |
||||
builder: ConfigBuilder<DefaultState>, |
||||
) -> Result<ConfigBuilder<DefaultState>> { |
||||
builder |
||||
.set_override("list_dicts.show_counts", self.show_counts) |
||||
.map_err(Into::into) |
||||
} |
||||
} |
||||
|
||||
#[async_trait] |
||||
impl Executable for ListDictsCmd { |
||||
async fn execute(&self, config: &AppConfig, container: &Container) -> Result<()> { |
||||
let config = config |
||||
.list_dicts |
||||
.as_ref() |
||||
.expect("ListDicts config not set"); |
||||
|
||||
let repo = container.create_dict_repo("default").await?; |
||||
|
||||
let dicts = repo.fetch_dicts().await?; |
||||
|
||||
if dicts.is_empty() { |
||||
println!("No dictionaries found."); |
||||
return Ok(()); |
||||
} |
||||
|
||||
println!("Dictionaries:"); |
||||
for dict_name in &dicts { |
||||
if config.show_counts { |
||||
let count_repo = container.create_dict_repo(dict_name).await?; |
||||
let count = count_repo.count_entries().await?; |
||||
println!(" - {} ({} entries)", dict_name, count); |
||||
} else { |
||||
println!(" - {}", dict_name); |
||||
} |
||||
} |
||||
|
||||
Ok(()) |
||||
} |
||||
} |
||||
|
||||
mod defaults { |
||||
use const_format::formatcp; |
||||
pub const SHOW_COUNTS: bool = false; |
||||
pub const HELP_LIST_DICTS_COUNTS: &str = formatcp!("Show entry counts for each dictionary"); |
||||
} |
||||
Loading…
Reference in new issue