pub mod fields;
pub mod isbn;
pub mod lang;
pub mod table;
pub use lang::lang_code_to_name;
pub use table::{build_table, build_vertical_table};
use crate::i18n::tr_with;
use chrono::Local;
use colored::*;
use rusqlite::Result as SqlResult;
use rusqlite::{Connection, Result};
use std::fs::File;
use std::io;
use std::sync::OnceLock;
static VERBOSE: OnceLock<bool> = OnceLock::new();
pub fn set_verbose(enabled: bool) {
let _ = VERBOSE.set(enabled);
}
pub fn is_verbose() -> bool {
*VERBOSE.get().unwrap_or(&false)
}
pub fn now_str() -> String {
Local::now().format("%+").to_string()
}
pub fn write_log(conn: &Connection, operation: &str, target: &str, message: &str) -> Result<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
operation TEXT NOT NULL,
target TEXT DEFAULT '',
message TEXT NOT NULL
);",
[],
)?;
let now = now_str();
conn.execute(
"INSERT INTO log (date, operation, target, message) VALUES (?1, ?2, ?3, ?4)",
(&now, &operation, &target, &message),
)?;
Ok(())
}
pub mod icons {
pub const OK: &str = "✅ ";
pub const ERR: &str = "❌ ";
pub const WARN: &str = "⚠️ ";
pub const INFO: &str = "📘 ";
}
pub fn print_ok(msg: &str, verbose: bool) {
if !verbose {
return;
}
println!("{}{}", icons::OK, msg.green().bold());
}
pub fn print_err(msg: &str) {
eprintln!("{}{}", icons::ERR, msg.red().bold());
}
pub fn print_warn(msg: &str) {
println!("{}{}", icons::WARN, msg.yellow().bold());
}
pub fn print_info(msg: &str, verbose: bool) {
if !verbose {
return;
}
println!("{}{}", icons::INFO, msg.blue().bold());
}
pub fn open_import_file(file: &str) -> Result<File, io::Error> {
let file_display = file.to_string();
File::open(file).inspect_err(|e| {
print_err(&tr_with(
"import.error.open_failed",
&[("file", &file_display), ("error", &e.to_string())],
));
})
}
pub fn handle_import_result(
result: &SqlResult<usize>,
imported: &mut u32,
failed: &mut u32,
title: &str,
) {
match result {
Ok(_) => {
*imported += 1;
}
Err(e) => {
*failed += 1;
print_err(&tr_with(
"import.error.insert_failed",
&[("title", title), ("error", &e.to_string())],
));
}
}
}