Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate regex;
- extern crate serde_json;
- #[macro_use]
- extern crate serde_derive;
- use std::env;
- use std::fs::File;
- use std::io::*;
- //use regex::RegexSetBuilder;
- #[allow(unused_imports)]
- use serde_json::to_string;
- fn main() {
- // for now, use the filename given via the cli and bring it into the app.
- // the default path for some reason is /users/jamesthornber/* maybe it takes it from the mac default path env?
- let args: Vec<String> = env::args().collect();
- //just accept the one command line arg (filename)
- let filename = &args[1];
- match find(filename, '.'){
- None => println!("No valid filename found, please ensure it contains .ged as a suffix"),
- Some(i) => println!("In file {}", filename),
- }
- let f = File::open(filename).expect("file not found");
- let xd: Vec<String> = BufReader::new(f).lines().map(|l| l.expect("Could not read line")).collect();
- let mut oui: Vec<&str> = Vec::new();
- for line in xd.iter(){
- let mut value: Vec<&str> = line.split(" ").map(|l| l).collect();
- value.push("\n");
- oui.extend(value.iter());
- }
- let mut index = 0;
- let mut the_one:foo::peeps = foo::peeps::default();
- let mut current_mode:String = String::from("");
- let mut current_level:i32 = 0;
- let mut current_level_mode:String = String::from("");
- let mut current_string: String = String::from("");
- let mut current_string_level1: String = String::from("");
- let mut all_peeps: Vec<foo::peeps> = Vec::new();
- let mut nb_peeps: i32 = 0;
- for mdr in oui{
- if index == 0 {
- current_level = mdr.parse::<i32>().unwrap();
- println!("{}",mdr);
- }
- if mdr == "\n" {
- index = 0;
- if current_mode == "BIRT" {
- if current_level_mode == "DATE" {
- the_one.birt.date = current_string;
- } else if current_level_mode == "PLAC"{
- the_one.birt.plac = current_string;
- }
- } else if current_mode == "DEAT" {
- if current_level_mode == "DATE" {
- the_one.deat.date = current_string;
- } else if current_level_mode == "PLAC" {
- the_one.deat.plac = current_string;
- }
- } else if current_mode == "NAME" {
- let surnamesplit: Vec<&str> = current_string_level1.split("/").collect();
- the_one.name = surnamesplit[0].to_string();
- the_one.surname = surnamesplit[1].to_string();
- } else if current_mode == "SEX" {
- the_one.sex = current_string_level1;
- }
- current_string = String::from("");
- current_string_level1 = String::from("");
- continue;
- }
- if current_level == 0 {
- if index == 1 {
- if mdr.contains("@P"){
- if nb_peeps != 0 {
- all_peeps.push(the_one);
- the_one = foo::peeps::default();
- }
- println!("new peep!");
- the_one.level = current_level;
- the_one.birt = foo::Birt::default();
- the_one.birt.date = String::from("");
- nb_peeps += 1;
- } else if mdr.contains("@F"){
- break;
- }
- }
- }
- if current_level == 1 {
- if index == 1 {
- current_mode = mdr.to_string();
- println!("mode: {}",current_mode);
- }
- if index >= 2 {
- current_string_level1.push_str(" ");
- current_string_level1.push_str(mdr);
- }
- }
- if current_level >= 2 {
- if index == 1 {
- current_level_mode = mdr.to_string();
- println!("level_mode: {}",current_level_mode);
- }
- if index >= 2 {
- current_string.push_str(" ");
- current_string.push_str(mdr);
- }
- }
- index += 1;
- }
- all_peeps.push(the_one);
- for peep in all_peeps{
- println!("birth : {} at {}",peep.birt.date,peep.birt.plac);
- println!("deat : {} at {}",peep.deat.date,peep.deat.plac);
- println!("name: {}, surname: {}",peep.name,peep.surname);
- println!("sex: {}",peep.sex);
- serde_sau(peep);
- }
- }
- fn serde_sau(peep:foo::peeps) -> Result<()> {
- let j = serde_json::to_string(&peep)?;
- println!("{}",j);
- return Ok(());
- }
- /*
- fn build_peeps(level: i32) -> peeps {
- peeps {
- level,
- at: String::from("at"),
- birt: foo::Birt::default(),
- date: String::from("date"),
- plac: String::from("plac"),
- sour: String::from("sour"),
- apid: String::from("apid"),
- page: String::from("page"),
- deat: String::from("deat"),
- name: String::from("name")
- }
- }
- */
- pub struct FileFormat {
- //when the file is read-in then we need to define where to put what, so this defines the header struct, and the peeps struct
- header: Header,
- people: Vec<foo::peeps>,
- }
- pub struct Header {
- //contains everything in the file header
- level: i32,
- head: String,
- char: String,
- sour: String,
- vers: String,
- name: String,
- corp: String,
- gedc: String,
- form: String,
- }
- mod foo {
- #[derive(Serialize, Deserialize)]
- pub struct Birt {
- pub date: String,
- pub plac: String
- }
- // for Birth
- impl Birt {
- pub fn default() -> Birt {
- Birt {
- date: String::from(""),
- plac: String::from("")
- }
- }
- }
- #[derive(Serialize, Deserialize)]
- pub struct peeps {
- //contains everything in the people section
- pub level: i32,
- pub birt: Birt,
- pub surname: String,
- pub deat: Birt,
- pub name: String,
- pub sex: String,
- }
- impl peeps {
- pub fn default() -> peeps {
- peeps {
- level: 0,
- birt: Birt::default(),
- surname: String::from(""),
- deat: Birt::default(),
- name: String::from(""),
- sex: String::from("")
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment