From 703d0d038178cbdf3016015cff9e7f8db390d630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sandro=20J=C3=A4ckel?= Date: Thu, 15 Aug 2024 16:23:03 +0200 Subject: [PATCH] Extract yaml file parsing into own function --- .rustfmt.toml | 1 - src/helm_config.rs | 13 +++++++++++++ src/main.rs | 12 ++++-------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index bfe7014..8941ec2 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -7,4 +7,3 @@ match_block_trailing_comma = true max_width = 120 overflow_delimited_expr = true reorder_impl_items = true -struct_field_align_threshold = 20 diff --git a/src/helm_config.rs b/src/helm_config.rs index c3d0b5b..107b65c 100644 --- a/src/helm_config.rs +++ b/src/helm_config.rs @@ -1,5 +1,7 @@ use std::collections::HashMap; +use anyhow::Context; +use git2::{DiffFile, Repository}; use serde::{Deserialize, Serialize}; #[allow(clippy::module_name_repetitions)] @@ -9,6 +11,17 @@ pub struct ImageRefs { pub container_images: HashMap, } +impl ImageRefs { + pub fn parse(repo: &Repository, diff_file: DiffFile) -> Result { + let blob_id = diff_file.id(); + let blob = repo + .find_blob(blob_id) + .with_context(|| format!("cannot find Git blob {blob_id}"))?; + Ok(serde_yml::from_slice(blob.content()) + .with_context(|| format!("cannot parse yaml file {:?}", diff_file.path()))?) + } +} + #[derive(Debug, Serialize, Deserialize)] pub struct ImageRef { pub account: String, diff --git a/src/main.rs b/src/main.rs index b3bfb63..817903b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -137,28 +137,24 @@ fn find_values_yaml(workspace: String, base: &str, head: &str) -> Result