icu_plurals/provider/rules/reference/
serializer.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
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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use crate::provider::rules::reference::ast;
use core::fmt;
use core::ops::RangeInclusive;

/// Unicode Plural Rule serializer converts an [`AST`] to a [`String`].
///
/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
///
/// # Examples
///
/// ```
/// use icu::plurals::provider::rules::reference::ast;
/// use icu::plurals::provider::rules::reference::parse;
/// use icu::plurals::provider::rules::reference::serialize;
///
/// let input = "i = 0 or n = 1 @integer 0, 1 @decimal 0.0~1.0, 0.00~0.04";
///
/// let ast = parse(input.as_bytes()).expect("Parsing failed.");
///
/// assert_eq!(ast.condition.0[0].0[0].expression.operand, ast::Operand::I);
/// assert_eq!(ast.condition.0[1].0[0].expression.operand, ast::Operand::N);
///
/// let mut result = String::new();
/// serialize(&ast, &mut result).expect("Serialization failed.");
///
/// assert_eq!(input, result);
/// ```
///
/// [`AST`]: crate::provider::rules::reference::ast
pub fn serialize(rule: &ast::Rule, w: &mut impl fmt::Write) -> fmt::Result {
    serialize_condition(&rule.condition, w)?;
    if let Some(samples) = &rule.samples {
        serialize_samples(samples, w)?;
    }
    Ok(())
}

/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub fn serialize_condition(cond: &ast::Condition, w: &mut impl fmt::Write) -> fmt::Result {
    let mut first = true;

    for cond in cond.0.iter() {
        if first {
            first = false;
        } else {
            w.write_str(" or ")?;
        }
        serialize_andcondition(cond, w)?;
    }
    Ok(())
}

fn serialize_andcondition(cond: &ast::AndCondition, w: &mut impl fmt::Write) -> fmt::Result {
    let mut first = true;

    for relation in cond.0.iter() {
        if first {
            first = false;
        } else {
            w.write_str(" and ")?;
        }
        serialize_relation(relation, w)?;
    }
    Ok(())
}

fn serialize_relation(relation: &ast::Relation, w: &mut impl fmt::Write) -> fmt::Result {
    serialize_expression(&relation.expression, w)?;
    w.write_char(' ')?;
    serialize_operator(relation.operator, w)?;
    w.write_char(' ')?;
    serialize_rangelist(&relation.range_list, w)
}

fn serialize_expression(exp: &ast::Expression, w: &mut impl fmt::Write) -> fmt::Result {
    serialize_operand(exp.operand, w)?;
    if let Some(modulus) = &exp.modulus {
        w.write_str(" % ")?;
        serialize_value(modulus, w)?;
    }
    Ok(())
}

fn serialize_operator(operator: ast::Operator, w: &mut impl fmt::Write) -> fmt::Result {
    match operator {
        ast::Operator::Eq => w.write_char('='),
        ast::Operator::NotEq => w.write_str("!="),
    }
}

fn serialize_operand(operand: ast::Operand, w: &mut impl fmt::Write) -> fmt::Result {
    match operand {
        ast::Operand::N => w.write_char('n'),
        ast::Operand::I => w.write_char('i'),
        ast::Operand::V => w.write_char('v'),
        ast::Operand::W => w.write_char('w'),
        ast::Operand::F => w.write_char('f'),
        ast::Operand::T => w.write_char('t'),
        ast::Operand::C => w.write_char('c'),
        ast::Operand::E => w.write_char('e'),
    }
}

fn serialize_rangelist(rl: &ast::RangeList, w: &mut impl fmt::Write) -> fmt::Result {
    let mut first = true;

    for rli in rl.0.iter() {
        if first {
            first = false;
        } else {
            w.write_str(", ")?;
        }
        serialize_rangelistitem(rli, w)?
    }
    Ok(())
}

fn serialize_rangelistitem(rli: &ast::RangeListItem, w: &mut impl fmt::Write) -> fmt::Result {
    match rli {
        ast::RangeListItem::Range(range) => serialize_range(range, w),
        ast::RangeListItem::Value(v) => serialize_value(v, w),
    }
}

fn serialize_range(range: &RangeInclusive<ast::Value>, w: &mut impl fmt::Write) -> fmt::Result {
    serialize_value(range.start(), w)?;
    w.write_str("..")?;
    serialize_value(range.end(), w)?;
    Ok(())
}

fn serialize_value(value: &ast::Value, w: &mut impl fmt::Write) -> fmt::Result {
    write!(w, "{}", value.0)
}

/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub fn serialize_samples(samples: &ast::Samples, w: &mut impl fmt::Write) -> fmt::Result {
    if let Some(sample_list) = &samples.integer {
        w.write_str(" @integer ")?;
        serialize_sample_list(sample_list, w)?;
    }
    if let Some(sample_list) = &samples.decimal {
        w.write_str(" @decimal ")?;
        serialize_sample_list(sample_list, w)?;
    }
    Ok(())
}

/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub fn serialize_sample_list(samples: &ast::SampleList, w: &mut impl fmt::Write) -> fmt::Result {
    let mut first = true;

    for sample_range in samples.sample_ranges.iter() {
        if first {
            first = false;
        } else {
            w.write_str(", ")?;
        }
        serialize_sample_range(sample_range, w)?;
    }

    if samples.ellipsis {
        w.write_str(", …")?;
    }
    Ok(())
}

/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub fn serialize_sample_range(
    sample_range: &ast::SampleRange,
    w: &mut impl fmt::Write,
) -> fmt::Result {
    serialize_decimal_value(&sample_range.lower_val, w)?;
    if let Some(upper_val) = &sample_range.upper_val {
        w.write_char('~')?;
        serialize_decimal_value(upper_val, w)?;
    }
    Ok(())
}

/// <div class="stab unstable">
/// 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways,
/// including in SemVer minor releases. In particular, the `DataProvider` implementations are only
/// guaranteed to match with this version's `*_unstable` providers. Use with caution.
/// </div>
pub fn serialize_decimal_value(val: &ast::DecimalValue, w: &mut impl fmt::Write) -> fmt::Result {
    w.write_str(&val.0)
}