Skip to main content

oxilean_codegen/js_backend/
jslit_traits.rs

1//! # JsLit - Trait Implementations
2//!
3//! This module contains trait implementations for `JsLit`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::lcnf::*;
12
13use super::types::JsLit;
14use std::fmt;
15
16impl fmt::Display for JsLit {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            JsLit::Num(n) => {
20                if n.fract() == 0.0 && n.is_finite() {
21                    write!(f, "{}", *n as i64)
22                } else {
23                    write!(f, "{}", n)
24                }
25            }
26            JsLit::BigInt(n) => write!(f, "{}n", n),
27            JsLit::Str(s) => {
28                write!(f, "\"")?;
29                for c in s.chars() {
30                    match c {
31                        '"' => write!(f, "\\\"")?,
32                        '\\' => write!(f, "\\\\")?,
33                        '\n' => write!(f, "\\n")?,
34                        '\r' => write!(f, "\\r")?,
35                        '\t' => write!(f, "\\t")?,
36                        c => write!(f, "{}", c)?,
37                    }
38                }
39                write!(f, "\"")
40            }
41            JsLit::Bool(b) => write!(f, "{}", b),
42            JsLit::Null => write!(f, "null"),
43            JsLit::Undefined => write!(f, "undefined"),
44        }
45    }
46}