Skip to main content

oxilean_codegen/ruby_backend/
rubypattern_traits.rs

1//! # RubyPattern - Trait Implementations
2//!
3//! This module contains trait implementations for `RubyPattern`.
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::RubyPattern;
15use std::fmt;
16
17impl std::fmt::Display for RubyPattern {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        match self {
20            RubyPattern::Pin(v) => write!(f, "^{}", v),
21            RubyPattern::Variable(v) => write!(f, "{}", v),
22            RubyPattern::Literal(l) => write!(f, "{}", l),
23            RubyPattern::Array(pats) => {
24                let ps: Vec<String> = pats.iter().map(|p| p.to_string()).collect();
25                write!(f, "[{}]", ps.join(", "))
26            }
27            RubyPattern::Hash(fields) => {
28                let fs: Vec<String> = fields
29                    .iter()
30                    .map(|(k, v)| {
31                        if let Some(p) = v {
32                            format!("{}: {}", k, p)
33                        } else {
34                            format!("{}:", k)
35                        }
36                    })
37                    .collect();
38                write!(f, "{{{}}}", fs.join(", "))
39            }
40            RubyPattern::Guard(p, cond) => write!(f, "{} if {}", p, cond),
41            _ => write!(f, "_"),
42        }
43    }
44}