|
1 | 1 | use super::scope::Scope;
|
2 |
| -use super::{ResolverError, WriteValue}; |
| 2 | +use super::ResolverError; |
3 | 3 |
|
4 | 4 | use std::borrow::Borrow;
|
5 | 5 | use std::fmt;
|
6 | 6 |
|
7 | 7 | use fluent_syntax::ast;
|
8 | 8 |
|
9 | 9 | use crate::memoizer::MemoizerKind;
|
10 |
| -use crate::resolver::ResolveValue; |
11 | 10 | use crate::resource::FluentResource;
|
12 | 11 | use crate::types::FluentValue;
|
13 | 12 |
|
14 | 13 | const MAX_PLACEABLES: u8 = 100;
|
15 | 14 |
|
16 |
| -impl<'bundle> WriteValue<'bundle> for ast::Pattern<&'bundle str> { |
17 |
| - fn write<'ast, 'args, 'errors, W, R, M>( |
18 |
| - &'ast self, |
19 |
| - w: &mut W, |
20 |
| - scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>, |
21 |
| - ) -> fmt::Result |
22 |
| - where |
23 |
| - W: fmt::Write, |
24 |
| - R: Borrow<FluentResource>, |
25 |
| - M: MemoizerKind, |
26 |
| - { |
27 |
| - let len = self.elements.len(); |
| 15 | +pub fn write_pattern<'bundle, 'ast, 'args, 'errors, W, R, M>( |
| 16 | + pattern: &'ast ast::Pattern<&'bundle str>, |
| 17 | + w: &mut W, |
| 18 | + scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>, |
| 19 | +) -> fmt::Result |
| 20 | +where |
| 21 | + W: fmt::Write, |
| 22 | + R: Borrow<FluentResource>, |
| 23 | + M: MemoizerKind, |
| 24 | +{ |
| 25 | + let len = pattern.elements.len(); |
28 | 26 |
|
29 |
| - for elem in &self.elements { |
30 |
| - if scope.dirty { |
31 |
| - return Ok(()); |
32 |
| - } |
| 27 | + for elem in &pattern.elements { |
| 28 | + if scope.dirty { |
| 29 | + return Ok(()); |
| 30 | + } |
33 | 31 |
|
34 |
| - match elem { |
35 |
| - ast::PatternElement::TextElement { value } => { |
36 |
| - if let Some(ref transform) = scope.bundle.transform { |
37 |
| - w.write_str(&transform(value))?; |
38 |
| - } else { |
39 |
| - w.write_str(value)?; |
40 |
| - } |
| 32 | + match elem { |
| 33 | + ast::PatternElement::TextElement { value } => { |
| 34 | + if let Some(ref transform) = scope.bundle.transform { |
| 35 | + w.write_str(&transform(value))?; |
| 36 | + } else { |
| 37 | + w.write_str(value)?; |
| 38 | + } |
| 39 | + } |
| 40 | + ast::PatternElement::Placeable { ref expression } => { |
| 41 | + scope.placeables += 1; |
| 42 | + if scope.placeables > MAX_PLACEABLES { |
| 43 | + scope.dirty = true; |
| 44 | + scope.add_error(ResolverError::TooManyPlaceables); |
| 45 | + return Ok(()); |
41 | 46 | }
|
42 |
| - ast::PatternElement::Placeable { ref expression } => { |
43 |
| - scope.placeables += 1; |
44 |
| - if scope.placeables > MAX_PLACEABLES { |
45 |
| - scope.dirty = true; |
46 |
| - scope.add_error(ResolverError::TooManyPlaceables); |
47 |
| - return Ok(()); |
48 |
| - } |
49 | 47 |
|
50 |
| - let needs_isolation = scope.bundle.use_isolating |
51 |
| - && len > 1 |
52 |
| - && !matches!( |
53 |
| - expression, |
54 |
| - ast::Expression::Inline(ast::InlineExpression::MessageReference { .. },) |
55 |
| - | ast::Expression::Inline( |
56 |
| - ast::InlineExpression::TermReference { .. }, |
57 |
| - ) |
58 |
| - | ast::Expression::Inline( |
59 |
| - ast::InlineExpression::StringLiteral { .. }, |
60 |
| - ) |
61 |
| - ); |
62 |
| - if needs_isolation { |
63 |
| - w.write_char('\u{2068}')?; |
64 |
| - } |
65 |
| - scope.maybe_track(w, self, expression)?; |
66 |
| - if needs_isolation { |
67 |
| - w.write_char('\u{2069}')?; |
68 |
| - } |
| 48 | + let needs_isolation = scope.bundle.use_isolating |
| 49 | + && len > 1 |
| 50 | + && !matches!( |
| 51 | + expression, |
| 52 | + ast::Expression::Inline(ast::InlineExpression::MessageReference { .. },) |
| 53 | + | ast::Expression::Inline(ast::InlineExpression::TermReference { .. },) |
| 54 | + | ast::Expression::Inline(ast::InlineExpression::StringLiteral { .. },) |
| 55 | + ); |
| 56 | + if needs_isolation { |
| 57 | + w.write_char('\u{2068}')?; |
| 58 | + } |
| 59 | + scope.maybe_track(w, pattern, expression)?; |
| 60 | + if needs_isolation { |
| 61 | + w.write_char('\u{2069}')?; |
69 | 62 | }
|
70 | 63 | }
|
71 | 64 | }
|
72 |
| - Ok(()) |
73 | 65 | }
|
| 66 | + Ok(()) |
74 | 67 | }
|
75 | 68 |
|
76 |
| -impl<'bundle> ResolveValue<'bundle> for ast::Pattern<&'bundle str> { |
77 |
| - fn resolve<'ast, 'args, 'errors, R, M>( |
78 |
| - &'ast self, |
79 |
| - scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>, |
80 |
| - ) -> FluentValue<'bundle> |
81 |
| - where |
82 |
| - R: Borrow<FluentResource>, |
83 |
| - M: MemoizerKind, |
84 |
| - { |
85 |
| - let len = self.elements.len(); |
| 69 | +pub fn resolve_pattern<'bundle, 'ast, 'args, 'errors, R, M>( |
| 70 | + pattern: &'ast ast::Pattern<&'bundle str>, |
| 71 | + scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>, |
| 72 | +) -> FluentValue<'bundle> |
| 73 | +where |
| 74 | + R: Borrow<FluentResource>, |
| 75 | + M: MemoizerKind, |
| 76 | +{ |
| 77 | + let len = pattern.elements.len(); |
86 | 78 |
|
87 |
| - if len == 1 { |
88 |
| - if let ast::PatternElement::TextElement { value } = self.elements[0] { |
89 |
| - return scope |
90 |
| - .bundle |
91 |
| - .transform |
92 |
| - .map_or_else(|| value.into(), |transform| transform(value).into()); |
93 |
| - } |
| 79 | + if len == 1 { |
| 80 | + if let ast::PatternElement::TextElement { value } = pattern.elements[0] { |
| 81 | + return scope |
| 82 | + .bundle |
| 83 | + .transform |
| 84 | + .map_or_else(|| value.into(), |transform| transform(value).into()); |
94 | 85 | }
|
95 |
| - |
96 |
| - let mut result = String::new(); |
97 |
| - self.write(&mut result, scope) |
98 |
| - .expect("Failed to write to a string."); |
99 |
| - result.into() |
100 | 86 | }
|
| 87 | + |
| 88 | + let mut result = String::new(); |
| 89 | + write_pattern(pattern, &mut result, scope).expect("Failed to write to a string."); |
| 90 | + result.into() |
101 | 91 | }
|
0 commit comments