Skip to content

Commit dc3a8da

Browse files
committed
Increase test coverage and slightly speed up resolver by bringing more of the Writer goodies
1 parent bae6083 commit dc3a8da

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

fluent-bundle/src/errors.rs

-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ pub enum FluentError {
1111
ResolverError(ResolverError),
1212
}
1313

14-
impl From<ParserError> for FluentError {
15-
fn from(error: ParserError) -> Self {
16-
FluentError::ParserError(error)
17-
}
18-
}
19-
2014
impl From<ResolverError> for FluentError {
2115
fn from(error: ResolverError) -> Self {
2216
FluentError::ResolverError(error)

fluent-bundle/src/resolve.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ impl<'source> ResolveValue<'source> for ast::Pattern<'source> {
157157
string.push_str(&s);
158158
}
159159
ast::PatternElement::Placeable(p) => {
160-
let result = scope
161-
.maybe_track(self, None, |scope| p.resolve(scope))
162-
.to_string();
163160
let needs_isolation = scope.bundle.use_isolating
164161
&& match p {
165162
ast::Expression::InlineExpression(
@@ -174,10 +171,13 @@ impl<'source> ResolveValue<'source> for ast::Pattern<'source> {
174171
_ => true,
175172
};
176173
if needs_isolation {
177-
write!(string, "\u{2068}{}\u{2069}", result).expect("Writing succeeded");
178-
} else {
179-
write!(string, "{}", result).expect("Writing succeeded");
180-
};
174+
string.write_char('\u{2068}').expect("Writing succeeded");
175+
}
176+
let result = scope.maybe_track(self, None, |scope| p.resolve(scope));
177+
write!(string, "{}", result).expect("Writing succeeded");
178+
if needs_isolation {
179+
string.write_char('\u{2069}').expect("Writing succeeded");
180+
}
181181
}
182182
}
183183
}

fluent-bundle/src/types.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,6 @@ impl<'source> From<&ast::Message<'source>> for DisplayableNode<'source> {
7272
}
7373
}
7474

75-
impl<'source> From<(&ast::Message<'source>, &ast::Attribute<'source>)>
76-
for DisplayableNode<'source>
77-
{
78-
fn from(input: (&ast::Message<'source>, &ast::Attribute<'source>)) -> Self {
79-
DisplayableNode {
80-
node_type: DisplayableNodeType::Message,
81-
id: input.0.id.name,
82-
attribute: Some(input.1.id.name),
83-
}
84-
}
85-
}
86-
8775
impl<'source> From<&ast::Term<'source>> for DisplayableNode<'source> {
8876
fn from(term: &ast::Term<'source>) -> Self {
8977
DisplayableNode {
@@ -179,10 +167,10 @@ impl<'source> FluentValue<'source> {
179167
impl<'source> fmt::Display for FluentValue<'source> {
180168
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181169
match self {
182-
FluentValue::String(s) => write!(f, "{}", s),
183-
FluentValue::Number(n) => write!(f, "{}", n),
170+
FluentValue::String(s) => f.write_str(s),
171+
FluentValue::Number(n) => f.write_str(n),
184172
FluentValue::Error(d) => write!(f, "{}", d),
185-
FluentValue::None() => write!(f, "???"),
173+
FluentValue::None() => f.write_str("???"),
186174
}
187175
}
188176
}

fluent-bundle/tests/bundle_test.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ bar =
7272
#[test]
7373
fn bundle_use_isolating() {
7474
let res = assert_get_resource_from_str_no_errors(
75-
"
75+
r#"
76+
-brand-name = Fluent
7677
foo = Foo
77-
bar = Foo { $name } bar
78-
",
78+
bar = Foo { $name } bar { -brand-name } baz { "Literal" }
79+
"#,
7980
);
8081
let mut bundle = assert_get_bundle_no_errors(&res, None);
8182

@@ -87,13 +88,16 @@ bar = Foo { $name } bar
8788
args.insert("name", "John".into());
8889
assert_format_no_errors(
8990
bundle.format("bar", Some(&args)),
90-
"Foo \u{2068}John\u{2069} bar",
91+
"Foo \u{2068}John\u{2069} bar Fluent baz Literal",
9192
);
9293

9394
bundle.set_use_isolating(false);
9495

9596
assert_format_no_errors(bundle.format("foo", None), "Foo");
9697
let mut args = HashMap::new();
9798
args.insert("name", "John".into());
98-
assert_format_no_errors(bundle.format("bar", Some(&args)), "Foo John bar");
99+
assert_format_no_errors(
100+
bundle.format("bar", Some(&args)),
101+
"Foo John bar Fluent baz Literal",
102+
);
99103
}

fluent-bundle/tests/primitives_test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,15 @@ selector-attr = { -bazTerm.attr ->
100100

101101
assert_format_no_errors(bundle.format("selector-attr", None), "FooBarBaz");
102102
}
103+
104+
#[test]
105+
fn primitives_placeable() {
106+
let res = assert_get_resource_from_str_no_errors(
107+
r#"
108+
key = { { "Literal" } }
109+
"#,
110+
);
111+
let bundle = assert_get_bundle_no_errors(&res, None);
112+
113+
assert_format_no_errors(bundle.format("key", None), "Literal");
114+
}

0 commit comments

Comments
 (0)