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
|
extern crate regex;
extern crate yaml_rust;
extern crate walkdir;
use walkdir::WalkDir;
use std::io::prelude::*;
use std::fs::File;
use std::env;
use std::path::Path;
use std::collections::HashMap;
mod pre_process;
use pre_process::Section;
mod config;
use config::Config;
mod character;
use character::{Images, Names, Traits, Character};
mod tags;
mod traits;
fn main() {
let raw_files = env::var("RAW_FILES").unwrap_or("characters".into());
let base_path = Path::new(&raw_files);
let cfg = Config::from_file("config.yml", vec!["name", "image", "misc", "tags", "traits"]);
let mut sections: HashMap<String, Section> = HashMap::new();
for (name, sec) in &cfg.sections {
sections.insert(name.clone(), Section::new(&name, &sec.pattern, sec.groups.clone()));
}
for entry in WalkDir::new(base_path).min_depth(1).into_iter().filter_map(|e| e.ok()) {
let mut f = File::open(entry.path()).expect("could not open file");
let mut buf = String::new();
if let Err(_) = f.read_to_string(&mut buf) {
println!("invalid file: {}", entry.path().to_str().unwrap());
continue;
}
let buf = pre_process::strip_irrelevant_content(&buf);
pre_process::split_sections(&buf, &mut sections);
{
let name: &Section = §ions["name".into()];
let image: &Section = §ions["image".into()];
let misc: &Section = §ions["misc".into()];
println!("{:?}", Character {
name: Names {
romaji: name.data["romaji".into()].clone(),
japanese: name.data["japanese".into()].clone(),
aliases: name.data["aliases".into()].split(", ").map(|s| s.to_string()).collect(),
},
image: Images {
thumb: image.data["thumb".into()].clone(),
full: image.data["full".into()].clone(),
},
tags: tags::parse(&(§ions["tags".into()] as &Section).data["tags_raw".into()]),
traits: Traits {
official: traits::parse(&(§ions["traits"] as &Section).data["official_raw".into()]),
indexed: traits::parse(&(§ions["traits"] as &Section).data["indexed_raw".into()]),
},
role: match misc.data["role".into()].len() > 0 {
true => Some(misc.data["role".into()].clone()),
false => None
}
});
}
}
}
|