Guest User

Untitled

a guest
Oct 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. extern crate regex;
  2. extern crate serde_json;
  3.  
  4. #[macro_use]
  5. extern crate serde_derive;
  6.  
  7. use std::env;
  8. use std::fs::File;
  9. use std::io::*;
  10. //use regex::RegexSetBuilder;
  11. #[allow(unused_imports)]
  12. use serde_json::to_string;
  13.  
  14.  
  15. fn main() {
  16. // for now, use the filename given via the cli and bring it into the app.
  17. // the default path for some reason is /users/jamesthornber/* maybe it takes it from the mac default path env?
  18. let args: Vec<String> = env::args().collect();
  19. //just accept the one command line arg (filename)
  20. let filename = &args[1];
  21. match find(filename, '.'){
  22. None => println!("No valid filename found, please ensure it contains .ged as a suffix"),
  23. Some(i) => println!("In file {}", filename),
  24. }
  25. let f = File::open(filename).expect("file not found");
  26. let xd: Vec<String> = BufReader::new(f).lines().map(|l| l.expect("Could not read line")).collect();
  27.  
  28. let mut oui: Vec<&str> = Vec::new();
  29. for line in xd.iter(){
  30. let mut value: Vec<&str> = line.split(" ").map(|l| l).collect();
  31. value.push("\n");
  32. oui.extend(value.iter());
  33. }
  34.  
  35. let mut index = 0;
  36. let mut the_one:foo::peeps = foo::peeps::default();
  37. let mut current_mode:String = String::from("");
  38. let mut current_level:i32 = 0;
  39. let mut current_level_mode:String = String::from("");
  40. let mut current_string: String = String::from("");
  41. let mut current_string_level1: String = String::from("");
  42. let mut all_peeps: Vec<foo::peeps> = Vec::new();
  43.  
  44. let mut nb_peeps: i32 = 0;
  45. for mdr in oui{
  46. if index == 0 {
  47. current_level = mdr.parse::<i32>().unwrap();
  48. println!("{}",mdr);
  49. }
  50.  
  51. if mdr == "\n" {
  52.  
  53. index = 0;
  54. if current_mode == "BIRT" {
  55. if current_level_mode == "DATE" {
  56. the_one.birt.date = current_string;
  57. } else if current_level_mode == "PLAC"{
  58. the_one.birt.plac = current_string;
  59. }
  60. } else if current_mode == "DEAT" {
  61. if current_level_mode == "DATE" {
  62. the_one.deat.date = current_string;
  63. } else if current_level_mode == "PLAC" {
  64. the_one.deat.plac = current_string;
  65. }
  66. } else if current_mode == "NAME" {
  67. let surnamesplit: Vec<&str> = current_string_level1.split("/").collect();
  68. the_one.name = surnamesplit[0].to_string();
  69. the_one.surname = surnamesplit[1].to_string();
  70. } else if current_mode == "SEX" {
  71. the_one.sex = current_string_level1;
  72. }
  73.  
  74. current_string = String::from("");
  75. current_string_level1 = String::from("");
  76. continue;
  77. }
  78.  
  79. if current_level == 0 {
  80. if index == 1 {
  81. if mdr.contains("@P"){
  82. if nb_peeps != 0 {
  83. all_peeps.push(the_one);
  84. the_one = foo::peeps::default();
  85. }
  86. println!("new peep!");
  87. the_one.level = current_level;
  88. the_one.birt = foo::Birt::default();
  89. the_one.birt.date = String::from("");
  90. nb_peeps += 1;
  91. } else if mdr.contains("@F"){
  92. break;
  93. }
  94. }
  95. }
  96.  
  97. if current_level == 1 {
  98. if index == 1 {
  99. current_mode = mdr.to_string();
  100. println!("mode: {}",current_mode);
  101. }
  102. if index >= 2 {
  103. current_string_level1.push_str(" ");
  104. current_string_level1.push_str(mdr);
  105. }
  106. }
  107.  
  108. if current_level >= 2 {
  109. if index == 1 {
  110. current_level_mode = mdr.to_string();
  111. println!("level_mode: {}",current_level_mode);
  112. }
  113. if index >= 2 {
  114. current_string.push_str(" ");
  115. current_string.push_str(mdr);
  116. }
  117. }
  118.  
  119. index += 1;
  120. }
  121. all_peeps.push(the_one);
  122. for peep in all_peeps{
  123. println!("birth : {} at {}",peep.birt.date,peep.birt.plac);
  124. println!("deat : {} at {}",peep.deat.date,peep.deat.plac);
  125. println!("name: {}, surname: {}",peep.name,peep.surname);
  126. println!("sex: {}",peep.sex);
  127. serde_sau(peep);
  128. }
  129.  
  130. }
  131.  
  132. fn serde_sau(peep:foo::peeps) -> Result<()> {
  133. let j = serde_json::to_string(&peep)?;
  134. println!("{}",j);
  135. return Ok(());
  136. }
  137.  
  138. /*
  139. fn build_peeps(level: i32) -> peeps {
  140. peeps {
  141. level,
  142. at: String::from("at"),
  143. birt: foo::Birt::default(),
  144. date: String::from("date"),
  145. plac: String::from("plac"),
  146. sour: String::from("sour"),
  147. apid: String::from("apid"),
  148. page: String::from("page"),
  149. deat: String::from("deat"),
  150. name: String::from("name")
  151. }
  152. }
  153. */
  154.  
  155. pub struct FileFormat {
  156. //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
  157. header: Header,
  158. people: Vec<foo::peeps>,
  159. }
  160. pub struct Header {
  161. //contains everything in the file header
  162. level: i32,
  163. head: String,
  164. char: String,
  165. sour: String,
  166. vers: String,
  167. name: String,
  168. corp: String,
  169. gedc: String,
  170. form: String,
  171. }
  172.  
  173.  
  174. mod foo {
  175.  
  176. #[derive(Serialize, Deserialize)]
  177. pub struct Birt {
  178. pub date: String,
  179. pub plac: String
  180. }
  181. // for Birth
  182. impl Birt {
  183. pub fn default() -> Birt {
  184. Birt {
  185. date: String::from(""),
  186. plac: String::from("")
  187. }
  188. }
  189. }
  190.  
  191. #[derive(Serialize, Deserialize)]
  192. pub struct peeps {
  193. //contains everything in the people section
  194. pub level: i32,
  195. pub birt: Birt,
  196. pub surname: String,
  197. pub deat: Birt,
  198. pub name: String,
  199. pub sex: String,
  200. }
  201.  
  202. impl peeps {
  203. pub fn default() -> peeps {
  204. peeps {
  205. level: 0,
  206. birt: Birt::default(),
  207. surname: String::from(""),
  208. deat: Birt::default(),
  209. name: String::from(""),
  210. sex: String::from("")
  211. }
  212. }
  213. }
  214. }
Add Comment
Please, Sign In to add comment