pub enum Expr<'src> {
Literal(Literal<'src>),
Star,
Call(Call<'src>),
Identifier(Identifier<'src>),
Index(Box<SpannedExpr<'src>>),
Context(Context<'src>),
BinExpr(BinExpr<'src>),
UnExpr {
op: UnOp,
expr: Box<SpannedExpr<'src>>,
},
}Expand description
Represents a GitHub Actions expression.
Variants§
Literal(Literal<'src>)
A literal value.
Star
The * literal within an index or context.
Call(Call<'src>)
A function call.
Identifier(Identifier<'src>)
A context identifier component, e.g. github in github.actor.
Index(Box<SpannedExpr<'src>>)
A context index component, e.g. [0] in foo[0].
Context(Context<'src>)
A full context reference.
BinExpr(BinExpr<'src>)
A binary expression, either logical or arithmetic.
UnExpr
A unary expression. Negation (!) is currently the only UnOp.
Fields
expr: Box<SpannedExpr<'src>>The expression to apply the operator to.
Implementations§
Source§impl<'src> Expr<'src>
impl<'src> Expr<'src>
Sourcepub fn is_literal(&self) -> bool
pub fn is_literal(&self) -> bool
Returns whether the expression is a literal.
Sourcepub fn constant_reducible(&self) -> bool
pub fn constant_reducible(&self) -> bool
Returns whether the expression is constant reducible.
“Constant reducible” is similar to “constant foldable” but with
meta-evaluation semantics: the expression 5 would not be
constant foldable in a normal program (because it’s already
an atom), but is “constant reducible” in a GitHub Actions expression
because an expression containing it (e.g. ${{ 5 }}) can be elided
entirely and replaced with 5.
There are three kinds of reducible expressions:
- Literals, which reduce to their literal value;
- Binops/unops with reducible subexpressions, which reduce to their evaluation;
- Select function calls where the semantics of the function mean that reducible arguments make the call itself reducible.
NOTE: This implementation is sound but not complete.
Sourcepub fn parse(expr: &'src str) -> Result<SpannedExpr<'src>, Error>
pub fn parse(expr: &'src str) -> Result<SpannedExpr<'src>, Error>
Parses the given string into an expression.
Sourcepub fn commutative_matches(&self, other: &Expr<'src>) -> bool
pub fn commutative_matches(&self, other: &Expr<'src>) -> bool
Returns whether this expression ‘commutatively matches’ the given expression.
For most expressions, this is the same as equivalence (i.e. ==).
For binary expressions that are also commutative, this takes commutivity into account.
For example, a == b is considered to match b == a, but a > b is not
considered to match b > a. This check is recusive, i.e. two nested binary expressions
will be fully checked for commutative equivalence.
Source§impl<'src> Expr<'src>
impl<'src> Expr<'src>
Sourcepub fn consteval(&self) -> Option<Evaluation>
pub fn consteval(&self) -> Option<Evaluation>
Evaluates a constant-reducible expression to its literal value.
Returns Some(Evaluation) if the expression can be constant-evaluated,
or None if the expression contains non-constant elements (like contexts or
non-reducible function calls).
This implementation follows GitHub Actions’ evaluation semantics as documented at: https://docs.github.com/en/actions/reference/workflows-and-actions/expressions
§Examples
use github_actions_expressions::{Expr, Evaluation};
let expr = Expr::parse("'hello'").unwrap();
let result = expr.consteval().unwrap();
assert_eq!(result.sema().to_string(), "hello");
let expr = Expr::parse("true && false").unwrap();
let result = expr.consteval().unwrap();
assert_eq!(result, Evaluation::Boolean(false));