use num_bigint::BigInt;
use num_rational::BigRational;
use num_traits::{pow, FromPrimitive};
use std::fs::File;
use std::io;
use std::io::Read;
pub fn get_escape_char(ch: char) -> Option<char> {
match ch {
'\\' => Some('\\'),
'"' => Some('"'),
'\'' => Some('\''),
'$' => Some('$'),
'n' => Some('\n'),
'r' => Some('\r'),
't' => Some('\t'),
_ => None,
}
}
pub fn is_value_end_char(ch: char) -> bool {
is_whitespace(ch) || is_end_delimiter(ch) || is_operator(ch)
}
pub fn is_whitespace(ch: char) -> bool {
ch.is_whitespace() || ch == '#'
}
pub fn is_end_delimiter(ch: char) -> bool {
match ch {
')' | ']' | '}' | '>' => true,
_ => false,
}
}
pub fn is_numeric_char(ch: char) -> bool {
match ch {
_ch if is_digit(_ch) => true,
'.' | ',' => true,
_ => false,
}
}
pub fn is_priority_operator(ch: char) -> bool {
match ch {
'*' | '/' | '%' => true,
_ => false,
}
}
pub fn is_operator(ch: char) -> bool {
match ch {
'+' | '-' | '*' | '/' | '%' => true,
_ => false,
}
}
pub fn is_digit(ch: char) -> bool {
match ch {
'0'...'9' => true,
_ => false,
}
}
pub fn is_reserved(field: &str) -> bool {
match field {
"@" | "null" | "true" | "false" | "Obj" | "Str" | "Arr" | "Tup" => true,
_ => false,
}
}
pub fn frac_from_whole_and_dec(whole: BigInt, decimal: BigInt, dec_len: usize) -> BigRational {
let denom = pow(BigInt::from_u8(10).unwrap(), dec_len);
BigRational::new(whole, 1.into()) + BigRational::new(decimal, denom)
}
pub fn read_file_str(fname: &str) -> io::Result<String> {
let mut file = File::open(fname)?;
let mut contents = String::new();
let _ = file.read_to_string(&mut contents)?;
Ok(contents)
}