refactor: flatten nested blocks with early returns

This commit is contained in:
Paul Campbell 2024-09-01 07:18:05 +01:00
parent f475095f4a
commit ca70c03e8b
7 changed files with 106 additions and 99 deletions

View file

@ -18,7 +18,9 @@ impl Handler<NotifyUser> for AlertsActor {
}; };
let net = self.net.clone(); let net = self.net.clone();
let shout = shout.clone(); let shout = shout.clone();
if let Some(user_notification) = self.history.sendable(msg.peel()) { let Some(user_notification) = self.history.sendable(msg.peel()) else {
return;
};
async move { async move {
if let Some(webhook_config) = shout.webhook() { if let Some(webhook_config) = shout.webhook() {
send_webhook(&user_notification, webhook_config, &net).await; send_webhook(&user_notification, webhook_config, &net).await;
@ -36,5 +38,4 @@ impl Handler<NotifyUser> for AlertsActor {
.into_actor(self) .into_actor(self)
.wait(ctx); .wait(ctx);
} }
}
} }

View file

@ -12,7 +12,9 @@ impl Handler<UnRegisterWebhook> for RepoActor {
type Result = (); type Result = ();
fn handle(&mut self, _msg: UnRegisterWebhook, ctx: &mut Self::Context) -> Self::Result { fn handle(&mut self, _msg: UnRegisterWebhook, ctx: &mut Self::Context) -> Self::Result {
if let Some(webhook_id) = self.webhook_id.take() { let Some(webhook_id) = self.webhook_id.take() else {
return;
};
self.update_tui(RepoUpdate::UnregisteringWebhook); self.update_tui(RepoUpdate::UnregisteringWebhook);
let forge = self.forge.duplicate(); let forge = self.forge.duplicate();
debug!("unregistering webhook"); debug!("unregistering webhook");
@ -27,5 +29,4 @@ impl Handler<UnRegisterWebhook> for RepoActor {
.wait(ctx); .wait(ctx);
debug!("unregistering webhook done"); debug!("unregistering webhook done");
} }
}
} }

View file

@ -59,8 +59,12 @@ impl Tui {
fn handle_input(&mut self, ctx: &mut <Self as actix::Actor>::Context) -> std::io::Result<()> { fn handle_input(&mut self, ctx: &mut <Self as actix::Actor>::Context) -> std::io::Result<()> {
if event::poll(std::time::Duration::from_millis(16))? { if event::poll(std::time::Duration::from_millis(16))? {
if let event::Event::Key(key) = event::read()? { let event::Event::Key(key) = event::read()? else {
if key.kind == KeyEventKind::Press { return Ok(());
};
if key.kind != KeyEventKind::Press {
return Ok(());
}
match key.code { match key.code {
KeyCode::Char('q') => { KeyCode::Char('q') => {
ctx.stop(); ctx.stop();
@ -85,8 +89,6 @@ impl Tui {
_ => (), _ => (),
} }
} }
}
}
Ok(()) Ok(())
} }
} }

View file

@ -5,7 +5,7 @@ use git_next_core::{ForgeAlias, RepoAlias};
use ratatui::{ use ratatui::{
buffer::Buffer, buffer::Buffer,
layout::{Alignment, Direction, Layout, Rect}, layout::{Alignment, Direction, Layout, Rect},
widgets::{block::Title, Block, Borders, Widget}, widgets::{block::Title, Block, Widget},
}; };
use crate::tui::{ use crate::tui::{

View file

@ -56,7 +56,8 @@ lazy_static::lazy_static! {
} }
impl From<LogLine> for Line<'_> { impl From<LogLine> for Line<'_> {
fn from(value: LogLine) -> Self { fn from(value: LogLine) -> Self {
if let Some(caps) = RE.captures(&value.raw) { match RE.captures(&value.raw) {
Some(caps) => {
let pre = caps["pre"].to_owned(); let pre = caps["pre"].to_owned();
let hash = caps["hash"].to_owned(); let hash = caps["hash"].to_owned();
let message = caps["message"].to_owned(); let message = caps["message"].to_owned();
@ -88,11 +89,13 @@ impl From<LogLine> for Line<'_> {
spans.push(message.into()); spans.push(message.into());
Line::from(spans) Line::from(spans)
} }
} else { }
None => {
// non-commit line // non-commit line
Line::from(value.raw.clone()) Line::from(value.raw.clone())
} }
} }
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -122,18 +122,17 @@ fn reset_next_to_main(
next: &git::Commit, next: &git::Commit,
next_branch: &BranchName, next_branch: &BranchName,
) -> Error { ) -> Error {
if let Err(err) = git::push::reset( match git::push::reset(
open_repository, open_repository,
repo_details, repo_details,
next_branch, next_branch,
&main.clone().into(), &main.clone().into(),
&git::push::Force::From(next.clone().into()), &git::push::Force::From(next.clone().into()),
) { ) {
Error::NonRetryable(format!( Ok(()) => Error::Retryable(format!("Branch {next_branch} has been reset")),
Err(err) => Error::NonRetryable(format!(
"Failed to reset branch '{next_branch}' to commit '{next}': {err}" "Failed to reset branch '{next_branch}' to commit '{next}': {err}"
)) )),
} else {
Error::Retryable(format!("Branch {next_branch} has been reset"))
} }
} }

View file

@ -24,12 +24,13 @@ pub async fn unregister(
None, None,
network::NetRequestLogging::None, network::NetRequestLogging::None,
); );
let result = net.delete(request).await; match net.delete(request).await {
if let Err(e) = result { Err(e) => {
tracing::warn!("Failed to unregister webhook"); tracing::warn!("Failed to unregister webhook");
return Err(git::forge::webhook::Error::FailedToUnregister( Err(git::forge::webhook::Error::FailedToUnregister(
e.to_string(), e.to_string(),
)); ))
}
_ => Ok(()),
} }
Ok(())
} }