use std::collections::BTreeMap;
use std::str;
use webidl::{self, ast};
pub fn multimap_insert<K: Ord, V: PartialEq>(m: &mut BTreeMap<K, Vec<V>>, key: K, value: V) {
let v = m.entry(key).or_insert_with(Vec::new);
if !v.contains(&value) {
v.push(value);
}
}
pub fn multimap_append<K: Ord + Clone, V: PartialEq>(
m: &mut BTreeMap<K, Vec<V>>,
other: BTreeMap<K, Vec<V>>,
) {
for (k, v) in other {
for value in v {
multimap_insert(m, k.clone(), value);
}
}
}
pub fn convert_html_to_doc_comment(html: &str) -> String {
use html2runes;
use regex::RegexBuilder;
let doc_comment_regex = RegexBuilder::new("^").multi_line(true).build().unwrap();
let md = html2runes::markdown::convert_string(html);
let mut doc = doc_comment_regex
.replace_all(md.trim_right(), "/// ")
.into();
doc += "\n";
doc
}
pub fn unreserve<S: Into<String>>(name: S) -> String {
const RESERVED_WORDS: &'static [&'static str] = &[
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn",
"for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref",
"return", "Self", "self", "static", "struct", "super", "trait", "true", "type", "unsafe",
"use", "where", "while", "abstract", "alignof", "become", "box", "do", "final", "macro",
"offsetof", "override", "priv", "proc", "pure", "sizeof", "typeof", "unsized", "virtual",
"yield",
];
let mut s = name.into();
if RESERVED_WORDS.contains(&&*s) {
s += "_";
}
s
}
pub fn snake(name: &str) -> String {
use heck::SnakeCase;
name.to_snake_case()
}
pub fn shouty_snake(name: &str) -> String {
use heck::ShoutySnakeCase;
name.to_shouty_snake_case()
}
pub fn camel(name: &str) -> String {
use heck::CamelCase;
name.to_camel_case()
}
pub fn parse_defs(src: &[u8]) -> Vec<ast::Definition> {
let src = str::from_utf8(src).expect("IDL contained invalid UTF-8");
webidl::parse_string(src).expect("Failed to parse IDL")
}