Skip to content

Commit c644a94

Browse files
committed
remove some clones
1 parent bd2b5f8 commit c644a94

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

src/audit/bot_conditions.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl BotConditions {
7474
args: exprs,
7575
}
7676
| Expr::Context(Context {
77-
full: _,
77+
raw: _,
7878
components: exprs,
7979
}) => exprs
8080
.iter()
@@ -132,10 +132,13 @@ impl BotConditions {
132132
}
133133

134134
fn bot_condition(expr: &str) -> Option<Confidence> {
135-
let Ok(expr) = (match ExplicitExpr::from_curly(expr) {
136-
Some(raw_expr) => Expr::parse(raw_expr.as_bare()),
137-
None => Expr::parse(expr),
138-
}) else {
135+
// TODO: Remove clones here.
136+
let bare = match ExplicitExpr::from_curly(expr) {
137+
Some(raw_expr) => raw_expr.as_bare().to_string(),
138+
None => expr.to_string(),
139+
};
140+
141+
let Ok(expr) = Expr::parse(&bare) else {
139142
tracing::warn!("couldn't parse expression: {expr}");
140143
return None;
141144
};

src/audit/overprovisioned_secrets.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ impl OverprovisionedSecrets {
7777
}
7878
}
7979
Expr::Index(expr) => results.extend(Self::secrets_expansions(expr)),
80-
Expr::Context(Context {
81-
full: _,
82-
components,
83-
}) => results.extend(components.iter().flat_map(Self::secrets_expansions)),
80+
Expr::Context(Context { raw: _, components }) => {
81+
results.extend(components.iter().flat_map(Self::secrets_expansions))
82+
}
8483
Expr::BinOp { lhs, op: _, rhs } => {
8584
results.extend(Self::secrets_expansions(lhs));
8685
results.extend(Self::secrets_expansions(rhs));

src/expr/mod.rs

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,42 @@ use pest_derive::Parser;
1111
struct ExprParser;
1212

1313
#[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>>,
1717
}
1818

19-
impl Context {
19+
impl<'src> Context<'src> {
2020
/// Creates a new `Context` from the given string.
2121
///
2222
/// Assumes that the string is a well-formed context string, and that
2323
/// 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 {
2525
// TODO: Use Rule::context to parse here instead?
2626
let components = ctx.split('.').map(Expr::ident).collect::<Vec<_>>();
2727

2828
Self {
29-
full: ctx.to_string(),
29+
raw: ctx,
3030
components,
3131
}
3232
}
3333

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 {
3535
Self {
36-
full: full.into(),
36+
raw,
3737
components: components.into(),
3838
}
3939
}
4040

4141
pub(crate) fn as_str(&self) -> &str {
42-
&self.full
42+
self.raw
4343
}
4444

4545
pub(crate) fn components(&self) -> &[Expr] {
4646
&self.components
4747
}
4848

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 {
5050
let parent = parent.into();
5151
let mut parent_components = parent.components().iter().peekable();
5252
let mut child_components = self.components().iter().peekable();
@@ -71,21 +71,21 @@ impl Context {
7171
}
7272
}
7373

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 {
7676
Context::from_str(val)
7777
}
7878
}
7979

80-
impl PartialEq for Context {
80+
impl PartialEq for Context<'_> {
8181
fn eq(&self, other: &Self) -> bool {
82-
self.full.eq_ignore_ascii_case(&other.full)
82+
self.raw.eq_ignore_ascii_case(other.raw)
8383
}
8484
}
8585

86-
impl PartialEq<str> for Context {
86+
impl PartialEq<str> for Context<'_> {
8787
fn eq(&self, other: &str) -> bool {
88-
self.full.eq_ignore_ascii_case(other)
88+
self.raw.eq_ignore_ascii_case(other)
8989
}
9090
}
9191

@@ -108,7 +108,7 @@ pub(crate) enum UnOp {
108108

109109
/// Represents a GitHub Actions expression.
110110
#[derive(Debug, PartialEq)]
111-
pub(crate) enum Expr {
111+
pub(crate) enum Expr<'src> {
112112
/// A number literal.
113113
Number(f64),
114114
/// A string literal.
@@ -120,34 +120,37 @@ pub(crate) enum Expr {
120120
/// The `*` literal within an index or context.
121121
Star,
122122
/// A function call.
123-
Call { func: String, args: Vec<Expr> },
123+
Call {
124+
func: &'src str,
125+
args: Vec<Expr<'src>>,
126+
},
124127
/// A context identifier component, e.g. `github` in `github.actor`.
125-
Identifier(String),
128+
Identifier(&'src str),
126129
/// A context index component, e.g. `[0]` in `foo[0]`.
127-
Index(Box<Expr>),
130+
Index(Box<Expr<'src>>),
128131
/// A full context reference.
129-
Context(Context),
132+
Context(Context<'src>),
130133
/// A binary operation, either logical or arithmetic.
131134
BinOp {
132-
lhs: Box<Expr>,
135+
lhs: Box<Expr<'src>>,
133136
op: BinOp,
134-
rhs: Box<Expr>,
137+
rhs: Box<Expr<'src>>,
135138
},
136139
/// A unary operation. Negation (`!`) is currently the only `UnOp`.
137-
UnOp { op: UnOp, expr: Box<Expr> },
140+
UnOp { op: UnOp, expr: Box<Expr<'src>> },
138141
}
139142

140-
impl Expr {
143+
impl<'src> Expr<'src> {
141144
/// Convenience API for making a boxed `Expr::String`.
142145
pub(crate) fn string(s: impl Into<String>) -> Box<Self> {
143146
Self::String(s.into()).into()
144147
}
145148

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)
148151
}
149152

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 {
151154
Self::Context(Context::new(r, components))
152155
}
153156

@@ -329,7 +332,7 @@ impl Expr {
329332
.collect::<Result<_, _>>()?;
330333

331334
Ok(Expr::Call {
332-
func: identifier.as_str().into(),
335+
func: identifier.as_str(),
333336
args,
334337
}
335338
.into())
@@ -339,7 +342,7 @@ impl Expr {
339342
Ok(Expr::Index(parse_pair(pair.into_inner().next().unwrap())?).into())
340343
}
341344
Rule::context => {
342-
let raw = pair.as_str().to_string();
345+
let raw = pair.as_str();
343346
let pairs = pair.into_inner();
344347

345348
let mut inner: Vec<Expr> = pairs

0 commit comments

Comments
 (0)