Skip to content

Commit 97f9539

Browse files
committed
De-generalize FluentMessage since it had a performance hit.
1 parent 3e7aa82 commit 97f9539

File tree

4 files changed

+182
-21
lines changed

4 files changed

+182
-21
lines changed

fluent-bundle/de.diff

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
diff --git a/fluent-bundle/src/bundle.rs b/fluent-bundle/src/bundle.rs
2+
index ba98774..fb595b0 100644
3+
--- a/fluent-bundle/src/bundle.rs
4+
+++ b/fluent-bundle/src/bundle.rs
5+
@@ -386,7 +386,7 @@ impl<R, M> FluentBundle<R, M> {
6+
/// let msg = bundle.get_message("hello-world");
7+
/// assert_eq!(msg.is_some(), true);
8+
/// ```
9+
- pub fn get_message(&self, id: &str) -> Option<FluentMessage<&str>>
10+
+ pub fn get_message(&self, id: &str) -> Option<FluentMessage>
11+
where
12+
R: Borrow<FluentResource>,
13+
{
14+
diff --git a/fluent-bundle/src/lib.rs b/fluent-bundle/src/lib.rs
15+
index edfc2b3..04a093d 100644
16+
--- a/fluent-bundle/src/lib.rs
17+
+++ b/fluent-bundle/src/lib.rs
18+
@@ -8,6 +8,11 @@
19+
//!
20+
//! There are four core concepts to understand Fluent runtime:
21+
//!
22+
+//! * [`FluentMessage`] - A single translation unit
23+
+//! * [`FluentResource`] - A list of [`FluentMessage`] units
24+
+//! * [`FluentBundle`] - A collection of [`FluentResource`] lists
25+
+//! * [`FluentArgs`] - A list of [`FluentValue`] elements used to resolve a [`FluentMessage`] value
26+
+//!
27+
//! # 1) Message
28+
//!
29+
//! [`FluentMessage`] is the core unit of the system.
30+
diff --git a/fluent-bundle/src/message.rs b/fluent-bundle/src/message.rs
31+
index 93c3487..284fb4b 100644
32+
--- a/fluent-bundle/src/message.rs
33+
+++ b/fluent-bundle/src/message.rs
34+
@@ -1,38 +1,70 @@
35+
-use fluent_syntax::{ast, parser::Slice};
36+
+use fluent_syntax::ast;
37+
38+
#[derive(Debug, PartialEq)]
39+
-pub struct FluentAttribute<'m, S> {
40+
- pub id: &'m S,
41+
- pub value: &'m ast::Pattern<S>,
42+
+pub struct FluentAttribute<'m> {
43+
+ pub id: &'m str,
44+
+ pub value: &'m ast::Pattern<&'m str>,
45+
}
46+
47+
-impl<'m, S> From<&'m ast::Attribute<S>> for FluentAttribute<'m, S> {
48+
- fn from(attr: &'m ast::Attribute<S>) -> Self {
49+
+impl<'m> From<&'m ast::Attribute<&'m str>> for FluentAttribute<'m> {
50+
+ fn from(attr: &'m ast::Attribute<&'m str>) -> Self {
51+
FluentAttribute {
52+
- id: &attr.id.name,
53+
+ id: attr.id.name,
54+
value: &attr.value,
55+
}
56+
}
57+
}
58+
-/// A single localization unit composed of an identifier,
59+
-/// value, and attributes.
60+
+
61+
+/// [`FluentMessage`] is a basic translation unit of the Fluent system.
62+
+///
63+
+/// A message is composed of a value and, optionally a list of attributes.
64+
+///
65+
+/// ### Simple Message
66+
+///
67+
+/// ```
68+
+/// use fluent_bundle::{FluentResource, FluentBundle};
69+
+///
70+
+/// let source = r#"
71+
+///
72+
+/// hello-world = Hello, ${ user }
73+
+///
74+
+/// "#;
75+
+///
76+
+/// let resource = FluentResource::try_new(source.to_string())
77+
+/// .expect("Failed to parse the resource.");
78+
+///
79+
+/// let mut bundle = FluentBundle::default();
80+
+/// bundle.add_resource(resource)
81+
+/// .expect("Failed to add a resource.");
82+
+///
83+
+/// let msg = bundle.get_message("hello-world")
84+
+/// .expect("Failed to retrieve a message.");
85+
+///
86+
+/// assert!(msg.value.is_some());
87+
+/// ```
88+
+///
89+
+/// ### Compound Message
90+
+///
91+
+/// ```text
92+
+/// confirm-modal = Are you sure?
93+
+/// .confirm = Yes
94+
+/// .cancel = No
95+
+/// .tooltip = Closing the window will lose all unsaved data.
96+
+/// ```
97+
#[derive(Debug, PartialEq)]
98+
-pub struct FluentMessage<'m, S> {
99+
- pub value: Option<&'m ast::Pattern<S>>,
100+
- pub attributes: Vec<FluentAttribute<'m, S>>,
101+
+pub struct FluentMessage<'m> {
102+
+ pub value: Option<&'m ast::Pattern<&'m str>>,
103+
+ pub attributes: Vec<FluentAttribute<'m>>,
104+
}
105+
106+
-impl<'m, S> FluentMessage<'m, S> {
107+
- pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute<S>>
108+
- where
109+
- S: Slice<'m>,
110+
- {
111+
- self.attributes.iter().find(|attr| attr.id.as_ref() == key)
112+
+impl<'m> FluentMessage<'m> {
113+
+ pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute> {
114+
+ self.attributes.iter().find(|attr| attr.id == key)
115+
}
116+
}
117+
118+
-impl<'m, S> From<&'m ast::Message<S>> for FluentMessage<'m, S> {
119+
- fn from(msg: &'m ast::Message<S>) -> Self {
120+
+impl<'m> From<&'m ast::Message<&'m str>> for FluentMessage<'m> {
121+
+ fn from(msg: &'m ast::Message<&'m str>) -> Self {
122+
FluentMessage {
123+
value: msg.value.as_ref(),
124+
attributes: msg.attributes.iter().map(Into::into).collect(),

fluent-bundle/src/bundle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<R, M> FluentBundle<R, M> {
386386
/// let msg = bundle.get_message("hello-world");
387387
/// assert_eq!(msg.is_some(), true);
388388
/// ```
389-
pub fn get_message(&self, id: &str) -> Option<FluentMessage<&str>>
389+
pub fn get_message(&self, id: &str) -> Option<FluentMessage>
390390
where
391391
R: Borrow<FluentResource>,
392392
{

fluent-bundle/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
//!
99
//! There are four core concepts to understand Fluent runtime:
1010
//!
11+
//! * [`FluentMessage`] - A single translation unit
12+
//! * [`FluentResource`] - A list of [`FluentMessage`] units
13+
//! * [`FluentBundle`] - A collection of [`FluentResource`] lists
14+
//! * [`FluentArgs`] - A list of [`FluentValue`] elements used to resolve a [`FluentMessage`] value
15+
//!
1116
//! # 1) Message
1217
//!
1318
//! [`FluentMessage`] is the core unit of the system.

fluent-bundle/src/message.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
1-
use fluent_syntax::{ast, parser::Slice};
1+
use fluent_syntax::ast;
22

33
#[derive(Debug, PartialEq)]
4-
pub struct FluentAttribute<'m, S> {
5-
pub id: &'m S,
6-
pub value: &'m ast::Pattern<S>,
4+
pub struct FluentAttribute<'m> {
5+
pub id: &'m str,
6+
pub value: &'m ast::Pattern<&'m str>,
77
}
88

9-
impl<'m, S> From<&'m ast::Attribute<S>> for FluentAttribute<'m, S> {
10-
fn from(attr: &'m ast::Attribute<S>) -> Self {
9+
impl<'m> From<&'m ast::Attribute<&'m str>> for FluentAttribute<'m> {
10+
fn from(attr: &'m ast::Attribute<&'m str>) -> Self {
1111
FluentAttribute {
12-
id: &attr.id.name,
12+
id: attr.id.name,
1313
value: &attr.value,
1414
}
1515
}
1616
}
17-
/// A single localization unit composed of an identifier,
18-
/// value, and attributes.
17+
18+
/// [`FluentMessage`] is a basic translation unit of the Fluent system.
19+
///
20+
/// A message is composed of a value and, optionally a list of attributes.
21+
///
22+
/// ### Simple Message
23+
///
24+
/// ```
25+
/// use fluent_bundle::{FluentResource, FluentBundle};
26+
///
27+
/// let source = r#"
28+
///
29+
/// hello-world = Hello, ${ user }
30+
///
31+
/// "#;
32+
///
33+
/// let resource = FluentResource::try_new(source.to_string())
34+
/// .expect("Failed to parse the resource.");
35+
///
36+
/// let mut bundle = FluentBundle::default();
37+
/// bundle.add_resource(resource)
38+
/// .expect("Failed to add a resource.");
39+
///
40+
/// let msg = bundle.get_message("hello-world")
41+
/// .expect("Failed to retrieve a message.");
42+
///
43+
/// assert!(msg.value.is_some());
44+
/// ```
45+
///
46+
/// ### Compound Message
47+
///
48+
/// ```text
49+
/// confirm-modal = Are you sure?
50+
/// .confirm = Yes
51+
/// .cancel = No
52+
/// .tooltip = Closing the window will lose all unsaved data.
53+
/// ```
1954
#[derive(Debug, PartialEq)]
20-
pub struct FluentMessage<'m, S> {
21-
pub value: Option<&'m ast::Pattern<S>>,
22-
pub attributes: Vec<FluentAttribute<'m, S>>,
55+
pub struct FluentMessage<'m> {
56+
pub value: Option<&'m ast::Pattern<&'m str>>,
57+
pub attributes: Vec<FluentAttribute<'m>>,
2358
}
2459

25-
impl<'m, S> FluentMessage<'m, S> {
26-
pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute<S>>
27-
where
28-
S: Slice<'m>,
29-
{
30-
self.attributes.iter().find(|attr| attr.id.as_ref() == key)
60+
impl<'m> FluentMessage<'m> {
61+
pub fn get_attribute(&self, key: &str) -> Option<&FluentAttribute> {
62+
self.attributes.iter().find(|attr| attr.id == key)
3163
}
3264
}
3365

34-
impl<'m, S> From<&'m ast::Message<S>> for FluentMessage<'m, S> {
35-
fn from(msg: &'m ast::Message<S>) -> Self {
66+
impl<'m> From<&'m ast::Message<&'m str>> for FluentMessage<'m> {
67+
fn from(msg: &'m ast::Message<&'m str>) -> Self {
3668
FluentMessage {
3769
value: msg.value.as_ref(),
3870
attributes: msg.attributes.iter().map(Into::into).collect(),

0 commit comments

Comments
 (0)