From f0e849bdd74b5a1667a9c4074f10aeed4a0dc067 Mon Sep 17 00:00:00 2001 From: jonmcquillan Date: Fri, 1 Sep 2023 09:05:57 -0600 Subject: [PATCH] Update to github actions and cargo fmt (#5) --- .circleci/config.yml | 49 -- .github/dependabot.yml | 18 +- .github/workflows/release.yml | 27 + .github/workflows/test.yml | 22 + Cargo.toml | 5 +- README.md | 3 +- examples/label_drop.rs | 76 +-- examples/label_passthrough.rs | 26 +- examples/parse.rs | 20 +- examples/reformat.rs | 20 +- examples/return_value.rs | 20 +- src/lib.rs | 5 +- src/parser.rs | 803 +++++++++++++++--------------- src/types/expression.rs | 180 +++---- src/types/function.rs | 594 +++++++++++----------- src/types/group.rs | 95 ++-- src/types/misc.rs | 178 +++---- src/types/operator.rs | 616 ++++++++++++----------- src/types/return_value.rs | 315 ++++++------ src/types/selector.rs | 412 ++++++++-------- tests/label_passthrough.rs | 292 ++++++----- tests/simple.rs | 906 ++++++++++++++++++---------------- tests/subquery.rs | 294 +++++------ 23 files changed, 2536 insertions(+), 2440 deletions(-) delete mode 100644 .circleci/config.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml diff --git a/.circleci/config.yml b/.circleci/config.yml deleted file mode 100644 index 0c60028..0000000 --- a/.circleci/config.yml +++ /dev/null @@ -1,49 +0,0 @@ -version: 2.1 - -jobs: - test: - docker: - - image: rust:1.56-slim-buster - steps: - - checkout - - run: - name: patch crate version - command: | - if [[ -n "$CIRCLE_TAG" ]]; then - version=$(echo "$CIRCLE_TAG" | sed -e 's/^v//') - sed -i -e "s/version = \"0.1.0\"/version = \"$version\"/" Cargo.toml - fi - - run: - name: cargo test - command: cargo test - - publish: - docker: - - image: rust:1.56-slim-buster - steps: - - checkout - - run: - name: patch crate version - command: | - version=$(echo "$CIRCLE_TAG" | sed -e 's/^v//') - sed -i -e "s/version = \"0.1.0\"/version = \"$version\"/" Cargo.toml - - run: - name: cargo publish - command: cargo publish --allow-dirty --locked - -workflows: - version: 2 - build-and-publish: - jobs: - - test: - filters: - tags: - only: /.*/ - - publish: - requires: - - test - filters: - branches: - ignore: /.*/ - tags: - only: /.*/ diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 2f3bc79..834f422 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,8 +1,14 @@ version: 2 updates: -- package-ecosystem: cargo - open-pull-requests-limit: 25 - directory: / - registries: [] - schedule: - interval: daily + - package-ecosystem: cargo + open-pull-requests-limit: 25 + directory: / + registries: [] + schedule: + interval: daily + - package-ecosystem: github-actions + directory: / + registries: [] + schedule: + interval: daily + diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..8b13679 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,27 @@ +# (c) Copyright 2023 Hewlett Packard Enterprise Development LP + +name: Release + +on: + release: + types: [created] + +jobs: + upload_artifacts: + runs-on: ubuntu-latest + env: + CARGO_TERM_COLOR: always + + steps: + - name: Checkout + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - name: Update Tag + shell: bash + run: | + version=$(echo "$GITHUB_REF" | sed -e 's/^v//') + sed -i -e "s/version = \"0.1.0\"/version = \"$version\"/" Cargo.toml + - name: Publish + run: cargo publish --allow-dirty --locked + env: + CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} + diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..279c791 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,22 @@ +# (c) Copyright 2023 Hewlett Packard Enterprise Development LP + +name: Rust Checks + +on: + pull_request: + +jobs: + build: + name: Rust Checks + runs-on: ubuntu-latest + env: + CARGO_TERM_COLOR: always + steps: + - name: Checkout + uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744 + - name: Rust Format + run: cargo fmt --check + - name: Clippy + run: cargo clippy + - name: Test + run: cargo test --verbose diff --git a/Cargo.toml b/Cargo.toml index 320dfd1..cc617a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,16 +7,13 @@ license = "MIT" keywords = ["parser", "prometheus", "pest"] homepage = "https://github.com/HewlettPackard/prometheus-parser-rs/" repository = "https://github.com/HewlettPackard/prometheus-parser-rs/" -exclude = [".circleci/**/*"] +exclude = [".github/**/*"] readme = "README.md" # NOTE: do not change the version here, it will be replaced with the git tag # version by CI version = "0.1.0" -[badges] -circle-ci = { repository = "HewlettPackard/prometheus-parser-rs", branch = "master" } - [dependencies] pest = "2.1" pest_consume = "1.1" diff --git a/README.md b/README.md index 0513568..ff8ee17 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # prometheus-parser-rs -[![CircleCI](https://circleci.com/gh/HewlettPackard/prometheus-parser-rs.svg?style=svg)](https://circleci.com/gh/HewlettPackard/prometheus-parser-rs) [![docs.rs](https://docs.rs/prometheus-parser/badge.svg)](https://docs.rs/prometheus-parser/) prometheus-parser parses arbitrary Prometheus expressions into a syntax tree @@ -225,7 +224,7 @@ though the [code of conduct] for some pointers to get started. Note that - as mentioned in the code of conduct - code contributions must indicate that you accept the [Developer Certificate of Origin][dco], -essentially asserting you have the necessary rights to submit the code you're +essentially asserting you have the necessary rights to submit the code you're contributing under the project's license (MIT). If you agree, simply pass `-s` to `git commit`: diff --git a/examples/label_drop.rs b/examples/label_drop.rs index 4df2add..56f9595 100644 --- a/examples/label_drop.rs +++ b/examples/label_drop.rs @@ -7,43 +7,43 @@ use prometheus_parser; use prometheus_parser::ReturnKind; fn main() { - let args: Vec = env::args().collect(); - - let expr = &args[1]; - let label = &args[2]; - let res = prometheus_parser::parse_expr(expr); - - match res { - Ok(r) => { - let return_value = r.return_value(); - - if let ReturnKind::Unknown(cause) = &return_value.kind { - println!("note: expression return type is unknown, result may be inaccurate"); - println!(" reason: {}", cause.message); - println!(" expression: {}", cause.expression); - println!(); - } - - if let Some(drops) = return_value.drops(label) { - println!("label '{}' is dropped:\n", label); - - if let Some(span) = drops.span { - println!("{}", expr); - println!( - "{}{}", - " ".repeat(span.start), - "-".repeat(span.end - span.start) - ); + let args: Vec = env::args().collect(); + + let expr = &args[1]; + let label = &args[2]; + let res = prometheus_parser::parse_expr(expr); + + match res { + Ok(r) => { + let return_value = r.return_value(); + + if let ReturnKind::Unknown(cause) = &return_value.kind { + println!("note: expression return type is unknown, result may be inaccurate"); + println!(" reason: {}", cause.message); + println!(" expression: {}", cause.expression); + println!(); + } + + if let Some(drops) = return_value.drops(label) { + println!("label '{}' is dropped:\n", label); + + if let Some(span) = drops.span { + println!("{}", expr); + println!( + "{}{}", + " ".repeat(span.start), + "-".repeat(span.end - span.start) + ); + } + + println!(); + println!("parent expression: {}", drops.expression); + } else { + println!("label '{}' is not dropped", label); + } } - - println!(); - println!("parent expression: {}", drops.expression); - } else { - println!("label '{}' is not dropped", label); - } - }, - Err(e) => { - eprintln!("error: {}", e); - } - }; + Err(e) => { + eprintln!("error: {}", e); + } + }; } diff --git a/examples/label_passthrough.rs b/examples/label_passthrough.rs index 87c6db1..ffc645d 100644 --- a/examples/label_passthrough.rs +++ b/examples/label_passthrough.rs @@ -5,19 +5,19 @@ use std::env; use prometheus_parser; fn main() { - let args: Vec = env::args().collect(); + let args: Vec = env::args().collect(); - let expr = &args[1]; - let labels: Vec<&str> = args[2..].iter().map(|s| s.as_str()).collect(); - let res = prometheus_parser::parse_expr(expr); + let expr = &args[1]; + let labels: Vec<&str> = args[2..].iter().map(|s| s.as_str()).collect(); + let res = prometheus_parser::parse_expr(expr); - match res { - Ok(r) => { - let return_value = r.return_value(); - println!("{:#?}", return_value.passthrough(&labels)); - }, - Err(e) => { - eprintln!("error: {}", e); - } - }; + match res { + Ok(r) => { + let return_value = r.return_value(); + println!("{:#?}", return_value.passthrough(&labels)); + } + Err(e) => { + eprintln!("error: {}", e); + } + }; } diff --git a/examples/parse.rs b/examples/parse.rs index b437f4b..218ec9c 100644 --- a/examples/parse.rs +++ b/examples/parse.rs @@ -5,15 +5,15 @@ use std::env; use prometheus_parser; fn main() { - let args: Vec = env::args().collect(); - let res = prometheus_parser::parse_expr(&args[1]); + let args: Vec = env::args().collect(); + let res = prometheus_parser::parse_expr(&args[1]); - match res { - Ok(r) => { - println!("{:#?}", r); - }, - Err(e) => { - eprintln!("error: {}", e); - } - }; + match res { + Ok(r) => { + println!("{:#?}", r); + } + Err(e) => { + eprintln!("error: {}", e); + } + }; } diff --git a/examples/reformat.rs b/examples/reformat.rs index e518df6..6ca38a3 100644 --- a/examples/reformat.rs +++ b/examples/reformat.rs @@ -5,15 +5,15 @@ use std::env; use prometheus_parser; fn main() { - let args: Vec = env::args().collect(); - let res = prometheus_parser::parse_expr(&args[1]); + let args: Vec = env::args().collect(); + let res = prometheus_parser::parse_expr(&args[1]); - match res { - Ok(r) => { - println!("{}", r); - }, - Err(e) => { - eprintln!("error: {}", e); - } - }; + match res { + Ok(r) => { + println!("{}", r); + } + Err(e) => { + eprintln!("error: {}", e); + } + }; } diff --git a/examples/return_value.rs b/examples/return_value.rs index 5db91cd..4705b60 100644 --- a/examples/return_value.rs +++ b/examples/return_value.rs @@ -5,15 +5,15 @@ use std::env; use prometheus_parser; fn main() { - let args: Vec = env::args().collect(); - let res = prometheus_parser::parse_expr(&args[1]); + let args: Vec = env::args().collect(); + let res = prometheus_parser::parse_expr(&args[1]); - match res { - Ok(r) => { - println!("{:#?}", r.return_value()); - }, - Err(e) => { - eprintln!("error: {}", e); - } - }; + match res { + Ok(r) => { + println!("{:#?}", r.return_value()); + } + Err(e) => { + eprintln!("error: {}", e); + } + }; } diff --git a/src/lib.rs b/src/lib.rs index 0b594d0..10c79fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,9 @@ - // (C) Copyright 2019-2020 Hewlett Packard Enterprise Development LP #![forbid(unsafe_code)] -mod types; mod parser; +mod types; -pub use types::*; pub use parser::{parse_expr, Result}; +pub use types::*; diff --git a/src/parser.rs b/src/parser.rs index cadbfcb..bcb1be0 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2,11 +2,11 @@ use std::f64; +use enquote::unquote; use lazy_static::lazy_static; use pest::prec_climber as pcl; use pest::prec_climber::PrecClimber; use pest_consume::{match_nodes, Error, Parser}; -use enquote::unquote; use crate::types::*; @@ -14,37 +14,35 @@ pub type Result = std::result::Result>; pub(crate) type Node<'i> = pest_consume::Node<'i, Rule, ()>; lazy_static! { - static ref PRECCLIMBER: PrecClimber = PrecClimber::new( - vec![ - pcl::Operator::new(Rule::op_or, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_unless, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_and, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_greater_than, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_greater_than_equal, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_less_than, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_less_than_equal, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_not_equal, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_equal, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_subtract, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_add, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_modulo, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_divide, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_multiply, pcl::Assoc::Left), - pcl::Operator::new(Rule::op_power, pcl::Assoc::Right) - ] - ); + static ref PRECCLIMBER: PrecClimber = PrecClimber::new(vec![ + pcl::Operator::new(Rule::op_or, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_unless, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_and, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_greater_than, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_greater_than_equal, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_less_than, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_less_than_equal, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_not_equal, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_equal, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_subtract, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_add, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_modulo, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_divide, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_multiply, pcl::Assoc::Left), + pcl::Operator::new(Rule::op_power, pcl::Assoc::Right) + ]); } /// An intermediate struct for selectors with a named metric. /// Label-only selectors may evaluate immediately to a Selector struct MetricSelectorIntermediate { - metric: String, - labels: Vec