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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use super::*;

use fluent_syntax::ast::{
    Attribute, Comment, Entry, Expression, InlineExpression, Message, NamedArgument, Pattern, PatternElement, Resource, Term, VariantKey,
};

pub trait PrettyPrint {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result;
}

impl PrettyPrint for Resource<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        for i in &self.body {
            PrettyPrint::pretty_print(i, f)?
        }
        Ok(())
    }
}

impl PrettyPrint for Entry<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        match self {
            Entry::Message(s) => PrettyPrint::pretty_print(s, f),
            Entry::Term(s) => PrettyPrint::pretty_print(s, f),
            Entry::Comment(s) => f.write_new_indent(s, "# "),
            Entry::GroupComment(s) => PrettyPrint::pretty_print(s, f),
            Entry::ResourceComment(s) => {
                f.write_new_indent(s, "### ")?;
                Ok(f.write_new_line())
            }
            Entry::Junk { .. } => {
                unimplemented!()
            }
        }
    }
}

impl PrettyPrint for Message<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        // self.comment
        if let Some(comment) = &self.comment {
            f.write_new_indent(comment, "# ")?
        }
        // self.id
        write!(f, "{} = ", self.id.name)?;
        // self.value
        let mut one_line = true;
        if let Some(s) = &self.value {
            one_line = text_is_one_line(s);
            match one_line {
                true => PrettyPrint::pretty_print(s, f)?,
                false => {
                    f.write_new_line();
                    f.write_new_indent(s, &f.tab())?;
                }
            }
        }
        // self.attributes
        let mut indent = f.indent.to_owned() + &f.tab();
        f.swap_indent(&mut indent);
        for item in &self.attributes {
            f.write_new_line();
            item.pretty_print(f)?;
        }
        f.swap_indent(&mut indent);
        // end
        Ok(())
    }
}

impl PrettyPrint for Term<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        write!(f, "{:#?}", self)
    }
}

impl PrettyPrint for Pattern<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        for item in &self.elements {
            match item {
                PatternElement::TextElement { value } => f.write_str(value)?,
                PatternElement::Placeable { expression } => PrettyPrint::pretty_print(expression, f)?,
            }
        }
        Ok(())
    }
}

fn text_is_one_line(text: &Pattern<&str>) -> bool {
    for i in &text.elements {
        match i {
            PatternElement::TextElement { value } => {
                if value.contains('\n') {
                    return false;
                }
            }
            PatternElement::Placeable { expression: Expression::Select { .. } } => return false,
            _ => continue,
        }
    }
    return true;
}

impl PrettyPrint for InlineExpression<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        match self {
            InlineExpression::StringLiteral { .. } => {
                unimplemented!()
            }
            InlineExpression::NumberLiteral { value } => {
                write!(f, "{}", value)
            }
            InlineExpression::FunctionReference { id, arguments } => {
                write!(f, "{}", id.name)?;
                write!(f, "(")?;
                let length = arguments.positional.len() + arguments.named.len();
                let mut this = 0;

                for item in &arguments.positional {
                    this += 1;

                    PrettyPrint::pretty_print(item, f)?;
                    if this != length {
                        write!(f, ", ")?
                    }
                }
                for NamedArgument { name, value } in &arguments.named {
                    this += 1;
                    write!(f, "{} = ", name.name)?;
                    PrettyPrint::pretty_print(value, f)?;
                    if this != length {
                        write!(f, ", ")?
                    }
                }
                write!(f, ")")
            }
            InlineExpression::MessageReference { .. } => {
                unimplemented!()
            }
            InlineExpression::TermReference { id, attribute, arguments } => {
                f.write_str(id.name)?;
                match attribute {
                    Some(s) => writeln!(f, "{:#?}", s)?,
                    None => {}
                }
                match arguments {
                    Some(s) => writeln!(f, "{:#?}", s)?,
                    None => {}
                }
                Ok(())
            }
            InlineExpression::VariableReference { id } => {
                write!(f, "${}", id.name)
            }
            InlineExpression::Placeable { .. } => {
                unimplemented!()
            }
        }
    }
}

impl PrettyPrint for Expression<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        match self {
            Expression::Select { selector, variants } => {
                write!(f, "{{")?;
                f.write_new_line();
                f.write_with_indent("");
                PrettyPrint::pretty_print(selector, f)?;
                write!(f, " -> ")?;
                f.write_new_line();
                for item in variants {
                    match item.default {
                        true => f.write_with_indent("   *"),
                        false => f.write_with_indent("    "),
                    }
                    write!(f, "[")?;
                    match item.key {
                        VariantKey::Identifier { name } => f.write_str(name)?,
                        VariantKey::NumberLiteral { value } => f.write_str(value)?,
                    }
                    write!(f, "] ")?;
                    PrettyPrint::pretty_print(&item.value, f)?;
                    f.write_new_line()
                }
                f.write_with_indent("}");
            }
            Expression::Inline(s) => {
                write!(f, "{{")?;
                write!(f, "{}", " ".repeat(f.config.inline_space as usize))?;
                PrettyPrint::pretty_print(s, f)?;
                write!(f, "{}", " ".repeat(f.config.inline_space as usize))?;
                write!(f, "}}")?;
            }
        }
        Ok(())
    }
}

impl PrettyPrint for Attribute<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        f.write_with_indent(".");
        write!(f, "{} = ", self.id.name)?;
        self.value.pretty_print(f)
    }
}

impl PrettyPrint for Comment<&str> {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        for i in &self.content {
            f.write_with_indent(i);
            f.write_new_line()
        }
        Ok(())
    }
}

impl PrettyPrint for &str {
    fn pretty_print(&self, f: &mut FluentFormatter) -> fmt::Result {
        Ok(f.buffer.push_str(self))
    }
}