summaryrefslogtreecommitdiffstats
path: root/third_party/rust/fluent-testing/src/scenarios/structs.rs
blob: 7ba2551c13776d765aa052a34f5bbed4d906361d (plain)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use fluent_fallback::types::ResourceId;

pub struct FileSource {
    pub name: String,
    pub locales: Vec<String>,
    pub path_scheme: String,
}

impl Default for FileSource {
    fn default() -> Self {
        Self {
            name: "default".to_string(),
            path_scheme: "{locale}/".to_string(),
            locales: vec!["en-US".to_string()],
        }
    }
}

impl FileSource {
    pub fn new<S: ToString>(name: S, path_scheme: S, locales: Vec<S>) -> Self {
        Self {
            name: name.to_string(),
            path_scheme: path_scheme.to_string(),
            locales: locales
                .iter()
                .map(|l| l.to_string().parse().unwrap())
                .collect(),
        }
    }
}

#[derive(Debug)]
pub struct L10nAttribute {
    pub name: String,
    pub value: String,
}

impl L10nAttribute {
    pub fn new<S: ToString>(name: S, value: S) -> Self {
        Self {
            name: name.to_string(),
            value: value.to_string(),
        }
    }
}

#[derive(Debug)]
pub struct L10nMessage {
    pub value: Option<String>,
    pub attributes: Option<Vec<L10nAttribute>>,
}

impl L10nMessage {
    pub fn new(value: Option<&str>, attributes: Option<Vec<L10nAttribute>>) -> Self {
        Self {
            value: value.map(|v| v.to_string()),
            attributes,
        }
    }
}

impl From<&str> for L10nMessage {
    fn from(value: &str) -> Self {
        Self {
            value: Some(value.to_string()),
            attributes: None,
        }
    }
}

#[derive(Debug)]
pub struct L10nArgument {
    pub id: String,
    pub value: String,
}

impl L10nArgument {
    pub fn new<S: ToString>(id: S, value: S) -> Self {
        Self {
            id: id.to_string(),
            value: value.to_string(),
        }
    }
}

#[derive(Debug)]
pub struct L10nKey {
    pub id: String,
    pub args: Option<Vec<L10nArgument>>,
}

impl L10nKey {
    pub fn new<S: ToString>(id: S, args: Option<Vec<L10nArgument>>) -> Self {
        Self {
            id: id.to_string(),
            args,
        }
    }
}

impl From<&str> for L10nKey {
    fn from(input: &str) -> Self {
        Self {
            id: input.to_string(),
            args: None,
        }
    }
}

#[derive(Clone, Copy, Debug)]
pub enum ExceptionalContext {
    /// There is no exceptional context for this query (happy path).
    None,
    /// A value is missing from a resource and should cause a fallback.
    ValueMissingFromResource,
    /// A value is missing from all resources in all locales.
    ValueMissingFromAllResources,
    /// An optional resource is missing from the top locale.
    OptionalResourceMissingFromLocale,
    /// An optional resource is missing from all locales.
    OptionalResourceMissingFromAllLocales,
    /// A required resource is missing from the top locale.
    RequiredResourceMissingFromLocale,
    /// A required resource is missing from all locales.
    RequiredResourceMissingFromAllLocales,
}

impl ExceptionalContext {
    /// This is a query for a value in a missing required resource.
    pub fn missing_required_resource(self) -> bool {
        matches!(
            self,
            Self::RequiredResourceMissingFromLocale | Self::RequiredResourceMissingFromAllLocales,
        )
    }

    /// This query should cause a format error to be appended to the errors Vec.
    pub fn causes_reported_format_error(self) -> bool {
        matches!(
            self,
            Self::ValueMissingFromResource
                | Self::ValueMissingFromAllResources
                | Self::OptionalResourceMissingFromLocale
                | Self::OptionalResourceMissingFromAllLocales
                | Self::RequiredResourceMissingFromAllLocales,
        )
    }

    /// This query should cause a failed value lookup.
    pub fn causes_failed_value_lookup(self) -> bool {
        matches!(
            self,
            Self::ValueMissingFromAllResources
                | Self::OptionalResourceMissingFromAllLocales
                | Self::RequiredResourceMissingFromAllLocales,
        )
    }

    /// This query should result in no bundles being generated.
    pub fn blocks_bundle_generation(self) -> bool {
        matches!(self, Self::RequiredResourceMissingFromAllLocales,)
    }
}

#[derive(Debug)]
pub struct Query {
    pub input: L10nKey,
    pub output: Option<L10nMessage>,
    pub exceptional_context: ExceptionalContext,
}

impl Query {
    pub fn new<K: Into<L10nKey>>(input: K, output: Option<L10nMessage>) -> Self {
        Self {
            input: input.into(),
            output,
            exceptional_context: ExceptionalContext::None,
        }
    }
}

impl From<(&str, &str)> for Query {
    fn from(i: (&str, &str)) -> Self {
        Self {
            input: i.0.into(),
            output: Some(i.1.into()),
            exceptional_context: ExceptionalContext::None,
        }
    }
}

impl From<(&str, &str, ExceptionalContext)> for Query {
    fn from(i: (&str, &str, ExceptionalContext)) -> Self {
        Self {
            input: i.0.into(),
            output: Some(i.1.into()),
            exceptional_context: i.2,
        }
    }
}

impl From<(&str, L10nMessage)> for Query {
    fn from(i: (&str, L10nMessage)) -> Self {
        Self {
            input: i.0.into(),
            output: Some(i.1),
            exceptional_context: ExceptionalContext::None,
        }
    }
}

impl From<(L10nKey, L10nMessage)> for Query {
    fn from(i: (L10nKey, L10nMessage)) -> Self {
        Self {
            input: i.0,
            output: Some(i.1),
            exceptional_context: ExceptionalContext::None,
        }
    }
}

impl From<&str> for Query {
    fn from(i: &str) -> Self {
        Self {
            input: i.into(),
            output: None,
            exceptional_context: ExceptionalContext::None,
        }
    }
}

impl From<L10nKey> for Query {
    fn from(key: L10nKey) -> Self {
        Self {
            input: key,
            output: None,
            exceptional_context: ExceptionalContext::None,
        }
    }
}

pub struct Queries(pub Vec<Query>);

impl From<Vec<&str>> for Queries {
    fn from(input: Vec<&str>) -> Self {
        Self(input.into_iter().map(|q| q.into()).collect())
    }
}

impl From<Vec<(&str, &str)>> for Queries {
    fn from(input: Vec<(&str, &str)>) -> Self {
        Self(input.into_iter().map(|q| q.into()).collect())
    }
}

impl std::ops::Deref for Queries {
    type Target = Vec<Query>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

pub struct Scenario {
    pub name: String,
    pub file_sources: Vec<FileSource>,
    pub locales: Vec<String>,
    pub res_ids: Vec<ResourceId>,
    pub queries: Queries,
}

impl Scenario {
    pub fn new<S: ToString, R: Into<ResourceId>, Q: Into<Queries>>(
        name: S,
        file_sources: Vec<FileSource>,
        locales: Vec<S>,
        res_ids: Vec<R>,
        queries: Q,
    ) -> Self {
        Self {
            name: name.to_string(),
            file_sources,
            locales: locales
                .into_iter()
                .map(|l| l.to_string().parse().unwrap())
                .collect(),
            res_ids: res_ids.into_iter().map(|id| id.into()).collect(),
            queries: queries.into(),
        }
    }
}