Skip to content

Commit 1ee2a7c

Browse files
JasperDeSutteralerque
authored andcommitted
refactor(fluent-bundle): Remove WriteValue and ResolveValue traits
1 parent af86dfb commit 1ee2a7c

File tree

5 files changed

+79
-110
lines changed

5 files changed

+79
-110
lines changed

fluent-bundle/src/bundle.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::entry::GetEntry;
2121
use crate::errors::{EntryKind, FluentError};
2222
use crate::memoizer::MemoizerKind;
2323
use crate::message::FluentMessage;
24-
use crate::resolver::{ResolveValue, Scope, WriteValue};
24+
use crate::resolver::pattern::{resolve_pattern, write_pattern};
25+
use crate::resolver::Scope;
2526
use crate::resource::FluentResource;
2627
use crate::types::FluentValue;
2728

@@ -450,7 +451,7 @@ impl<R, M> FluentBundle<R, M> {
450451
M: MemoizerKind,
451452
{
452453
let mut scope = Scope::new(self, args, Some(errors));
453-
pattern.write(w, &mut scope)
454+
write_pattern(pattern, w, &mut scope)
454455
}
455456

456457
/// Formats a pattern which comes from a `FluentMessage`.
@@ -493,7 +494,7 @@ impl<R, M> FluentBundle<R, M> {
493494
M: MemoizerKind,
494495
{
495496
let mut scope = Scope::new(self, args, Some(errors));
496-
let value = pattern.resolve(&mut scope);
497+
let value = resolve_pattern(pattern, &mut scope);
497498
value.into_string(&scope)
498499
}
499500

fluent-bundle/src/resolver/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
}
4949

5050
/// Errors generated during the process of resolving a fluent message into a string.
51-
/// This process takes place in the `write` method of the `WriteValue` trait.
51+
/// This process takes place in the `write_or_resolve` method of the `WriteOrResolve` trait.
5252
#[derive(Debug, PartialEq, Eq, Clone)]
5353
pub enum ResolverError {
5454
Reference(ReferenceKind),

fluent-bundle/src/resolver/mod.rs

+9-31
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! The `resolver` module contains the definitions and implementations for the internal
2-
//! `ResolveValue` and `WriteValue` traits. The former converts AST nodes to a
3-
//! [`FluentValue`], and the latter converts them to a string that is written to an
4-
//! implementor of the [`std::fmt::Write`] trait.
2+
//! `WriteOrResolve` and `WriteOrResolveContext` traits.
3+
//! There is an implementation that resolves AST nodes to a [`FluentValue`], and one
4+
//! that writes to an implementor of the [`std::fmt::Write`] trait.
55
66
pub mod errors;
77
mod expression;
88
mod inline_expression;
9-
pub mod pattern;
9+
pub(crate) mod pattern;
1010
mod scope;
1111

1212
pub use errors::ResolverError;
@@ -21,31 +21,7 @@ use crate::memoizer::MemoizerKind;
2121
use crate::resource::FluentResource;
2222
use crate::types::FluentValue;
2323

24-
/// Resolves an AST node to a [`FluentValue`].
25-
pub(crate) trait ResolveValue<'bundle> {
26-
/// Resolves an AST node to a [`FluentValue`].
27-
fn resolve<'ast, 'args, 'errors, R, M>(
28-
&'ast self,
29-
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
30-
) -> FluentValue<'bundle>
31-
where
32-
R: Borrow<FluentResource>,
33-
M: MemoizerKind;
34-
}
35-
36-
/// Resolves an AST node to a string that is written to source `W`.
37-
pub(crate) trait WriteValue<'bundle> {
38-
/// Resolves an AST node to a string that is written to source `W`.
39-
fn write<'ast, 'args, 'errors, W, R, M>(
40-
&'ast self,
41-
w: &mut W,
42-
scope: &mut Scope<'bundle, 'ast, 'args, 'errors, R, M>,
43-
) -> fmt::Result
44-
where
45-
W: fmt::Write,
46-
R: Borrow<FluentResource>,
47-
M: MemoizerKind;
48-
}
24+
use self::pattern::{resolve_pattern, write_pattern};
4925

5026
pub trait WriteOrResolveContext<'bundle> {
5127
type Result;
@@ -71,6 +47,7 @@ pub trait WriteOrResolveContext<'bundle> {
7147
M: MemoizerKind;
7248
}
7349

50+
/// Resolves an AST node to a string that is written to source `W`.
7451
impl<'bundle, W> WriteOrResolveContext<'bundle> for W
7552
where
7653
W: fmt::Write,
@@ -118,10 +95,11 @@ where
11895
R: Borrow<FluentResource>,
11996
M: MemoizerKind,
12097
{
121-
pattern.write(self, scope)
98+
write_pattern(pattern, self, scope)
12299
}
123100
}
124101

102+
/// Resolves an AST node to a [`FluentValue`].
125103
struct ResolveContext;
126104

127105
impl<'bundle> WriteOrResolveContext<'bundle> for ResolveContext {
@@ -156,7 +134,7 @@ impl<'bundle> WriteOrResolveContext<'bundle> for ResolveContext {
156134
R: Borrow<FluentResource>,
157135
M: MemoizerKind,
158136
{
159-
pattern.resolve(scope)
137+
resolve_pattern(pattern, scope)
160138
}
161139
}
162140

fluent-bundle/src/resolver/pattern.rs

+64-74
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,91 @@
11
use super::scope::Scope;
2-
use super::{ResolverError, WriteValue};
2+
use super::ResolverError;
33

44
use std::borrow::Borrow;
55
use std::fmt;
66

77
use fluent_syntax::ast;
88

99
use crate::memoizer::MemoizerKind;
10-
use crate::resolver::ResolveValue;
1110
use crate::resource::FluentResource;
1211
use crate::types::FluentValue;
1312

1413
const MAX_PLACEABLES: u8 = 100;
1514

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();
2826

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+
}
3331

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(());
4146
}
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-
}
4947

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}')?;
6962
}
7063
}
7164
}
72-
Ok(())
7365
}
66+
Ok(())
7467
}
7568

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();
8678

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());
9485
}
95-
96-
let mut result = String::new();
97-
self.write(&mut result, scope)
98-
.expect("Failed to write to a string.");
99-
result.into()
10086
}
87+
88+
let mut result = String::new();
89+
write_pattern(pattern, &mut result, scope).expect("Failed to write to a string.");
90+
result.into()
10191
}

fluent-bundle/src/resolver/scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::borrow::Borrow;
77

88
use super::{ResolveContext, ResolverError, WriteOrResolve, WriteOrResolveContext};
99

10-
/// State for a single `ResolveValue::to_value` call.
10+
/// State for a single `WriteOrResolve::write_or_resolve` call.
1111
pub struct Scope<'bundle, 'ast, 'args, 'errors, R, M> {
1212
/// The current `FluentBundle` instance.
1313
pub bundle: &'bundle FluentBundle<R, M>,

0 commit comments

Comments
 (0)