|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::io; |
| 3 | +use std::mem; |
| 4 | + |
| 5 | +use regex::Regex; |
| 6 | +use yaml_rust::YamlLoader; |
| 7 | + |
| 8 | +use error::Error; |
| 9 | +use weekly::Extractor; |
| 10 | + |
| 11 | +enum EntryType { |
| 12 | + Draft, |
| 13 | + Topic, |
| 14 | +} |
| 15 | + |
| 16 | +struct Entry { |
| 17 | + name: String, |
| 18 | + kind: EntryType, |
| 19 | + link: Option<String>, |
| 20 | + description: Option<String>, |
| 21 | + quote: Option<String>, |
| 22 | + cc: Vec<String>, |
| 23 | + // TODO: tag? keyword? |
| 24 | +} |
| 25 | + |
| 26 | +impl Entry { |
| 27 | + fn parse(yaml: &str) -> Option<Entry> { |
| 28 | + YamlLoader::load_from_str(yaml).ok().and_then(|docs| { |
| 29 | + docs.iter().next().and_then(|doc| { |
| 30 | + let name = doc["name"].as_str().map(|s| s.to_string()); |
| 31 | + let kind = match doc["type"].as_str() { |
| 32 | + Some("draft") => Some(EntryType::Draft), |
| 33 | + Some("topic") => Some(EntryType::Topic), |
| 34 | + Some(_) => None, |
| 35 | + None => Some(EntryType::Draft), |
| 36 | + }; |
| 37 | + let link = doc["link"].as_str().map(|s| s.to_string()); |
| 38 | + let description = doc["description"].as_str().map(|s| s.to_string()); |
| 39 | + let quote = doc["quote"].as_str().map(|s| s.to_string()); |
| 40 | + |
| 41 | + let mut cc = Vec::new(); |
| 42 | + if let Some(raw_cc) = doc["cc"].as_vec() { |
| 43 | + for person in raw_cc { |
| 44 | + match person.as_str() { |
| 45 | + Some(c) => cc.push(c.to_string()), |
| 46 | + None => {} |
| 47 | + } |
| 48 | + } |
| 49 | + } else { |
| 50 | + doc["cc"] |
| 51 | + .as_str() |
| 52 | + .map(|s| format!("[{}]", s)) |
| 53 | + .and_then(|s| { |
| 54 | + YamlLoader::load_from_str(&s).ok().and_then(|ds| { |
| 55 | + ds.iter().next().and_then(|d| d.as_vec()).map(|v| { |
| 56 | + for person in v { |
| 57 | + match person.as_str() { |
| 58 | + Some(c) => { |
| 59 | + cc.extend(c.split(' ').map(|s| s.to_string())) |
| 60 | + } |
| 61 | + None => {} |
| 62 | + } |
| 63 | + } |
| 64 | + }) |
| 65 | + }) |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + match (name, kind) { |
| 70 | + (Some(name), Some(kind)) => Some(Entry { |
| 71 | + name: name, |
| 72 | + kind: kind, |
| 73 | + link: link, |
| 74 | + description: description, |
| 75 | + quote: quote, |
| 76 | + cc: cc, |
| 77 | + }), |
| 78 | + _ => None, |
| 79 | + } |
| 80 | + }) |
| 81 | + }) |
| 82 | + } |
| 83 | + |
| 84 | + fn field_append(a: &mut Option<String>, b: &mut Option<String>) { |
| 85 | + match mem::replace(b, None) { |
| 86 | + Some(s2) => { |
| 87 | + if a.is_some() { |
| 88 | + a.as_mut().map(|s1| s1.push_str(&s2)); |
| 89 | + } else { |
| 90 | + mem::replace(a, Some(s2)); |
| 91 | + } |
| 92 | + } |
| 93 | + None => {} |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn merge(&mut self, mut other: Entry) { |
| 98 | + assert_eq!(self.name, other.name); |
| 99 | + self.kind = other.kind; |
| 100 | + Self::field_append(&mut self.link, &mut other.link); // FIXME: not a proper way to merge link |
| 101 | + Self::field_append(&mut self.description, &mut other.description); |
| 102 | + Self::field_append(&mut self.quote, &mut other.quote); |
| 103 | + self.cc.append(&mut other.cc); |
| 104 | + } |
| 105 | + |
| 106 | + fn render(&self, out: &mut io::Write) -> Result<(), Error> { |
| 107 | + write!(out, "- ")?; |
| 108 | + match self.link.as_ref() { |
| 109 | + Some(link) => write!(out, "[{}]({})", self.name, link)?, |
| 110 | + None => write!(out, "{}", self.name)?, |
| 111 | + } |
| 112 | + match self.description.as_ref() { |
| 113 | + Some(desc) => write!(out, ", {}\n", desc)?, |
| 114 | + None => write!(out, "\n")?, |
| 115 | + } |
| 116 | + match self.quote.as_ref() { |
| 117 | + Some(quote) => { |
| 118 | + for line in quote.lines() { |
| 119 | + write!(out, " > {}\n", line)?; |
| 120 | + } |
| 121 | + } |
| 122 | + None => {} |
| 123 | + } |
| 124 | + if self.cc.len() > 0 { |
| 125 | + let cc_list: Vec<_> = self.cc |
| 126 | + .iter() |
| 127 | + .map(|person| format!("[@{}](https://github.com/{})", person, person)) |
| 128 | + .collect(); |
| 129 | + write!(out, "{}\n", cc_list.join(", "))?; |
| 130 | + } |
| 131 | + Ok(()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +pub struct Formal { |
| 136 | + entries: HashMap<String, Entry>, |
| 137 | +} |
| 138 | + |
| 139 | +impl Formal { |
| 140 | + pub fn new() -> Formal { |
| 141 | + Formal { |
| 142 | + entries: HashMap::new(), |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fn parse(&mut self, yaml: &str) { |
| 147 | + let entry = Entry::parse(yaml); |
| 148 | + match entry { |
| 149 | + Some(e) => { |
| 150 | + if let Some(ent) = self.entries.get_mut(&e.name) { |
| 151 | + ent.merge(e); |
| 152 | + return; |
| 153 | + } |
| 154 | + self.entries.insert(e.name.clone(), e); |
| 155 | + } |
| 156 | + None => {} |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +impl Extractor for Formal { |
| 162 | + fn extract(&mut self, comment: &str) -> bool { |
| 163 | + let begin = Regex::new(r"```[:space:]*(yaml|yml)").unwrap(); |
| 164 | + let end = Regex::new(r"```").unwrap(); |
| 165 | + let mut entry = String::new(); |
| 166 | + let mut in_yaml = false; |
| 167 | + let mut res = false; |
| 168 | + for line in comment.lines() { |
| 169 | + if begin.is_match(line) { |
| 170 | + entry = String::new(); |
| 171 | + in_yaml = true; |
| 172 | + } else if end.is_match(line) { |
| 173 | + res = true; |
| 174 | + self.parse(&entry); |
| 175 | + in_yaml = false; |
| 176 | + } else if in_yaml { |
| 177 | + entry.push_str(line); |
| 178 | + entry.push_str("\n"); |
| 179 | + } |
| 180 | + } |
| 181 | + res |
| 182 | + } |
| 183 | + |
| 184 | + fn render(&self, out: &mut io::Write) -> Result<(), Error> { |
| 185 | + for entry in self.entries.values() { |
| 186 | + entry.render(out)?; |
| 187 | + } |
| 188 | + Ok(()) |
| 189 | + } |
| 190 | +} |
0 commit comments