1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use std::fs; use crate::core::{EmptyResult, GenericResult}; use crate::types::{Date, Decimal}; use self::foreign_income::{ForeignIncome, CurrencyIncome, CountryCode, CurrencyInfo, DeductionInfo, IncomeType, ControlledForeignCompanyInfo}; use self::record::Record; use self::parser::{TaxStatementReader, TaxStatementWriter}; #[macro_use] mod record; mod encoding; mod foreign_income; mod parser; mod types; #[derive(Debug)] pub struct TaxStatement { path: String, pub year: i32, records: Vec<Box<dyn Record>>, } impl TaxStatement { pub fn read(path: &str) -> GenericResult<TaxStatement> { Ok(TaxStatementReader::read(path).map_err(|e| format!( "Error while reading {:?} tax statement: {}", path, e))?) } pub fn save(&self) -> EmptyResult { let temp_path = format!("{}.new", self.path); TaxStatementWriter::write(self, &temp_path).map_err(|e| { let _ = fs::remove_file(&temp_path); format!("Failed to save the tax statement to {:?}: {}", temp_path, e) })?; fs::rename(&temp_path, &self.path).map_err(|e| { let _ = fs::remove_file(&temp_path); format!("Failed to rename {:?} to {:?}: {}", temp_path, self.path, e) })?; Ok(()) } pub fn add_dividend_income( &mut self, description: &str, date: Date, currency: &str, currency_rate: Decimal, amount: Decimal, paid_tax: Decimal, local_amount: Decimal, local_paid_tax: Decimal, ) -> EmptyResult { self.get_foreign_incomes()?.push(CurrencyIncome { type_: IncomeType::Dividend, description: description.to_owned(), county_code: CountryCode::Usa, date: date, tax_payment_date: date, currency: CurrencyInfo::new(currency, currency_rate)?, amount: amount, local_amount: local_amount, paid_tax: paid_tax, local_paid_tax: local_paid_tax, deduction: DeductionInfo::new_none(), controlled_foreign_company: ControlledForeignCompanyInfo::new_none(), }); Ok(()) } pub fn add_interest_income( &mut self, description: &str, date: Date, currency: &str, currency_rate: Decimal, amount: Decimal, local_amount: Decimal, ) -> EmptyResult { self.get_foreign_incomes()?.push(CurrencyIncome { type_: IncomeType::Interest, description: description.to_owned(), county_code: CountryCode::Usa, date: date, tax_payment_date: date, currency: CurrencyInfo::new(currency, currency_rate)?, amount: amount, local_amount: local_amount, paid_tax: dec!(0), local_paid_tax: dec!(0), deduction: DeductionInfo::new_none(), controlled_foreign_company: ControlledForeignCompanyInfo::new_none(), }); Ok(()) } pub fn add_stock_income( &mut self, description: &str, date: Date, currency: &str, currency_rate: Decimal, amount: Decimal, local_amount: Decimal, purchase_local_cost: Decimal, ) -> EmptyResult { self.get_foreign_incomes()?.push(CurrencyIncome { type_: IncomeType::Stock, description: description.to_owned(), county_code: CountryCode::Usa, date: date, tax_payment_date: date, currency: CurrencyInfo::new(currency, currency_rate)?, amount: amount, local_amount: local_amount, paid_tax: dec!(0), local_paid_tax: dec!(0), deduction: DeductionInfo { code: 201, amount: purchase_local_cost, }, controlled_foreign_company: ControlledForeignCompanyInfo::new_none(), }); Ok(()) } fn get_foreign_incomes(&mut self) -> GenericResult<&mut Vec<CurrencyIncome>> { Ok(self.get_mut_record(ForeignIncome::RECORD_NAME)? .map(|record: &mut ForeignIncome| &mut record.incomes) .ok_or("Foreign income must be enabled in the tax statement")?) } fn get_mut_record<T: 'static + Record>(&mut self, name: &str) -> GenericResult<Option<&mut T>> { let mut found_record = None; for record in &mut self.records { if record.name() != name { continue; } if found_record.is_some() { return Err!("The statement has several {} records", name); } found_record = Some(record); } Ok(match found_record { Some(record) => Some( record.as_mut_any().downcast_mut::<T>().ok_or_else(|| format!( "Failed to cast {} record to the underlaying type", name))?), None => None, }) } }