use phf::phf_map;
pub static TYPST_SHORTHANDS: phf::Map<&'static str, &'static str> = phf_map! {
"arrow.l.r.double.long" => "<==>",
"arrow.l.r.long" => "<-->",
"arrow.r.double.long" => "==>",
"arrow.r.long" => "-->",
"arrow.r.long.squiggly" => "~~>",
"arrow.l.double.long" => "<==",
"arrow.l.long" => "<--",
"arrow.l.long.squiggly" => "<~~",
"arrow.r.bar" => "|->",
"arrow.r.double.bar" => "|=>",
"arrow.r.tail" => ">->",
"arrow.r.twohead" => "->>",
"arrow.l.tail" => "<-<",
"arrow.l.twohead" => "<<-",
"arrow.l.r.double" => "<=>",
"arrow.l.r" => "<->",
"arrow.r" => "->",
"arrow.r.double" => "=>",
"arrow.r.squiggly" => "~>",
"arrow.l" => "<-",
"arrow.l.squiggly" => "<~",
"gt.double" => ">>",
"gt.eq" => ">=",
"gt.triple" => ">>>",
"lt.double" => "<<",
"lt.eq" => "<=",
"lt.triple" => "<<<",
"eq.not" => "!=",
"bar.v.double" => "||",
"bracket.l.stroked" => "[|",
"bracket.r.stroked" => "|]",
"colon.eq" => ":=",
"eq.colon" => "=:",
"colon.double.eq" => "::=",
"dots.h" => "...",
"ast.op" => "*",
"minus" => "-",
"tilde.op" => "~",
};
pub static SHORTHAND_TO_TYPST: phf::Map<&'static str, &'static str> = phf_map! {
"<==>" => "arrow.l.r.double.long",
"<-->" => "arrow.l.r.long",
"==>" => "arrow.r.double.long",
"-->" => "arrow.r.long",
"~~>" => "arrow.r.long.squiggly",
"<==" => "arrow.l.double.long",
"<--" => "arrow.l.long",
"<~~" => "arrow.l.long.squiggly",
"|->" => "arrow.r.bar",
"|=>" => "arrow.r.double.bar",
">->" => "arrow.r.tail",
"->>" => "arrow.r.twohead",
"<-<" => "arrow.l.tail",
"<<-" => "arrow.l.twohead",
"<=>" => "arrow.l.r.double",
"<->" => "arrow.l.r",
"->" => "arrow.r",
"=>" => "arrow.r.double",
"~>" => "arrow.r.squiggly",
"<-" => "arrow.l",
"<~" => "arrow.l.squiggly",
">>" => "gt.double",
">=" => "gt.eq",
">>>" => "gt.triple",
"<<" => "lt.double",
"<=" => "lt.eq",
"<<<" => "lt.triple",
"!=" => "eq.not",
"||" => "bar.v.double",
"[|" => "bracket.l.stroked",
"|]" => "bracket.r.stroked",
":=" => "colon.eq",
"=:" => "eq.colon",
"::=" => "colon.double.eq",
"..." => "dots.h",
};
#[inline]
pub fn has_shorthand(symbol: &str) -> bool {
TYPST_SHORTHANDS.contains_key(symbol)
}
#[inline]
pub fn get_shorthand(symbol: &str) -> Option<&'static str> {
TYPST_SHORTHANDS.get(symbol).copied()
}
#[inline]
pub fn apply_shorthand(symbol: &str, prefer_shorthands: bool) -> &str {
if prefer_shorthands {
TYPST_SHORTHANDS.get(symbol).copied().unwrap_or(symbol)
} else {
symbol
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_arrow_shorthands() {
assert_eq!(get_shorthand("arrow.r"), Some("->"));
assert_eq!(get_shorthand("arrow.l.r.double"), Some("<=>"));
assert_eq!(get_shorthand("arrow.r.long"), Some("-->"));
}
#[test]
fn test_comparison_shorthands() {
assert_eq!(get_shorthand("gt.eq"), Some(">="));
assert_eq!(get_shorthand("lt.eq"), Some("<="));
assert_eq!(get_shorthand("eq.not"), Some("!="));
}
#[test]
fn test_apply_shorthand() {
assert_eq!(apply_shorthand("arrow.r", true), "->");
assert_eq!(apply_shorthand("arrow.r", false), "arrow.r");
assert_eq!(apply_shorthand("alpha", true), "alpha"); }
#[test]
fn test_reverse_mapping() {
assert_eq!(SHORTHAND_TO_TYPST.get("->"), Some(&"arrow.r"));
assert_eq!(SHORTHAND_TO_TYPST.get("<=>"), Some(&"arrow.l.r.double"));
}
}