use crate::builtins::bigint::BigInt;
use gc::{Finalize, Trace};
use std::fmt::{Display, Formatter, Result};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Trace, Finalize, PartialEq)]
pub enum Const {
String(Box<str>),
Num(f64),
Int(i32),
BigInt(BigInt),
Bool(bool),
Null,
Undefined,
}
impl From<&str> for Const {
fn from(s: &str) -> Self {
Self::String(s.to_owned().into_boxed_str())
}
}
impl From<&String> for Const {
fn from(s: &String) -> Self {
Self::String(s.clone().into_boxed_str())
}
}
impl From<Box<str>> for Const {
fn from(s: Box<str>) -> Self {
Self::String(s)
}
}
impl From<String> for Const {
fn from(s: String) -> Self {
Self::String(s.into_boxed_str())
}
}
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(i)
}
}
impl From<bool> for Const {
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl Display for Const {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
match *self {
Self::String(ref st) => write!(f, "\"{}\"", st),
Self::Num(num) => write!(f, "{}", num),
Self::Int(num) => write!(f, "{}", num),
Self::BigInt(ref num) => write!(f, "{}", num),
Self::Bool(v) => write!(f, "{}", v),
Self::Null => write!(f, "null"),
Self::Undefined => write!(f, "undefined"),
}
}
}