Skip to main content

oxilean_codegen/python_backend/
pythonlit_traits.rs

1//! # PythonLit - Trait Implementations
2//!
3//! This module contains trait implementations for `PythonLit`.
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::PythonLit;
14use std::fmt;
15
16impl fmt::Display for PythonLit {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            PythonLit::Int(n) => write!(f, "{}", n),
20            PythonLit::Float(n) => {
21                if n.fract() == 0.0 && n.is_finite() {
22                    write!(f, "{:.1}", n)
23                } else {
24                    write!(f, "{}", n)
25                }
26            }
27            PythonLit::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            PythonLit::Bool(b) => write!(f, "{}", if *b { "True" } else { "False" }),
42            PythonLit::None => write!(f, "None"),
43            PythonLit::Bytes(bytes) => {
44                write!(f, "b\"")?;
45                for b in bytes {
46                    write!(f, "\\x{:02x}", b)?;
47                }
48                write!(f, "\"")
49            }
50            PythonLit::Ellipsis => write!(f, "..."),
51        }
52    }
53}