1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.

use std::fmt;
#[derive(Debug)]
pub struct CodegenState<'a> {
    pub tokens: String,
    pub indent_tokens: Vec<&'a str>,
    pub default_newline: &'a str,
    pub default_indent: &'a str,
}

impl<'a> CodegenState<'a> {
    pub fn indent(&mut self, v: &'a str) {
        self.indent_tokens.push(v);
    }
    pub fn dedent(&mut self) {
        self.indent_tokens.pop();
    }
    pub fn add_indent(&mut self) {
        self.tokens.extend(self.indent_tokens.iter().cloned());
    }
    pub fn add_token(&mut self, tok: &'a str) {
        self.tokens.push_str(tok);
    }
}

impl<'a> fmt::Display for CodegenState<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.tokens)
    }
}

pub trait Codegen<'a> {
    fn codegen(&self, state: &mut CodegenState<'a>);
}

impl<'a, T> Codegen<'a> for Option<T>
where
    T: Codegen<'a>,
{
    fn codegen(&self, state: &mut CodegenState<'a>) {
        if let Some(s) = &self {
            s.codegen(state);
        }
    }
}

#[cfg(windows)]
const LINE_ENDING: &str = "\r\n";
#[cfg(not(windows))]
const LINE_ENDING: &str = "\n";

impl<'a> Default for CodegenState<'a> {
    fn default() -> Self {
        Self {
            default_newline: LINE_ENDING,
            default_indent: "    ",
            indent_tokens: Default::default(),
            tokens: Default::default(),
        }
    }
}