#![allow(dead_code)]
use std::path::{Path, PathBuf};
use tabnas::{Tabnas, Value};
use tabnas_support::{find_spec_dir, Failure};
pub fn repo_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
.to_path_buf()
}
pub fn spec_dir() -> PathBuf {
find_spec_dir(Some(Path::new(env!("CARGO_MANIFEST_DIR"))))
.expect("a test/spec directory above rs/")
}
pub fn to_value(value: &Value) -> tabnas_support::Value {
tabnas_support::Value::from(value.to_json())
}
pub fn to_failure(error: tabnas::TabnasError) -> Failure {
Failure::new(error.code.clone())
.at(error.row, error.col)
.with_message(error.to_string())
}
pub fn default_parser() -> Tabnas {
tabnas_xml::make()
}
pub fn strip_ansi(text: &str) -> String {
let mut out = String::with_capacity(text.len());
let mut chars = text.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\u{1b}' && chars.peek() == Some(&'[') {
chars.next();
for next in chars.by_ref() {
if next == 'm' {
break;
}
}
continue;
}
out.push(ch);
}
out
}
pub fn spec_unescape(s: &str) -> String {
if !s.contains('\\') {
return s.to_string();
}
let chars: Vec<char> = s.chars().collect();
let mut out = String::with_capacity(s.len());
let mut i = 0;
while i < chars.len() {
let c = chars[i];
if c == '\\' && i + 1 < chars.len() {
match chars[i + 1] {
'n' => {
out.push('\n');
i += 2;
continue;
}
'r' => {
out.push('\r');
i += 2;
continue;
}
't' => {
out.push('\t');
i += 2;
continue;
}
'\\' => {
out.push('\\');
i += 2;
continue;
}
'u' if i + 5 < chars.len() => {
let hex: String = chars[i + 2..i + 6].iter().collect();
if hex.chars().all(|h| h.is_ascii_hexdigit()) {
if let Some(decoded) =
u32::from_str_radix(&hex, 16).ok().and_then(char::from_u32)
{
out.push(decoded);
i += 6;
continue;
}
}
}
_ => {}
}
}
out.push(c);
i += 1;
}
out
}
pub fn json(value: &Value) -> String {
value.to_json().to_string()
}