Files
himalaya/src/account.rs
T
2026-05-20 00:53:23 +02:00

49 lines
1.5 KiB
Rust

use std::{env::temp_dir, path::PathBuf};
use crate::config::{AccountConfig, Config};
use anyhow::Result;
use comfy_table::{presets, ContentArrangement};
use dirs::download_dir;
#[derive(Debug)]
pub struct Account<B> {
pub backend: B,
pub downloads_dir: PathBuf,
pub table_preset: String,
pub table_arrangement: ContentArrangement,
}
impl<B> Account<B> {
pub fn new(config: Config, account_config: AccountConfig, backend: B) -> Result<Self> {
Ok(Self {
backend,
downloads_dir: account_config
.downloads_dir
.as_ref()
.and_then(|dir| dir.to_str())
.and_then(|dir| shellexpand::full(dir).ok())
.map(|dir| PathBuf::from(dir.to_string()))
.or(config
.downloads_dir
.as_ref()
.and_then(|dir| dir.to_str())
.and_then(|dir| shellexpand::full(dir).ok())
.map(|dir| PathBuf::from(dir.to_string())))
.or(download_dir())
.unwrap_or_else(temp_dir),
table_preset: config
.table_preset
.or(account_config.table_preset)
.unwrap_or(presets::UTF8_FULL_CONDENSED.to_string()),
table_arrangement: config
.table_arrangement
.or(account_config.table_arrangement)
.unwrap_or_default()
.into(),
})
}
}