Skip to content

Commit ad8fbc5

Browse files
committed
Minor cleanups and switch to fmt::Display in types
1 parent e33a74d commit ad8fbc5

File tree

3 files changed

+85
-74
lines changed

3 files changed

+85
-74
lines changed

fluent-bundle/src/bundle.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::entry::GetEntry;
1717
use crate::errors::FluentError;
1818
use crate::resolve::{resolve_value_for_entry, Scope};
1919
use crate::resource::FluentResource;
20-
use crate::types::DisplayableNode;
2120
use crate::types::FluentValue;
2221

2322
#[derive(Debug, PartialEq)]
@@ -387,25 +386,14 @@ impl<'bundle, R> FluentBundle<'bundle, R> {
387386
.attributes
388387
.iter()
389388
.find(|attr| attr.id.name == attr_name)?;
390-
resolve_value_for_entry(
391-
&attr.value,
392-
DisplayableNode::new(message.id.name, Some(attr.id.name)),
393-
&mut scope,
394-
)
395-
.to_string()
389+
resolve_value_for_entry(&attr.value, (message, attr).into(), &mut scope).to_string()
396390
} else {
397391
let message_id = path;
398392
let message = self.get_message(message_id)?;
399393
message
400394
.value
401395
.as_ref()
402-
.map(|value| {
403-
resolve_value_for_entry(
404-
value,
405-
DisplayableNode::new(message.id.name, None),
406-
&mut scope,
407-
)
408-
})?
396+
.map(|value| resolve_value_for_entry(value, message.into(), &mut scope))?
409397
.to_string()
410398
};
411399

