use boa_interner::{Interner, Sym, ToInternedString};
use num_bigint::BigInt;
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub enum Const {
String(Sym),
Num(f64),
Int(i32),
BigInt(Box<BigInt>),
Bool(bool),
Null,
Undefined,
}
impl From<Sym> for Const {
fn from(string: Sym) -> Self {
Self::String(string)
}
}
impl From<f64> for Const {
fn from(num: f64) -> Self {
Self::Num(num)
}
}
impl From<i32> for Const {
fn from(i: i32) -> Self {
Self::Int(i)
}
}
impl From<BigInt> for Const {
fn from(i: BigInt) -> Self {
Self::BigInt(Box::new(i))
}
}
impl From<Box<BigInt>> for Const {
fn from(i: Box<BigInt>) -> Self {
Self::BigInt(i)
}
}
impl From<bool> for Const {
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl ToInternedString for Const {
fn to_interned_string(&self, interner: &Interner) -> String {
match *self {
Self::String(st) => {
format!("\"{}\"", interner.resolve_expect(st))
}
Self::Num(num) => num.to_string(),
Self::Int(num) => num.to_string(),
Self::BigInt(ref num) => num.to_string(),
Self::Bool(v) => v.to_string(),
Self::Null => "null".to_owned(),
Self::Undefined => "undefined".to_owned(),
}
}
}