You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
2.1 KiB

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 name: String,
pub path: String,
}
#[derive(ClapArgs, Debug, Clone)]
pub struct ImportDictCmd {
#[arg(long, help = defaults::HELP_IMPORT_DICT_NAME)]
pub name: String,
#[arg(long, help = defaults::HELP_IMPORT_DICT_INPUT)]
pub path: String,
}
impl Configurable for ImportDictCmd {
fn apply_defaults(
&self,
builder: ConfigBuilder<DefaultState>,
) -> Result<ConfigBuilder<DefaultState>> {
builder
.set_default("import_dict.name", defaults::IMPORT_DICT_NAME)?
.set_default("import_dict.path", defaults::IMPORT_DICT_PATH)
.map_err(Into::into)
}
fn apply_overrides(
&self,
builder: ConfigBuilder<DefaultState>,
) -> Result<ConfigBuilder<DefaultState>> {
builder
.set_override("import_dict.name", self.name.clone())?
.set_override("import_dict.path", self.path.clone())
.map_err(Into::into)
}
}
#[async_trait]
impl Executable for ImportDictCmd {
async fn execute(&self, config: &AppConfig, container: &Container) -> Result<()> {
let config = config
.import_dict
.as_ref()
.expect("ImportDict config not set");
let importer = container.create_dict_importer(&config.name).await?;
// Importer expects an impl DictSource
// We need to create a DictSource from the path
use applib::JsonFileDictSource;
let source = JsonFileDictSource::new(&config.path)?;
importer.import(source).await?;
Ok(())
}
}
mod defaults {
use const_format::formatcp;
pub const IMPORT_DICT_NAME: &str = "";
pub const IMPORT_DICT_PATH: &str = "";
pub const HELP_IMPORT_DICT_NAME: &str = formatcp!("Dictionary name");
pub const HELP_IMPORT_DICT_INPUT: &str = formatcp!("Dictionary file path");
}