Skip to main content

oxilean_codegen/haskell_backend/
haskellfunction_traits.rs

1//! # HaskellFunction - Trait Implementations
2//!
3//! This module contains trait implementations for `HaskellFunction`.
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::*;
14use super::types::HaskellFunction;
15use std::fmt;
16
17impl fmt::Display for HaskellFunction {
18    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19        if let Some(ty) = &self.type_annotation {
20            writeln!(f, "{} :: {}", self.name, ty)?;
21        }
22        for eq in &self.equations {
23            write!(f, "{}", self.name)?;
24            for p in &eq.patterns {
25                write!(f, " {}", paren_pattern(p))?;
26            }
27            if !eq.guards.is_empty() {
28                for g in &eq.guards {
29                    write!(f, "\n  | {} = {}", g.condition, g.body)?;
30                }
31            } else if let Some(body) = &eq.body {
32                write!(f, " = {}", body)?;
33            }
34            if !eq.where_clause.is_empty() {
35                write!(f, "\n  where")?;
36                for wf in &eq.where_clause {
37                    let wf_str = wf.to_string();
38                    for line in wf_str.lines() {
39                        write!(f, "\n    {}", line)?;
40                    }
41                }
42            }
43            writeln!(f)?;
44        }
45        Ok(())
46    }
47}