From e056b1d25acf1bd8db601700e12472cc0ee75c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Wed, 14 Aug 2024 11:48:51 +0200 Subject: [PATCH] Add rustfmt config --- .rustfmt.toml | 10 ++++++++++ shell.nix | 6 ++++-- src/main.rs | 48 +++++++++++++++++------------------------------- 3 files changed, 31 insertions(+), 33 deletions(-) create mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..bfe7014 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,10 @@ +edition = "2021" +condense_wildcard_suffixes = true +format_code_in_doc_comments = true +group_imports = "StdExternalCrate" +imports_granularity = "Module" +match_block_trailing_comma = true +max_width = 120 +overflow_delimited_expr = true +reorder_impl_items = true +struct_field_align_threshold = 20 diff --git a/shell.nix b/shell.nix index 996a810..1c0f861 100644 --- a/shell.nix +++ b/shell.nix @@ -2,14 +2,16 @@ with pkgs; -mkShell { +let + fenix = import (fetchTarball "https://github.com/nix-community/fenix/archive/monthly.tar.gz") { }; +in mkShell { nativeBuildInputs = [ cargo cargo-deny clippy pkg-config rustc - rustfmt + fenix.default.rustfmt # keep this line if you use bash bashInteractive diff --git a/src/main.rs b/src/main.rs index 3146483..f54947c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,19 @@ mod changes; mod images; +use std::sync::Arc; +use std::{env, str}; + use anyhow::{anyhow, Context}; use changes::{Change, ChangeCommit, RepoChange}; -use clap::{builder::styling::Style, Parser, Subcommand}; +use clap::builder::styling::Style; +use clap::{Parser, Subcommand}; use git2::Repository; use images::ContainerImages; use octocrab::commits::PullRequestTarget; -use octocrab::models::{pulls, pulls::ReviewState}; +use octocrab::models::pulls; +use octocrab::models::pulls::ReviewState; use octocrab::Octocrab; -use std::{env, str, sync::Arc}; const BOLD_UNDERLINE: Style = Style::new().bold().underline(); @@ -93,17 +97,15 @@ async fn main() -> Result<(), anyhow::Error> { head_commit: cli.head, changes: Vec::new(), }; - find_reviews(&octocrab, repo) - .await - .context("while finding reviews")?; + find_reviews(&octocrab, repo).await.context("while finding reviews")?; print_changes(&[repo.clone()]); - } + }, Commands::HelmChart { workspace } => { find_values_yaml(&octocrab, workspace.clone(), cli.base, cli.head) .await .context("while finding values.yaml files")?; - } + }, } Ok(()) @@ -132,15 +134,9 @@ async fn find_values_yaml( ) -> Result<(), anyhow::Error> { let repo = Repository::open(workspace).context("failed to open repository")?; - let base_ref = repo - .revparse_single(&base) - .context("can't parse base_ref")? - .id(); + let base_ref = repo.revparse_single(&base).context("can't parse base_ref")?.id(); - let head_ref = repo - .revparse_single(&head) - .context("can't parse head_ref")? - .id(); + let head_ref = repo.revparse_single(&head).context("can't parse head_ref")?.id(); // compare base and head ref against each other and generate the diff GitHub shows in the files tab in the PR let base_tree = repo @@ -171,23 +167,19 @@ async fn find_values_yaml( } let new_images_content = repo.find_blob(new_file.id())?; - let new_image_config: ContainerImages = - serde_yml::from_slice(new_images_content.content())?; + let new_image_config: ContainerImages = serde_yml::from_slice(new_images_content.content())?; let old_file = diff_delta.old_file(); if old_file.exists() { let old_images_content = repo.find_blob(old_file.id())?; - let old_image_config: ContainerImages = - serde_yml::from_slice(old_images_content.content())?; + let old_image_config: ContainerImages = serde_yml::from_slice(old_images_content.content())?; for (name, image) in &new_image_config.container_images { for source in &image.sources { changes.push(RepoChange { name: name.clone(), remote: source.repo.clone(), - base_commit: old_image_config.container_images[name].sources[0] - .commit - .clone(), + base_commit: old_image_config.container_images[name].sources[0].commit.clone(), head_commit: source.commit.clone(), changes: Vec::new(), }); @@ -209,10 +201,7 @@ async fn find_values_yaml( Ok(()) } -async fn find_reviews( - octocrab: &Arc, - repo: &mut RepoChange, -) -> Result<(), anyhow::Error> { +async fn find_reviews(octocrab: &Arc, repo: &mut RepoChange) -> Result<(), anyhow::Error> { let (repo_owner, repo_name) = parse_remote(&repo.remote).context("while parsing remote")?; let link_prefix = format!("https://github.com/{repo_owner}/{repo_name}"); @@ -343,10 +332,7 @@ fn print_changes(changes: &[RepoChange]) { } } -fn collect_approved_reviews( - pr_reviews: &[pulls::Review], - review: &mut Change, -) -> Result<(), anyhow::Error> { +fn collect_approved_reviews(pr_reviews: &[pulls::Review], review: &mut Change) -> Result<(), anyhow::Error> { for pr_review in pr_reviews { // TODO: do we need to check if this is the last review of the user? if pr_review.state.ok_or(anyhow!("review has no state"))? == ReviewState::Approved {