use super::tags; use super::tags::Tag; use super::dl_list; use super::dl_list::DLListItem; use super::section; use super::section::Section; use super::regex::Regex; use super::tiles; use std::collections::HashMap; #[derive(Debug, Serialize)] pub struct Names { pub romaji: String, pub japanese: String, pub aliases: Vec, } #[derive(Debug, Serialize)] pub struct Images { pub thumb: String, pub full: String, } #[derive(Debug, Serialize)] pub struct Traits { pub official: Vec, pub indexed: Vec, } #[derive(Debug, Serialize)] pub struct Character { pub name: Names, pub image: Images, pub tags: Vec, pub traits: Traits, pub assignments: Vec, pub extra: Vec, pub role: Option, } impl Names { pub fn new() -> Self { Names { romaji: String::new(), japanese: String::new(), aliases: vec![], } } } impl Images { pub fn new() -> Self { Images { thumb: String::new(), full: String::new(), } } } impl Traits { pub fn new() -> Self { Traits { official: vec![], indexed: vec![], } } } impl Character { pub fn new() -> Self { Character { name: Names::new(), image: Images::new(), tags: vec![], traits: Traits::new(), assignments: vec![], extra: vec![], role: None, } } pub fn parse(&mut self, buf: &str) { let re_extras = Regex::new(r#"(?is)Extra Details \| [0-9]+.*?
(.*?)
"#) .unwrap(); let mut sections = get_sections(); section::process(&buf, &mut sections); let caps = re_extras.captures(&buf); if caps.is_some() { self.extra = dl_list::parse(caps.unwrap().at(1).unwrap()); } { let name: &Section = §ions["name".into()]; let image: &Section = §ions["image".into()]; let misc: &Section = §ions["misc".into()]; let tags: &Section = §ions["tags".into()]; self.name.romaji = name.data["romaji".into()].clone(); self.name.japanese = name.data["japanese".into()].clone(); if name.data["aliases".into()].len() > 0 { self.name.aliases = name.data["aliases".into()] .split(", ") .map(|s| s.to_string()) .collect(); } self.image.thumb = image.data["thumb".into()].clone(); self.image.full = image.data["full".into()].clone(); if tags.data.contains_key("tags_raw".into()) { self.tags = tags::parse(&tags.data["tags_raw".into()]); } self.traits.official = dl_list::parse(&(§ions["traits"] as &Section).data["official_raw".into()]); self.traits.indexed = dl_list::parse(&(§ions["traits"] as &Section).data["indexed_raw".into()]); self.assignments = tiles::parse_tile_link_ids(&(§ions["assignments"] as &Section).data["raw".into()], "series"); if misc.data["role".into()].len() > 0 { self.role = Some(misc.data["role".into()].clone()); } } } } fn get_sections() -> HashMap { let mut s: HashMap = HashMap::new(); s.insert("name".into(), Section::new("name", r#"(?is)Romaji Name.*?(.*?)\s?.*?Japanese Name.*?(.*?)\s?.*?Aliases.*?(.*?)\s?"#, vec!["romaji", "japanese", "aliases"])); s.insert("misc".into(), Section::new("misc", r#"(?is)Role.*?(.*?)\s?"#, vec!["role"])); s.insert("image".into(), Section::new("image", r#"(?is)

.*.*?

View Full Size Image"#, vec!["thumb", "full"])); s.insert("tags".into(), Section::new("tags", r#"(?is)tagged as

.*?(.*?)"#, vec!["tags_raw"])); s.insert("traits".into(), Section::new("traits", r#"(?is)indexed traits

.*?
(.*?)
.*?official traits\s?

.*?
(.*?)
"#, vec!["indexed_raw", "official_raw"])); s.insert("assignments".into(), Section::new("assignments", r#"(?is)appears in the following

(.*?)"#, vec!["raw"])); s.insert("chars_similar_traits".into(), Section::new("assignments", r#"(?is)with Similar Traits

(.*?)"#, vec!["raw"])); s }