main.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use qif_generator::account::{Account, AccountType};
  2. use std::path::PathBuf;
  3. use structopt::StructOpt;
  4. mod categories;
  5. mod convert;
  6. mod import;
  7. mod receipt;
  8. #[cfg(feature = "telegram")]
  9. mod telegram;
  10. mod ui;
  11. mod user;
  12. /// Search for a pattern in a file and display the lines that contain it.
  13. #[derive(StructOpt)]
  14. struct Cli {
  15. #[structopt(parse(from_os_str), long, help = "Accounts csv file")]
  16. accounts: Option<PathBuf>,
  17. #[structopt(short, long)]
  18. database: Option<String>,
  19. #[structopt(long, default_value = "New")]
  20. memo: String,
  21. /// Run telegram bot
  22. #[cfg(feature = "telegram")]
  23. #[structopt(short, long)]
  24. telegram: bool,
  25. /// Run Turbo Vision UI
  26. #[cfg(feature = "tv")]
  27. #[structopt(long)]
  28. ui: bool,
  29. /// The path to the file to read
  30. #[structopt(required_unless_one = &["telegram", "ui"])]
  31. filename: Option<String>,
  32. /// Account name
  33. #[structopt(long, default_value = "Wallet")]
  34. account: String,
  35. /// Account type
  36. #[structopt(long, parse(try_from_str), default_value = "Cash")]
  37. account_type: AccountType,
  38. }
  39. #[cfg(not(tarpaulin_include))]
  40. fn main() {
  41. let args = Cli::from_args();
  42. #[cfg(feature = "telegram")]
  43. if args.telegram {
  44. telegram::bot();
  45. return;
  46. }
  47. let mut user = user::User::new(0, &args.database);
  48. match args.accounts {
  49. None => (),
  50. Some(path) => user.accounts(import::read_accounts(&path).unwrap()),
  51. }
  52. #[cfg(feature = "tv")]
  53. if args.ui {
  54. ui::run_tv();
  55. return;
  56. }
  57. // If program is used as command-line tool
  58. let acc = Account::new()
  59. .name(&args.account)
  60. .account_type(args.account_type)
  61. .build();
  62. if let Some(filename) = &args.filename {
  63. let cat = &|item: &str, stats: &mut categories::CatStats, acc: &[String]| -> String {
  64. categories::get_category(&item, stats, acc)
  65. };
  66. let t = convert::convert(filename, &args.memo, &mut user, &acc, &cat).unwrap();
  67. print!("{}", acc.to_string());
  68. println!("{}", t.to_string());
  69. }
  70. }