Skip to content

Commit

Permalink
locate: switch to yamlpath::QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
woodruffw committed Sep 9, 2024
1 parent 62f1847 commit e9981fa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ serde = { version = "1.0.208", features = ["derive"] }
serde-sarif = "0.6.5"
serde_json = "1.0.125"
serde_yaml = "0.9.34"
yamlpath = "0.3.0"
yamlpath = "0.4.0"
24 changes: 8 additions & 16 deletions src/finding/locate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,26 @@ impl Locator {
workflow: &'w Workflow,
location: &WorkflowLocation,
) -> Result<Feature<'w>> {
let mut path = vec![];
let mut builder = yamlpath::QueryBuilder::new();

if let Some(job) = &location.job {
path.extend([
yamlpath::Component::Key("jobs".into()),
yamlpath::Component::Key(job.id.into()),
]);
builder = builder.key("jobs").key(job.id);

if let Some(step) = &job.step {
path.extend([
yamlpath::Component::Key("steps".into()),
yamlpath::Component::Index(step.index),
]);
builder = builder.key("steps").index(step.index);
} else if let Some(key) = &job.key {
path.push(yamlpath::Component::Key(key.to_string()));
builder = builder.key(*key);
}
} else {
// Non-job top-level key.
path.push(yamlpath::Component::Key(
builder = builder.key(
location
.key
.expect("API misuse: must provide key if job is not specified")
.to_string(),
));
.expect("API misuse: must provide key if job is not specified"),
);
}

// Infallible: we always have at least one path component above.
let query = yamlpath::Query::new(path).unwrap();
let query = builder.build();
let feature = workflow.document.query(&query)?;

Ok(Feature {
Expand Down

0 comments on commit e9981fa

Please sign in to comment.