Skip to main content

oxilean_codegen/scala_backend/
scalalit_traits.rs

1//! # ScalaLit - Trait Implementations
2//!
3//! This module contains trait implementations for `ScalaLit`.
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::ScalaLit;
14use std::fmt;
15
16impl fmt::Display for ScalaLit {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            ScalaLit::Int(n) => write!(f, "{}", n),
20            ScalaLit::Long(n) => write!(f, "{}L", n),
21            ScalaLit::Double(v) => {
22                if v.fract() == 0.0 && v.is_finite() {
23                    write!(f, "{}.0", *v as i64)
24                } else {
25                    write!(f, "{}", v)
26                }
27            }
28            ScalaLit::Float(v) => write!(f, "{}f", v),
29            ScalaLit::Bool(b) => write!(f, "{}", b),
30            ScalaLit::Char(c) => write!(f, "'{}'", c),
31            ScalaLit::Null => write!(f, "null"),
32            ScalaLit::Unit => write!(f, "()"),
33            ScalaLit::Str(s) => {
34                write!(f, "\"")?;
35                for c in s.chars() {
36                    match c {
37                        '"' => write!(f, "\\\"")?,
38                        '\\' => write!(f, "\\\\")?,
39                        '\n' => write!(f, "\\n")?,
40                        '\r' => write!(f, "\\r")?,
41                        '\t' => write!(f, "\\t")?,
42                        c => write!(f, "{}", c)?,
43                    }
44                }
45                write!(f, "\"")
46            }
47        }
48    }
49}