cargo_zng/l10n/
pseudo.rs

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
use std::{borrow::Cow, fmt::Write as _, fs, path::Path};

use fluent_syntax::ast::*;

use crate::util;

pub fn pseudo(dir: &str, check: bool, verbose: bool) {
    fluent_pseudo_impl(dir, "pseudo", false, false, check, verbose)
}

pub fn pseudo_mirr(dir: &str, check: bool, verbose: bool) {
    fluent_pseudo_impl(dir, "pseudo-mirr", true, false, check, verbose)
}

pub fn pseudo_wide(dir: &str, check: bool, verbose: bool) {
    fluent_pseudo_impl(dir, "pseudo-wide", false, true, check, verbose)
}

fn fluent_pseudo_impl(dir: &str, to_name: &str, flipped: bool, elongate: bool, check: bool, verbose: bool) {
    pseudo_impl(dir, to_name, &|s| fluent_pseudo::transform(s, flipped, elongate), check, verbose)
}

fn pseudo_impl(dir: &str, to_name: &str, transform: &impl Fn(&str) -> Cow<str>, check: bool, verbose: bool) {
    let dir = Path::new(dir);
    let to_dir = dir.with_file_name(to_name);

    for entry in fs::read_dir(dir).unwrap_or_else(|e| fatal!("cannot read `{}`, {e}", dir.display())) {
        let entry = entry.unwrap_or_else(|e| fatal!("cannot read `{}` entry, {e}", dir.display()));
        let path = entry.path();
        if path.is_file() && path.extension().map(|e| e == "ftl").unwrap_or(false) {
            let _ = util::check_or_create_dir(check, &to_dir);
            generate(&path, &to_dir.join(path.file_name().unwrap()), transform, check, verbose);
        }
    }

    let unnamed = dir.with_extension("ftl");
    if unnamed.exists() {
        generate(&unnamed, &to_dir.with_extension("ftl"), transform, check, verbose);
    }
}

fn generate(from: &Path, to: &Path, transform: &impl Fn(&str) -> Cow<str>, check: bool, verbose: bool) {
    let source = match fs::read_to_string(from) {
        Ok(s) => s,
        Err(e) => {
            error!("cannot read `{}`, {e}", from.display());
            return;
        }
    };

    let source = match fluent_syntax::parser::parse(source) {
        Ok(s) => s,
        Err((s, e)) => {
            error!(
                "cannot parse `{}`\n{}",
                from.display(),
                e.into_iter().map(|e| format!("    {e}")).collect::<Vec<_>>().join("\n")
            );
            s
        }
    };

    let mut output = "# Test locale, generated by cargo zng l10n".to_owned();

    for entry in source.body {
        match entry {
            Entry::Message(m) => write_entry(&mut output, &m.id, m.value.as_ref(), &m.attributes, transform),
            Entry::Term(t) => write_entry(&mut output, &t.id, Some(&t.value), &t.attributes, transform),
            Entry::Comment(_) | Entry::GroupComment(_) | Entry::ResourceComment(_) | Entry::Junk { .. } => {}
        }
    }

    if let Err(e) = util::check_or_write(check, to, output.as_bytes(), verbose) {
        error!("cannot write `{}`, {e}", to.display());
    } else {
        println!("  generated {}", to.display());
    }
}

fn write_entry(
    output: &mut String,
    id: &Identifier<String>,
    value: Option<&Pattern<String>>,
    attributes: &[Attribute<String>],
    transform: &impl Fn(&str) -> Cow<str>,
) {
    write!(output, "\n\n{} = ", id.name).unwrap();
    if let Some(value) = value {
        write_pattern(output, value, transform);
    }
    for attr in attributes {
        write!(output, "\n    .{} = ", attr.id.name).unwrap();
        write_pattern(output, &attr.value, transform);
    }
}

fn write_pattern(output: &mut String, pattern: &Pattern<String>, transform: &impl Fn(&str) -> Cow<str>) {
    for el in &pattern.elements {
        match el {
            PatternElement::TextElement { value } => {
                let mut prefix = "";
                for line in value.lines() {
                    write!(output, "{prefix}{}", transform(line)).unwrap();
                    prefix = "    ";
                }
            }
            PatternElement::Placeable { expression } => write_expression(output, expression, transform),
        }
    }
}

fn write_expression(output: &mut String, expr: &Expression<String>, transform: &impl Fn(&str) -> Cow<str>) {
    match expr {
        Expression::Select { selector, variants } => {
            write!(output, "{{").unwrap();
            write_inline_expression(output, selector, transform);
            writeln!(output, " ->").unwrap();

            for v in variants {
                write!(output, "    ").unwrap();
                if v.default {
                    write!(output, "*").unwrap();
                }
                let key = match &v.key {
                    VariantKey::Identifier { name } => name,
                    VariantKey::NumberLiteral { value } => value,
                };
                write!(output, "[{key}] ").unwrap();

                write_pattern(output, &v.value, transform);
            }

            writeln!(output, "}}").unwrap();
        }
        Expression::Inline(e) => write_inline_expression(output, e, transform),
    }
}
fn write_inline_expression(output: &mut String, expr: &InlineExpression<String>, transform: &impl Fn(&str) -> Cow<str>) {
    match expr {
        InlineExpression::StringLiteral { value } => write!(output, "{{ \"{value}\" }}").unwrap(),
        InlineExpression::NumberLiteral { value } => write!(output, "{{ {value} }}").unwrap(),
        InlineExpression::FunctionReference { id, arguments } => {
            write!(output, "{{ {}", id.name).unwrap();
            write_arguments(output, arguments, transform);
            write!(output, " }}").unwrap()
        }
        InlineExpression::MessageReference { id, attribute } => {
            write!(output, "{{ {}", id.name).unwrap();
            if let Some(a) = attribute {
                write!(output, ".{}", a.name).unwrap();
            }
            write!(output, " }}").unwrap()
        }
        InlineExpression::TermReference { id, attribute, arguments } => {
            write!(output, "{{ -{}", id.name).unwrap();
            if let Some(a) = attribute {
                write!(output, ".{}", a.name).unwrap();
            }
            if let Some(args) = arguments {
                write_arguments(output, args, transform);
            }
            write!(output, " }}").unwrap()
        }
        InlineExpression::VariableReference { id } => write!(output, "{{ ${} }}", id.name).unwrap(),
        InlineExpression::Placeable { expression } => {
            write!(output, "{{ ").unwrap();
            write_expression(output, expression, transform);
            write!(output, " }}").unwrap();
        }
    }
}

fn write_arguments(output: &mut String, arguments: &CallArguments<String>, transform: &impl Fn(&str) -> Cow<str>) {
    write!(output, "(").unwrap();
    let mut sep = "";
    for a in &arguments.positional {
        write!(output, "{sep}").unwrap();
        write_inline_expression(output, a, transform);
        sep = ", ";
    }
    for a in &arguments.named {
        write!(output, "{sep}{}", a.name.name).unwrap();
        write_inline_expression(output, &a.value, transform);
        sep = ", ";
    }
    write!(output, ")").unwrap();
}