1
- //! `Entry` is used to store Messages, Terms and Functions in `FluentBundle` instances.
1
+ //! `Entry` is used to store the lookup information for Messages, Terms and Functions in
2
+ //! `FluentBundle` instances.
2
3
3
4
use std:: borrow:: Borrow ;
4
5
@@ -12,24 +13,34 @@ use crate::types::FluentValue;
12
13
pub type FluentFunction =
13
14
Box < dyn for < ' a > Fn ( & [ FluentValue < ' a > ] , & FluentArgs ) -> FluentValue < ' a > + Send + Sync > ;
14
15
16
+ type ResourceIdx = usize ;
17
+ type EntryIdx = usize ;
18
+
19
+ /// The [`Entry`] stores indexes into the [`FluentBundle`]'s resources for Messages and Terms,
20
+ /// and owns the [`Box`] pointers to the [`FluentFunction`].
15
21
pub enum Entry {
16
- Message ( ( usize , usize ) ) ,
17
- Term ( ( usize , usize ) ) ,
22
+ Message ( ( ResourceIdx , EntryIdx ) ) ,
23
+ Term ( ( ResourceIdx , EntryIdx ) ) ,
18
24
Function ( FluentFunction ) ,
19
25
}
20
26
21
27
pub trait GetEntry {
28
+ /// Looks up a message by its string ID, and returns it if it exists.
22
29
fn get_entry_message ( & self , id : & str ) -> Option < & ast:: Message < & str > > ;
30
+
31
+ /// Looks up a term by its string ID, and returns it if it exists.
23
32
fn get_entry_term ( & self , id : & str ) -> Option < & ast:: Term < & str > > ;
33
+
34
+ /// Looks up a function by its string ID, and returns it if it exists.
24
35
fn get_entry_function ( & self , id : & str ) -> Option < & FluentFunction > ;
25
36
}
26
37
27
38
impl < ' bundle , R : Borrow < FluentResource > , M > GetEntry for FluentBundle < R , M > {
28
39
fn get_entry_message ( & self , id : & str ) -> Option < & ast:: Message < & str > > {
29
40
self . entries . get ( id) . and_then ( |ref entry| match entry {
30
- Entry :: Message ( pos ) => {
31
- let res = self . resources . get ( pos . 0 ) ?. borrow ( ) ;
32
- if let ast:: Entry :: Message ( ref msg) = res. get_entry ( pos . 1 ) ? {
41
+ Entry :: Message ( ( resource_idx , entry_idx ) ) => {
42
+ let res = self . resources . get ( * resource_idx ) ?. borrow ( ) ;
43
+ if let ast:: Entry :: Message ( ref msg) = res. get_entry ( * entry_idx ) ? {
33
44
Some ( msg)
34
45
} else {
35
46
None
@@ -41,9 +52,9 @@ impl<'bundle, R: Borrow<FluentResource>, M> GetEntry for FluentBundle<R, M> {
41
52
42
53
fn get_entry_term ( & self , id : & str ) -> Option < & ast:: Term < & str > > {
43
54
self . entries . get ( id) . and_then ( |ref entry| match entry {
44
- Entry :: Term ( pos ) => {
45
- let res = self . resources . get ( pos . 0 ) ?. borrow ( ) ;
46
- if let ast:: Entry :: Term ( ref msg) = res. get_entry ( pos . 1 ) ? {
55
+ Entry :: Term ( ( resource_idx , entry_idx ) ) => {
56
+ let res = self . resources . get ( * resource_idx ) ?. borrow ( ) ;
57
+ if let ast:: Entry :: Term ( ref msg) = res. get_entry ( * entry_idx ) ? {
47
58
Some ( msg)
48
59
} else {
49
60
None
0 commit comments