Fix clippy warnings

This commit is contained in:
Brian Tarricone 2023-09-19 21:35:16 -07:00
parent e3fffe1814
commit bfd146de95
2 changed files with 24 additions and 29 deletions

View File

@ -19,7 +19,7 @@ use core::fmt;
use crate::config::PublishEvent; use crate::config::PublishEvent;
pub trait GitlabEventExt { pub trait GitlabEventExt {
fn should_publish(&self, publish_events: &Vec<PublishEvent>) -> bool; fn should_publish(&self, publish_events: &[PublishEvent]) -> bool;
fn project(&self) -> &Project; fn project(&self) -> &Project;
fn r#ref(&self) -> Option<&str>; fn r#ref(&self) -> Option<&str>;
fn user(&self) -> &str; fn user(&self) -> &str;
@ -264,7 +264,7 @@ macro_rules! find_publish_event {
} }
impl GitlabEventExt for GitlabEvent { impl GitlabEventExt for GitlabEvent {
fn should_publish(&self, publish_events: &Vec<PublishEvent>) -> bool { fn should_publish(&self, publish_events: &[PublishEvent]) -> bool {
match self { match self {
GitlabEvent::Push { r#ref, .. } => { GitlabEvent::Push { r#ref, .. } => {
if let Some(PublishEvent::Push { branches }) = if let Some(PublishEvent::Push { branches }) =
@ -274,7 +274,7 @@ impl GitlabEventExt for GitlabEvent {
None => true, None => true,
Some(branches) => { Some(branches) => {
let refname = parse_ref(r#ref); let refname = parse_ref(r#ref);
branches.iter().find(|branch| branch.find(&refname).is_some()).is_some() branches.iter().any(|branch| branch.find(&refname).is_some())
} }
} }
} else { } else {
@ -324,19 +324,19 @@ impl GitlabEventExt for GitlabEvent {
fn project(&self) -> &Project { fn project(&self) -> &Project {
match self { match self {
GitlabEvent::Push { project, .. } => &project, GitlabEvent::Push { project, .. } => project,
GitlabEvent::TagPush { project, .. } => &project, GitlabEvent::TagPush { project, .. } => project,
GitlabEvent::Issue { project, .. } => project, GitlabEvent::Issue { project, .. } => project,
GitlabEvent::MergeRequest { project, .. } => &project, GitlabEvent::MergeRequest { project, .. } => project,
GitlabEvent::Pipeline { project, .. } => &project, GitlabEvent::Pipeline { project, .. } => project,
GitlabEvent::Other => unreachable!("Unsupported event type"), GitlabEvent::Other => unreachable!("Unsupported event type"),
} }
} }
fn r#ref(&self) -> Option<&str> { fn r#ref(&self) -> Option<&str> {
match self { match self {
GitlabEvent::Push { r#ref, .. } => Some(&r#ref), GitlabEvent::Push { r#ref, .. } => Some(r#ref),
GitlabEvent::TagPush { r#ref, .. } => Some(&r#ref), GitlabEvent::TagPush { r#ref, .. } => Some(r#ref),
GitlabEvent::Issue { .. } => None, GitlabEvent::Issue { .. } => None,
GitlabEvent::MergeRequest { object_attributes, .. } => Some(&object_attributes.target_branch), GitlabEvent::MergeRequest { object_attributes, .. } => Some(&object_attributes.target_branch),
GitlabEvent::Pipeline { object_attributes, .. } => Some(&object_attributes.r#ref), GitlabEvent::Pipeline { object_attributes, .. } => Some(&object_attributes.r#ref),
@ -346,8 +346,8 @@ impl GitlabEventExt for GitlabEvent {
fn user(&self) -> &str { fn user(&self) -> &str {
match self { match self {
GitlabEvent::Push { user_name, .. } => &user_name, GitlabEvent::Push { user_name, .. } => user_name,
GitlabEvent::TagPush { user_name, .. } => &user_name, GitlabEvent::TagPush { user_name, .. } => user_name,
GitlabEvent::Issue { user, .. } => &user.name, GitlabEvent::Issue { user, .. } => &user.name,
GitlabEvent::MergeRequest { user, .. } => &user.name, GitlabEvent::MergeRequest { user, .. } => &user.name,
GitlabEvent::Pipeline { user, .. } => &user.name, GitlabEvent::Pipeline { user, .. } => &user.name,
@ -356,7 +356,7 @@ impl GitlabEventExt for GitlabEvent {
} }
fn titles(&self) -> Vec<String> { fn titles(&self) -> Vec<String> {
fn find_commit<'a>(commits: &'a Vec<Commit>, sha: &str) -> Option<&'a Commit> { fn find_commit<'a>(commits: &'a [Commit], sha: &str) -> Option<&'a Commit> {
commits.iter().find(|commit| commit.id == sha) commits.iter().find(|commit| commit.id == sha)
} }
@ -395,7 +395,7 @@ impl GitlabEventExt for GitlabEvent {
} => { } => {
let title = format!( let title = format!(
"**tagged** {}", "**tagged** {}",
find_commit(commits, &checkout_sha) find_commit(commits, checkout_sha)
.map(|commit| &commit.title) .map(|commit| &commit.title)
.unwrap_or(checkout_sha) .unwrap_or(checkout_sha)
); );
@ -433,7 +433,7 @@ impl GitlabEventExt for GitlabEvent {
let title = object_attributes let title = object_attributes
.name .name
.as_ref() .as_ref()
.map(|n| n.clone()) .cloned()
.or(merge_request.as_ref().map(|mr| mr.title.clone())) .or(merge_request.as_ref().map(|mr| mr.title.clone()))
.iter() .iter()
.fold(format!("Pipeline **{}**", object_attributes.status), |accum, title| { .fold(format!("Pipeline **{}**", object_attributes.status), |accum, title| {
@ -474,7 +474,7 @@ mod test {
use std::{fs::File, io::BufReader}; use std::{fs::File, io::BufReader};
fn load_test_data(name: &str) -> anyhow::Result<GitlabEvent> { fn load_test_data(name: &str) -> anyhow::Result<GitlabEvent> {
let f = File::open(&format!("{}/test-data/{}.json", env!("CARGO_MANIFEST_DIR"), name))?; let f = File::open(format!("{}/test-data/{}.json", env!("CARGO_MANIFEST_DIR"), name))?;
let r = BufReader::new(f); let r = BufReader::new(f);
let event: GitlabEvent = serde_json::from_reader(r)?; let event: GitlabEvent = serde_json::from_reader(r)?;
Ok(event) Ok(event)

View File

@ -83,37 +83,32 @@ async fn ensure_matrix_room_joined(matrix_client: &Client, room_id: &OwnedRoomOr
|| a_room || a_room
.canonical_alias() .canonical_alias()
.iter() .iter()
.find(|alias| alias.as_str() == our_room_str) .any(|alias| alias.as_str() == our_room_str)
.is_some() || a_room.alt_aliases().iter().any(|alias| alias.as_str() == our_room_str)
|| a_room
.alt_aliases()
.iter()
.find(|alias| alias.as_str() == our_room_str)
.is_some()
} }
let mut room = matrix_client let mut room = matrix_client
.joined_rooms() .joined_rooms()
.iter() .iter()
.find(|a_room| room_matches(*a_room, room_id)) .find(|a_room| room_matches(a_room, room_id))
.map(|room| room.clone()); .cloned();
if room.is_none() { if room.is_none() {
if let Some(invited) = matrix_client if let Some(invited) = matrix_client
.invited_rooms() .invited_rooms()
.iter() .iter()
.find(|a_room| room_matches(*a_room, room_id)) .find(|a_room| room_matches(a_room, room_id))
{ {
invited.accept_invitation().await?; invited.accept_invitation().await?;
} else { } else {
matrix_client.join_room_by_id_or_alias(room_id, &[]).await?; matrix_client.join_room_by_id_or_alias(room_id, &[]).await?;
} }
let settings = build_sync_settings(&matrix_client).await; let settings = build_sync_settings(matrix_client).await;
matrix_client.sync_once(settings).await?; matrix_client.sync_once(settings).await?;
room = matrix_client room = matrix_client
.joined_rooms() .joined_rooms()
.iter() .iter()
.find(|a_room| room_matches(*a_room, room_id)) .find(|a_room| room_matches(a_room, room_id))
.map(|room| room.clone()); .cloned();
} }
room.ok_or_else(|| anyhow!("Unable to join room {}", room_id)) room.ok_or_else(|| anyhow!("Unable to join room {}", room_id))
@ -162,7 +157,7 @@ async fn run() -> anyhow::Result<()> {
let addr = config let addr = config
.bind_address .bind_address
.as_ref() .as_ref()
.map(|ba| ba.clone()) .cloned()
.unwrap_or_else(|| "127.0.0.1".to_string()) .unwrap_or_else(|| "127.0.0.1".to_string())
.parse::<IpAddr>() .parse::<IpAddr>()
.context("Failed to parse bind_address")?; .context("Failed to parse bind_address")?;