@@ -11,42 +11,42 @@ use pest_derive::Parser;
11
11
struct ExprParser ;
12
12
13
13
#[ derive( Debug ) ]
14
- pub ( crate ) struct Context {
15
- pub ( crate ) full : String ,
16
- pub ( crate ) components : Vec < Expr > ,
14
+ pub ( crate ) struct Context < ' src > {
15
+ pub ( crate ) raw : & ' src str ,
16
+ pub ( crate ) components : Vec < Expr < ' src > > ,
17
17
}
18
18
19
- impl Context {
19
+ impl < ' src > Context < ' src > {
20
20
/// Creates a new `Context` from the given string.
21
21
///
22
22
/// Assumes that the string is a well-formed context string, and that
23
23
/// it's a "bare" context string, i.e. doesn't begin with a function call.
24
- pub ( crate ) fn from_str ( ctx : & str ) -> Self {
24
+ pub ( crate ) fn from_str ( ctx : & ' src str ) -> Self {
25
25
// TODO: Use Rule::context to parse here instead?
26
26
let components = ctx. split ( '.' ) . map ( Expr :: ident) . collect :: < Vec < _ > > ( ) ;
27
27
28
28
Self {
29
- full : ctx. to_string ( ) ,
29
+ raw : ctx,
30
30
components,
31
31
}
32
32
}
33
33
34
- pub ( crate ) fn new ( full : impl Into < String > , components : impl Into < Vec < Expr > > ) -> Self {
34
+ pub ( crate ) fn new ( raw : & ' src str , components : impl Into < Vec < Expr < ' src > > > ) -> Self {
35
35
Self {
36
- full : full . into ( ) ,
36
+ raw ,
37
37
components : components. into ( ) ,
38
38
}
39
39
}
40
40
41
41
pub ( crate ) fn as_str ( & self ) -> & str {
42
- & self . full
42
+ self . raw
43
43
}
44
44
45
45
pub ( crate ) fn components ( & self ) -> & [ Expr ] {
46
46
& self . components
47
47
}
48
48
49
- pub ( crate ) fn child_of ( & self , parent : impl Into < Context > ) -> bool {
49
+ pub ( crate ) fn child_of ( & self , parent : impl Into < Context < ' src > > ) -> bool {
50
50
let parent = parent. into ( ) ;
51
51
let mut parent_components = parent. components ( ) . iter ( ) . peekable ( ) ;
52
52
let mut child_components = self . components ( ) . iter ( ) . peekable ( ) ;
@@ -71,21 +71,21 @@ impl Context {
71
71
}
72
72
}
73
73
74
- impl From < & str > for Context {
75
- fn from ( val : & str ) -> Self {
74
+ impl < ' a > From < & ' a str > for Context < ' a > {
75
+ fn from ( val : & ' a str ) -> Self {
76
76
Context :: from_str ( val)
77
77
}
78
78
}
79
79
80
- impl PartialEq for Context {
80
+ impl PartialEq for Context < ' _ > {
81
81
fn eq ( & self , other : & Self ) -> bool {
82
- self . full . eq_ignore_ascii_case ( & other. full )
82
+ self . raw . eq_ignore_ascii_case ( other. raw )
83
83
}
84
84
}
85
85
86
- impl PartialEq < str > for Context {
86
+ impl PartialEq < str > for Context < ' _ > {
87
87
fn eq ( & self , other : & str ) -> bool {
88
- self . full . eq_ignore_ascii_case ( other)
88
+ self . raw . eq_ignore_ascii_case ( other)
89
89
}
90
90
}
91
91
@@ -108,7 +108,7 @@ pub(crate) enum UnOp {
108
108
109
109
/// Represents a GitHub Actions expression.
110
110
#[ derive( Debug , PartialEq ) ]
111
- pub ( crate ) enum Expr {
111
+ pub ( crate ) enum Expr < ' src > {
112
112
/// A number literal.
113
113
Number ( f64 ) ,
114
114
/// A string literal.
@@ -120,34 +120,37 @@ pub(crate) enum Expr {
120
120
/// The `*` literal within an index or context.
121
121
Star ,
122
122
/// A function call.
123
- Call { func : String , args : Vec < Expr > } ,
123
+ Call {
124
+ func : & ' src str ,
125
+ args : Vec < Expr < ' src > > ,
126
+ } ,
124
127
/// A context identifier component, e.g. `github` in `github.actor`.
125
- Identifier ( String ) ,
128
+ Identifier ( & ' src str ) ,
126
129
/// A context index component, e.g. `[0]` in `foo[0]`.
127
- Index ( Box < Expr > ) ,
130
+ Index ( Box < Expr < ' src > > ) ,
128
131
/// A full context reference.
129
- Context ( Context ) ,
132
+ Context ( Context < ' src > ) ,
130
133
/// A binary operation, either logical or arithmetic.
131
134
BinOp {
132
- lhs : Box < Expr > ,
135
+ lhs : Box < Expr < ' src > > ,
133
136
op : BinOp ,
134
- rhs : Box < Expr > ,
137
+ rhs : Box < Expr < ' src > > ,
135
138
} ,
136
139
/// A unary operation. Negation (`!`) is currently the only `UnOp`.
137
- UnOp { op : UnOp , expr : Box < Expr > } ,
140
+ UnOp { op : UnOp , expr : Box < Expr < ' src > > } ,
138
141
}
139
142
140
- impl Expr {
143
+ impl < ' src > Expr < ' src > {
141
144
/// Convenience API for making a boxed `Expr::String`.
142
145
pub ( crate ) fn string ( s : impl Into < String > ) -> Box < Self > {
143
146
Self :: String ( s. into ( ) ) . into ( )
144
147
}
145
148
146
- pub ( crate ) fn ident ( i : impl Into < String > ) -> Self {
147
- Self :: Identifier ( i. into ( ) )
149
+ pub ( crate ) fn ident ( i : & ' src str ) -> Self {
150
+ Self :: Identifier ( i)
148
151
}
149
152
150
- pub ( crate ) fn context ( r : impl Into < String > , components : impl Into < Vec < Expr > > ) -> Self {
153
+ pub ( crate ) fn context ( r : & ' src str , components : impl Into < Vec < Expr < ' src > > > ) -> Self {
151
154
Self :: Context ( Context :: new ( r, components) )
152
155
}
153
156
@@ -329,7 +332,7 @@ impl Expr {
329
332
. collect :: < Result < _ , _ > > ( ) ?;
330
333
331
334
Ok ( Expr :: Call {
332
- func : identifier. as_str ( ) . into ( ) ,
335
+ func : identifier. as_str ( ) ,
333
336
args,
334
337
}
335
338
. into ( ) )
@@ -339,7 +342,7 @@ impl Expr {
339
342
Ok ( Expr :: Index ( parse_pair ( pair. into_inner ( ) . next ( ) . unwrap ( ) ) ?) . into ( ) )
340
343
}
341
344
Rule :: context => {
342
- let raw = pair. as_str ( ) . to_string ( ) ;
345
+ let raw = pair. as_str ( ) ;
343
346
let pairs = pair. into_inner ( ) ;
344
347
345
348
let mut inner: Vec < Expr > = pairs
0 commit comments