11
11
//! [`resolve`]: ../resolve
12
12
//! [`FluentBundle::format`]: ../bundle/struct.FluentBundle.html#method.format
13
13
14
- use std:: borrow:: Borrow ;
15
- use std:: borrow :: Cow ;
14
+ use std:: borrow:: { Borrow , Cow } ;
15
+ use std:: fmt ;
16
16
use std:: str:: FromStr ;
17
17
18
18
use fluent_syntax:: ast;
@@ -21,52 +21,65 @@ use intl_pluralrules::PluralCategory;
21
21
use crate :: resolve:: Scope ;
22
22
use crate :: resource:: FluentResource ;
23
23
24
- #[ derive( Debug , PartialEq , Clone ) ]
24
+ #[ derive( Debug , PartialEq , Eq , Copy , Clone , Hash ) ]
25
25
pub enum DisplayableNodeType {
26
26
Message ,
27
27
Term ,
28
28
Variable ,
29
29
Function ,
30
30
}
31
31
32
- #[ derive( Debug , PartialEq , Clone ) ]
32
+ #[ derive( Debug , PartialEq , Eq , Copy , Clone , Hash ) ]
33
33
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 > ,
37
37
}
38
38
39
39
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 {
46
41
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 ) ,
51
46
}
52
47
}
48
+ }
53
49
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) ?,
60
57
} ;
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
+ }
63
72
}
73
+ }
64
74
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 {
66
79
DisplayableNode {
67
80
node_type : DisplayableNodeType :: Message ,
68
- id,
69
- attribute,
81
+ id : input . 0 . id . name ,
82
+ attribute : Some ( input . 1 . id . name ) ,
70
83
}
71
84
}
72
85
}
@@ -121,13 +134,18 @@ pub enum FluentValue<'source> {
121
134
122
135
impl < ' source > FluentValue < ' source > {
123
136
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 ( ) ) ,
127
141
}
128
142
}
129
143
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 {
131
149
match ( self , other) {
132
150
( & FluentValue :: String ( ref a) , & FluentValue :: String ( ref b) ) => a == b,
133
151
( & FluentValue :: Number ( ref a) , & FluentValue :: Number ( ref b) ) => a == b,
@@ -152,21 +170,26 @@ impl<'source> FluentValue<'source> {
152
170
match self {
153
171
FluentValue :: String ( s) => s. clone ( ) ,
154
172
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 ( ) ,
157
175
}
158
176
}
159
177
}
160
178
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
+ }
164
187
}
165
188
}
166
189
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 ( ) )
170
193
}
171
194
}
172
195
0 commit comments