Skip to main content

oxilean_codegen/ruby_backend/
rubylit_traits.rs

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