#![allow(dead_code)]
use std::path::{Path, PathBuf};
use tabnas::Tabnas;
use tabnas_jsonc::{make_with, JsoncOptions};
use tabnas_support::{find_spec_dir, Failure, Value};
pub fn repo_root() -> &'static Path {
Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("rs/ has a parent")
}
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: &tabnas::Value) -> Value {
if value.is_undefined() {
Value::Undefined
} else {
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 unescape_jsonc(cell: &str) -> String {
if !cell.contains('\\') {
return cell.to_string();
}
let mut out = String::with_capacity(cell.len());
let mut chars = cell.chars().peekable();
while let Some(c) = chars.next() {
if c == '\\' {
let decoded = match chars.peek() {
Some('n') => Some('\n'),
Some('r') => Some('\r'),
Some('t') => Some('\t'),
Some('0') => Some('\0'),
Some('\\') => Some('\\'),
_ => None,
};
if let Some(decoded) = decoded {
chars.next();
out.push(decoded);
continue;
}
}
out.push(c);
}
out
}
pub fn options_from_cell(cell: &str) -> Result<JsoncOptions, String> {
if cell.trim().is_empty() {
return Ok(JsoncOptions::default());
}
let bag: serde_json::Value = serde_json::from_str(cell)
.map_err(|error| format!("opts cell is not JSON: {error}: {cell}"))?;
Ok(JsoncOptions::from_value(&tabnas::Value::from_json(&bag)))
}
pub fn parser_for(options: JsoncOptions) -> Tabnas {
make_with(options)
}