mod format;
#[macro_use]
mod parse_macros;
mod parse;
pub use crate::format::FormatError;
pub use crate::parse::ParseError;
use num_bigint as numb;
use num_complex as numc;
use std::fmt;
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
String(String),
Bytes(Vec<u8>),
Integer(numb::BigInt),
Float(f64),
Complex(numc::Complex<f64>),
Tuple(Vec<Value>),
List(Vec<Value>),
Dict(Vec<(Value, Value)>),
Set(Vec<Value>),
Boolean(bool),
None,
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(f, "{}", self.format_ascii().map_err(|_| fmt::Error)?)
}
}
impl Value {
pub fn is_string(&self) -> bool {
matches!(self, Value::String(_))
}
pub fn as_string(&self) -> Option<&String> {
match self {
Value::String(string) => Some(string),
_ => None,
}
}
pub fn is_bytes(&self) -> bool {
matches!(self, Value::Bytes(_))
}
pub fn as_bytes(&self) -> Option<&Vec<u8>> {
match self {
Value::Bytes(bytes) => Some(bytes),
_ => None,
}
}
pub fn is_integer(&self) -> bool {
matches!(self, Value::Integer(_))
}
pub fn as_integer(&self) -> Option<&numb::BigInt> {
match self {
Value::Integer(integer) => Some(integer),
_ => None,
}
}
pub fn is_float(&self) -> bool {
matches!(self, Value::Float(_))
}
pub fn as_float(&self) -> Option<f64> {
match self {
Value::Float(float) => Some(*float),
_ => None,
}
}
pub fn is_complex(&self) -> bool {
matches!(self, Value::Complex(_))
}
pub fn as_complex(&self) -> Option<numc::Complex<f64>> {
match self {
Value::Complex(complex) => Some(*complex),
_ => None,
}
}
pub fn is_tuple(&self) -> bool {
matches!(self, Value::Tuple(_))
}
pub fn as_tuple(&self) -> Option<&Vec<Value>> {
match self {
Value::Tuple(tuple) => Some(tuple),
_ => None,
}
}
pub fn is_list(&self) -> bool {
matches!(self, Value::List(_))
}
pub fn as_list(&self) -> Option<&Vec<Value>> {
match self {
Value::List(list) => Some(list),
_ => None,
}
}
pub fn is_dict(&self) -> bool {
matches!(self, Value::Dict(_))
}
pub fn as_dict(&self) -> Option<&Vec<(Value, Value)>> {
match self {
Value::Dict(dict) => Some(dict),
_ => None,
}
}
pub fn is_set(&self) -> bool {
matches!(self, Value::Set(_))
}
pub fn as_set(&self) -> Option<&Vec<Value>> {
match self {
Value::Set(set) => Some(set),
_ => None,
}
}
pub fn is_boolean(&self) -> bool {
matches!(self, Value::Boolean(_))
}
pub fn as_boolean(&self) -> Option<bool> {
match self {
Value::Boolean(boolean) => Some(*boolean),
_ => None,
}
}
pub fn is_none(&self) -> bool {
matches!(self, Value::None)
}
}