summaryrefslogtreecommitdiff
path: root/src/qif.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/qif.rs')
-rw-r--r--src/qif.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/qif.rs b/src/qif.rs
new file mode 100644
index 0000000..46b2bc3
--- /dev/null
+++ b/src/qif.rs
@@ -0,0 +1,76 @@
+use std::fmt;
+
+pub struct QifFile {
+ header: String,
+ entries: Vec<QifEntry>
+}
+
+impl QifFile {
+ pub fn new(header: String) -> QifFile {
+ QifFile {
+ header: header,
+ entries: Vec::new()
+ }
+ }
+
+ pub fn append_non_empty(&mut self, entry: QifEntry) {
+ if !entry.is_empty() {
+ self.entries.push(entry);
+ }
+ }
+
+ pub fn append(&mut self, entry: QifEntry) {
+ self.entries.push(entry);
+ }
+}
+
+impl fmt::Display for QifFile {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ let mut hat_separated = String::new();
+
+ for entry in &self.entries {
+ hat_separated.push_str("\n");
+ hat_separated.push_str(&entry.to_string());
+ hat_separated.push_str("\n^");
+ }
+
+ write!(f, "{}{}", self.header, hat_separated)
+ }
+}
+
+pub struct QifEntry {
+ date: String,
+ amount: String,
+ description: String
+}
+
+const DATE_PREFIX: &str = "D";
+const AMOUNT_PREFIX: &str = "T";
+const DESCRIPTION_PREFIX: &str = "M";
+
+impl QifEntry {
+ pub fn new(lines: &Vec<String>) -> QifEntry {
+ let date = lines.iter().find(|l| l.starts_with(DATE_PREFIX)).expect("No date");
+ let amount = lines.iter().find(|l| l.starts_with(AMOUNT_PREFIX)).expect("No amount");
+ let description = lines.iter().find(|l| l.starts_with(DESCRIPTION_PREFIX)).expect("No description");
+ QifEntry {
+ date: date.clone(),
+ amount: amount.clone(),
+ description: description.clone()
+ }
+ }
+
+ pub fn is_empty(&self) -> bool {
+ self.amount == String::from("T0")
+ }
+
+ pub fn clean_description(&self) -> String {
+ self.description.clone()
+ }
+}
+
+impl fmt::Display for QifEntry {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "{}\n{}\n{}", self.date, self.amount, self.clean_description())
+ }
+}