Permalink
Browse files

Problems with xml

Trying parse the xml o ofx file. Isso #4
  • Loading branch information...
fernandobatels committed Nov 27, 2018
1 parent dffa153 commit 3f813aca382ba68b12619b9edcc9c832bba837cb
Showing with 30 additions and 6 deletions.
  1. +1 −1 Cargo.toml
  2. +28 −4 src/backend/ofx.rs
  3. +1 −1 src/main.rs
@@ -9,7 +9,7 @@ authors = ["Luis Fernando Batels <luisfbatels@gmail.com>"]

[dependencies]
json = "*"
libc = "*"
dummy_xml = "*"
uuid = { version = "0.7", features = ["v4"] }
prettytable-rs = "*"
chrono = "*"
@@ -6,13 +6,16 @@
/// Copyright 2018 Luis Fernando Batels <luisfbatels@gmail.com>
///

use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use dummy_xml::parser::*;
use backend::storage::*;
use backend::accounts::*;

pub struct Ofx<'a> {
storage: &'a mut Storage,
account: &'a mut Account,
file_doc: Document<'a>,
file_path: String,
}

@@ -21,11 +24,32 @@ impl<'a> Ofx<'a> {
// Create the object for import ofx file
pub fn new(storage: &'a mut Storage, account: &'a mut Account, file_path: String) -> Result<Ofx<'a>, &'static str> {

if Path::new(&file_path).exists() {
return Ok(Ofx { storage: storage, account: account, file_path: file_path});
let file = File::open(file_path.clone());

if file.is_err() {
return Err("OFX file not found");
}

let mut file_content = String::new();
if file.unwrap().read_to_string(&mut file_content).is_err() {
return Err("Something went wrong reading the OFX file");
}

if !file_content.contains("<OFX>") {
return Err("The OFX file not contains <OFX> tag");
}

let xml_split: Vec<&str> = file_content.split("<OFX>").collect();

let xml_str = format!("<OFX>{}", xml_split[1]);

let xml = parse_string(&xml_str);

if xml.is_err() {
return Err("Invalid XML content in OFX file");
}

Err("OFX file not found")
Ok(Ofx { storage: storage, account: account, file_path: file_path, file_doc: xml.unwrap() })
}

}
@@ -14,7 +14,7 @@ extern crate prettytable;
extern crate chrono;
extern crate csv;
extern crate assert_cmd;
extern crate libc;
extern crate dummy_xml;

mod ui;
mod backend;

0 comments on commit 3f813ac

Please sign in to comment.