@@ -470,10 +458,10 @@ impl<'bundle, R> FluentBundle<'bundle, R> {
470458
let mut errors = vec![];
471459
let message = self.get_message(message_id)?;
472460

473-
let value = message.value.as_ref().map(|value| {
474-
resolve_value_for_entry(value, DisplayableNode::new(message.id.name, None), &mut scope)
475-
.to_string()
476-
});
461+
let value = message
462+
.value
463+
.as_ref()
464+
.map(|value| resolve_value_for_entry(value, message.into(), &mut scope).to_string());
477465

478466
// Setting capacity helps performance for cases with attributes,
479467
// but is slower than `::new` for cases without.
@@ -485,11 +473,7 @@ impl<'bundle, R> FluentBundle<'bundle, R> {
485473
};
486474

487475
for attr in message.attributes.iter() {
488-
let val = resolve_value_for_entry(
489-
&attr.value,
490-
DisplayableNode::new(message.id.name, Some(attr.id.name)),
491-
&mut scope,
492-
);
476+
let val = resolve_value_for_entry(&attr.value, (message, attr).into(), &mut scope);
493477
attributes.insert(attr.id.name, val.to_string());
494478
}
495479

fluent-bundle/src/resolve.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ impl<'bundle, R: Borrow<FluentResource>> Scope<'bundle, R> {
6767
F: FnMut(&mut Scope<'bundle, R>) -> FluentValue<'bundle>,
6868
{
6969
let mut hasher = DefaultHasher::new();
70-
(entry.id, entry.attribute).hash(&mut hasher);
71-
entry.id.hash(&mut hasher);
72-
if let Some(attr) = entry.attribute {
73-
attr.hash(&mut hasher);
74-
}
70+
entry.hash(&mut hasher);
7571
let hash = hasher.finish();
7672

7773
if self.travelled.borrow().contains(&hash) {
@@ -109,7 +105,9 @@ where
109105
if value.elements.len() == 1 {
110106
return match value.elements[0] {
111107
ast::PatternElement::TextElement(s) => FluentValue::String(s.into()),
112-
ast::PatternElement::Placeable(ref p) => scope.track(entry.clone(), |scope| p.resolve(scope)),
108+
ast::PatternElement::Placeable(ref p) => {
109+
scope.track(entry.clone(), |scope| p.resolve(scope))
110+
}
113111
};
114112
}
115113

@@ -135,7 +133,9 @@ fn generate_ref_error<'source, R>(
135133
where
136134
R: Borrow<FluentResource>,
137135
{
138-
scope.errors.push(ResolverError::Reference(node.get_error()));
136+
scope
137+
.errors
138+
.push(ResolverError::Reference(node.get_error()));
139139
FluentValue::Error(node)
140140
}
141141

@@ -243,7 +243,8 @@ impl<'source> ResolveValue<'source> for ast::InlineExpression<'source> {
243243

244244
if let Some(msg) = msg {
245245
if let Some(attr) = attribute {
246-
scope.maybe_resolve_attribute(&msg.attributes, self.into(), attr.name)
246+
scope
247+
.maybe_resolve_attribute(&msg.attributes, self.into(), attr.name)
247248
.unwrap_or_else(|| generate_ref_error(scope, self.into()))
248249
} else if let Some(value) = msg.value.as_ref() {
249250
scope.track(self.into(), |scope| value.resolve(scope))
@@ -268,7 +269,8 @@ impl<'source> ResolveValue<'source> for ast::InlineExpression<'source> {
268269

269270
let value = if let Some(term) = term {
270271
if let Some(attr) = attribute {
271-
scope.maybe_resolve_attribute(&term.attributes, self.into(), attr.name)
272+
scope
273+
.maybe_resolve_attribute(&term.attributes, self.into(), attr.name)
272274
.unwrap_or_else(|| generate_ref_error(scope, self.into()))
273275
} else {
274276
term.resolve(&mut scope)
@@ -280,7 +282,8 @@ impl<'source> ResolveValue<'source> for ast::InlineExpression<'source> {
280282
value
281283
}
282284
ast::InlineExpression::FunctionReference { id, arguments } => {
283-
let (resolved_positional_args, resolved_named_args) = get_arguments(scope, arguments);
285+
let (resolved_positional_args, resolved_named_args) =
286+
get_arguments(scope, arguments);
284287

285288
let func = scope.bundle.get_function(id.name);
286289

@@ -301,7 +304,8 @@ impl<'source> ResolveValue<'source> for ast::InlineExpression<'source> {
301304
} else {
302305
let displayable_node: DisplayableNode = self.into();
303306
if scope.local_args.is_none() {
304-
scope.errors
307+
scope
308+
.errors
305309
.push(ResolverError::Reference(displayable_node.get_error()));
306310
}
307311
FluentValue::Error(displayable_node)

fluent-bundle/src/types.rs

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//! [`resolve`]: ../resolve
1212
//! [`FluentBundle::format`]: ../bundle/struct.FluentBundle.html#method.format
1313
14-
use std::borrow::Borrow;
15-
use std::borrow::Cow;
14+
use std::borrow::{Borrow, Cow};
15+
use std::fmt;
1616
use std::str::FromStr;
1717

1818
use fluent_syntax::ast;
@@ -21,52 +21,65 @@ use intl_pluralrules::PluralCategory;
2121
use crate::resolve::Scope;
2222
use crate::resource::FluentResource;
2323

24-
#[derive(Debug, PartialEq, Clone)]
24+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
2525
pub enum DisplayableNodeType {
2626
Message,
2727
Term,
2828
Variable,
2929
Function,
3030
}
3131

32-
#[derive(Debug, PartialEq, Clone)]
32+
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash)]
3333
pub struct DisplayableNode<'source> {
34-
pub node_type: DisplayableNodeType,
35-
pub id: &'source str,
36-
pub attribute: Option<&'source str>,
34+
node_type: DisplayableNodeType,
35+
id: &'source str,
36+
attribute: Option<&'source str>,
3737
}
3838

3939
impl<'source> DisplayableNode<'source> {
40-
pub fn display(&self) -> String {
41-
let mut id = self.id.to_owned();
42-
if let Some(attr) = self.attribute {
43-
id.push_str(".");
44-
id.push_str(attr);
45-
}
40+
pub fn get_error(&self) -> String {
4641
match self.node_type {
47-
DisplayableNodeType::Message => id,
48-
DisplayableNodeType::Term => format!("-{}", id),
49-
DisplayableNodeType::Variable => format!("${}", id),
50-
DisplayableNodeType::Function => format!("{}()", id),
42+
DisplayableNodeType::Message => format!("Unknown message: {}", self),
43+
DisplayableNodeType::Term => format!("Unknown term: {}", self),
44+
DisplayableNodeType::Variable => format!("Unknown variable: {}", self),
45+
DisplayableNodeType::Function => format!("Unknown function: {}", self),
5146
}
5247
}
48+
}
5349

54-
pub fn get_error(&self) -> String {
55-
let mut id = match self.node_type {
56-
DisplayableNodeType::Message => String::from("Unknown message: "),
57-
DisplayableNodeType::Term => String::from("Unknown term: "),
58-
DisplayableNodeType::Variable => String::from("Unknown variable: "),
59-
DisplayableNodeType::Function => String::from("Unknown function: "),
50+
impl<'source> fmt::Display for DisplayableNode<'source> {
51+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52+
match self.node_type {
53+
DisplayableNodeType::Message => write!(f, "{}", self.id)?,
54+
DisplayableNodeType::Term => write!(f, "-{}", self.id)?,
55+
DisplayableNodeType::Variable => write!(f, "${}", self.id)?,
56+
DisplayableNodeType::Function => write!(f, "{}()", self.id)?,
6057
};
61-
id.push_str(&self.display());
62-
id
58+
if let Some(attr) = self.attribute {
59+
write!(f, ".{}", attr)?;
60+
}
61+
Ok(())
62+
}
63+
}
64+
65+
impl<'source> From<&ast::Message<'source>> for DisplayableNode<'source> {
66+
fn from(msg: &ast::Message<'source>) -> Self {
67+
DisplayableNode {
68+
node_type: DisplayableNodeType::Message,
69+
id: msg.id.name,
70+
attribute: None,
71+
}
6372
}
73+
}
6474

65-
pub fn new(id: &'source str, attribute: Option<&'source str>) -> Self {
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 {
6679
DisplayableNode {
6780
node_type: DisplayableNodeType::Message,
68-
id,
69-
attribute,
81+
id: input.0.id.name,
82+
attribute: Some(input.1.id.name),
7083
}
7184
}
7285
}
@@ -121,13 +134,18 @@ pub enum FluentValue<'source> {
121134

122135
impl<'source> FluentValue<'source> {
123136
pub fn into_number<S: ToString>(v: S) -> Self {
124-
match f64::from_str(&v.to_string()) {
125-
Ok(_) => FluentValue::Number(v.to_string().into()),
126-
Err(_) => FluentValue::String(v.to_string().into()),
137+
let s = v.to_string();
138+
match f64::from_str(&s) {
139+
Ok(_) => FluentValue::Number(s.into()),
140+
Err(_) => FluentValue::String(s.into()),
127141
}
128142
}
129143

130-
pub fn matches<R: Borrow<FluentResource>>(&self, other: &FluentValue, scope: &Scope<R>) -> bool {
144+
pub fn matches<R: Borrow<FluentResource>>(
145+
&self,
146+
other: &FluentValue,
147+
scope: &Scope<R>,
148+
) -> bool {
131149
match (self, other) {
132150
(&FluentValue::String(ref a), &FluentValue::String(ref b)) => a == b,
133151
(&FluentValue::Number(ref a), &FluentValue::Number(ref b)) => a == b,
@@ -152,21 +170,26 @@ impl<'source> FluentValue<'source> {
152170
match self {
153171
FluentValue::String(s) => s.clone(),
154172
FluentValue::Number(n) => n.clone(),
155-
FluentValue::Error(d) => d.display().into(),
156-
FluentValue::None() => String::from("???").into(),
173+
FluentValue::Error(d) => d.to_string().into(),
174+
FluentValue::None() => "???".into(),
157175
}
158176
}
159177
}
160178

161-
impl<'source> From<String> for FluentValue<'source> {
162-
fn from(s: String) -> Self {
163-
FluentValue::String(s.into())
179+
impl<'source> fmt::Display for FluentValue<'source> {
180+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181+
match self {
182+
FluentValue::String(s) => write!(f, "{}", s),
183+
FluentValue::Number(n) => write!(f, "{}", n),
184+
FluentValue::Error(d) => write!(f, "{}", d),
185+
FluentValue::None() => write!(f, "???"),
186+
}
164187
}
165188
}
166189

167-
impl<'source> From<&'source String> for FluentValue<'source> {
168-
fn from(s: &'source String) -> Self {
169-
FluentValue::String((&s[..]).into())
190+
impl<'source> From<String> for FluentValue<'source> {
191+
fn from(s: String) -> Self {
192+
FluentValue::String(s.into())
170193
}
171194
}
172195

0 commit comments

Comments
 (0)