@@ -4,15 +4,17 @@ use crate::resolver::{ResolveValue, ResolverError, WriteValue};
44use crate :: types:: FluentValue ;
55use crate :: { FluentArgs , FluentError , FluentResource } ;
66use fluent_syntax:: ast;
7- use std:: borrow:: Borrow ;
7+ use std:: borrow:: { Borrow , Cow } ;
88use std:: fmt;
9+ use std:: marker:: PhantomData ;
910
1011/// State for a single `ResolveValue::to_value` call.
11- pub struct Scope < ' bundle , ' ast , ' args , ' errors , R , M > {
12+ pub struct Scope < ' bundle , ' ast , ' args , ' errors , R , M , Args > {
13+ lt : PhantomData < & ' args ( ) > ,
1214 /// The current `FluentBundle` instance.
1315 pub bundle : & ' bundle FluentBundle < R , M > ,
1416 /// The current arguments passed by the developer.
15- pub ( super ) args : Option < & ' args FluentArgs < ' args > > ,
17+ pub ( super ) args : Args ,
1618 /// Local args
1719 pub ( super ) local_args : Option < FluentArgs < ' bundle > > ,
1820 /// The running count of resolved placeables. Used to detect the Billion
@@ -26,15 +28,16 @@ pub struct Scope<'bundle, 'ast, 'args, 'errors, R, M> {
2628 pub dirty : bool ,
2729}
2830
29- impl < ' bundle , ' ast , ' args , ' errors , R , M > Scope < ' bundle , ' ast , ' args , ' errors , R , M > {
31+ impl < ' bundle , ' ast , ' args , ' errors , R , M , Args > Scope < ' bundle , ' ast , ' args , ' errors , R , M , Args > {
3032 pub fn new (
3133 bundle : & ' bundle FluentBundle < R , M > ,
32- args : Option < & ' args FluentArgs > ,
34+ arg_resolver : Args ,
3335 errors : Option < & ' errors mut Vec < FluentError > > ,
3436 ) -> Self {
3537 Scope {
38+ lt : PhantomData ,
3639 bundle,
37- args,
40+ args : arg_resolver ,
3841 local_args : None ,
3942 placeables : 0 ,
4043 travelled : Default :: default ( ) ,
@@ -48,7 +51,11 @@ impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R
4851 errors. push ( error. into ( ) ) ;
4952 }
5053 }
54+ }
5155
56+ impl < ' bundle , ' ast , ' args , ' errors , R , M , Args : ArgumentResolver < ' args > >
57+ Scope < ' bundle , ' ast , ' args , ' errors , R , M , Args >
58+ {
5259 /// This method allows us to lazily add Pattern on the stack, only if the
5360 /// Pattern::resolve has been called on an empty stack.
5461 ///
@@ -138,3 +145,14 @@ impl<'bundle, 'ast, 'args, 'errors, R, M> Scope<'bundle, 'ast, 'args, 'errors, R
138145 }
139146 }
140147}
148+
149+ pub trait ArgumentResolver < ' a > : Copy {
150+ fn resolve ( self , name : & str ) -> Option < Cow < FluentValue < ' a > > > ;
151+ }
152+
153+ impl < ' args > ArgumentResolver < ' args > for Option < & ' args FluentArgs < ' args > > {
154+ fn resolve ( self , name : & str ) -> Option < Cow < FluentValue < ' args > > > {
155+ let arg = self ?. get ( name) ?;
156+ Some ( Cow :: Borrowed ( arg) )
157+ }
158+ }
0 commit comments