From 77a07013a9d4bf32aed1f837edefa83dfd6b50f1 Mon Sep 17 00:00:00 2001 From: Sergio <77530549+sergi0g@users.noreply.github.com> Date: Wed, 19 Mar 2025 19:20:29 +0200 Subject: [PATCH] refactor: use array slices instead of vectors wherever possible --- src/check.rs | 8 ++++---- src/http.rs | 8 ++++---- src/registry.rs | 25 ++++++++++++++----------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/check.rs b/src/check.rs index ee3a39e..01d19dc 100644 --- a/src/check.rs +++ b/src/check.rs @@ -26,10 +26,10 @@ async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Ve let json_url = base_url.clone() + "json"; if refresh { let refresh_url = base_url + "refresh"; - match client.get(&(&refresh_url), vec![], false).await { + match client.get(&refresh_url, &[], false).await { Ok(response) => { if response.status() != 200 { - ctx.logger.warn(format!("GET {}: Failed to refresh server. Server returned invalid response code: {}",refresh_url,response.status())); + ctx.logger.warn(format!("GET {}: Failed to refresh server. Server returned invalid response code: {}", refresh_url, response.status())); return Vec::new(); } }, @@ -40,10 +40,10 @@ async fn get_remote_updates(ctx: &Context, client: &Client, refresh: bool) -> Ve } } - match client.get(&json_url, vec![], false).await { + match client.get(&json_url, &[], false).await { Ok(response) => { if response.status() != 200 { - ctx.logger.warn(format!("GET {}: Failed to fetch updates from server. Server returned invalid response code: {}",json_url,response.status())); + ctx.logger.warn(format!("GET {}: Failed to fetch updates from server. Server returned invalid response code: {}", json_url, response.status())); return Vec::new(); } let json = parse_json(&get_response_body(response).await); diff --git a/src/http.rs b/src/http.rs index f520d3a..0a1b521 100644 --- a/src/http.rs +++ b/src/http.rs @@ -42,7 +42,7 @@ impl Client { &self, url: &str, method: RequestMethod, - headers: Vec<(&str, Option<&str>)>, + headers: &[(&str, Option<&str>)], ignore_401: bool, ) -> Result { let mut request = match method { @@ -51,7 +51,7 @@ impl Client { }; for (name, value) in headers { if let Some(v) = value { - request = request.header(name, v) + request = request.header(*name, *v) } } match request.send().await { @@ -114,7 +114,7 @@ impl Client { pub async fn get( &self, url: &str, - headers: Vec<(&str, Option<&str>)>, + headers: &[(&str, Option<&str>)], ignore_401: bool, ) -> Result { self.request(url, RequestMethod::GET, headers, ignore_401) @@ -124,7 +124,7 @@ impl Client { pub async fn head( &self, url: &str, - headers: Vec<(&str, Option<&str>)>, + headers: &[(&str, Option<&str>)], ) -> Result { self.request(url, RequestMethod::HEAD, headers, false).await } diff --git a/src/registry.rs b/src/registry.rs index 376b6ec..e0ddb08 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -22,7 +22,7 @@ use crate::{ pub async fn check_auth(registry: &str, ctx: &Context, client: &Client) -> Option { let protocol = get_protocol(registry, &ctx.config.registries); let url = format!("{}://{}/v2/", protocol, registry); - let response = client.get(&url, Vec::new(), true).await; + let response = client.get(&url, &[], true).await; match response { Ok(response) => { let status = response.status(); @@ -57,9 +57,9 @@ pub async fn get_latest_digest( protocol, &image.parts.registry, &image.parts.repository, &image.parts.tag ); let authorization = to_bearer_string(&token); - let headers = vec![("Accept", Some("application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.index.v1+json")), ("Authorization", authorization.as_deref())]; + let headers = [("Accept", Some("application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.docker.distribution.manifest.v2+json, application/vnd.oci.image.index.v1+json")), ("Authorization", authorization.as_deref())]; - let response = client.head(&url, headers).await; + let response = client.head(&url, &headers).await; let time = start.elapsed().unwrap().as_millis() as u32; ctx.logger.debug(format!( "Checked for digest update to {} in {}ms", @@ -95,7 +95,7 @@ pub async fn get_latest_digest( } pub async fn get_token( - images: &Vec<&Image>, + images: &[&Image], auth_url: &str, credentials: &Option, client: &Client, @@ -105,9 +105,9 @@ pub async fn get_token( url = format!("{}&scope=repository:{}:pull", url, image.parts.repository); } let authorization = credentials.as_ref().map(|creds| format!("Basic {}", creds)); - let headers = vec![("Authorization", authorization.as_deref())]; + let headers = [("Authorization", authorization.as_deref())]; - let response = client.get(&url, headers, false).await; + let response = client.get(&url, &headers, false).await; let response_json = match response { Ok(response) => parse_json(&get_response_body(response).await), Err(_) => error!("GET {}: Request failed!", url), @@ -131,7 +131,7 @@ pub async fn get_latest_tag( protocol, &image.parts.registry, &image.parts.repository, ); let authorization = to_bearer_string(&token); - let headers = vec![ + let headers = [ ("Accept", Some("application/json")), ("Authorization", authorization.as_deref()), ]; @@ -147,7 +147,7 @@ pub async fn get_latest_tag( )); let (new_tags, next) = match get_extra_tags( &next_url.unwrap(), - headers.clone(), + &headers, base, &image.version_info.as_ref().unwrap().format_str, client, @@ -205,18 +205,21 @@ pub async fn get_latest_tag( } } } - None => error!("Image {} has no remote version tags! Local tag: {}", image.reference, image.parts.tag), + None => error!( + "Image {} has no remote version tags! Local tag: {}", + image.reference, image.parts.tag + ), } } pub async fn get_extra_tags( url: &str, - headers: Vec<(&str, Option<&str>)>, + headers: &[(&str, Option<&str>)], base: &Version, format_str: &str, client: &Client, ) -> Result<(Vec, Option), String> { - let response = client.get(url, headers, false).await; + let response = client.get(url, &headers, false).await; match response { Ok(res) => {