Skip to content
Merged

dev #56

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod forge {
mod api_type;
mod http_client;

pub(crate) use api_type::function;
pub use api_type::{ApiType, guess_api_type_from_host};
pub use http_client::HttpClient;
}
Expand Down
22 changes: 10 additions & 12 deletions src/cli/browse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use clap::Args;

use crate::{
cli::{
config::Config,
config::{self, Config},
forge::{self, ApiType, gitea, github, gitlab},
},
forge,
git::{self, GitRemoteData},
merge_config_into_args,
};

const DEFAULT_REMOTE: &str = "origin";
Expand Down Expand Up @@ -81,7 +79,7 @@ pub fn browse_repository(mut args: BrowseCommandArgs) -> anyhow::Result<()> {
let remote = git::get_remote_data(&remote_name)
.with_context(|| format!("Failed to get remote URL for remote '{}'", &remote_name))?;

merge_config_into_args!(&config, args, Some(&remote), "browse", [api, no_browser]);
config::merge_config_into_args!(&config, args, Some(&remote), "browse", [api, no_browser]);

let api_type = match args.api {
Some(api_type) => api_type,
Expand Down Expand Up @@ -125,7 +123,7 @@ pub fn browse_repository(mut args: BrowseCommandArgs) -> anyhow::Result<()> {
}

fn browse_home(remote: &GitRemoteData, api_type: &ApiType, no_browser: bool) -> anyhow::Result<()> {
let get_home_url = forge!(api_type, get_url_for_home);
let get_home_url = forge::function!(api_type, get_url_for_home);
let url = get_home_url(remote);

print_or_open(&url, no_browser)
Expand All @@ -137,7 +135,7 @@ fn browse_commitish(
commit_ish: &str,
no_browser: bool,
) -> anyhow::Result<()> {
let get_commit_url = forge!(api_type, get_url_for_commit);
let get_commit_url = forge::function!(api_type, get_url_for_commit);
let commit = git::rev_parse(commit_ish)
.with_context(|| format!("Failed to resolve commit-ish: {commit_ish}"))?;
let url = get_commit_url(remote, &commit);
Expand All @@ -151,7 +149,7 @@ fn browse_issue(
issue_number: u32,
no_browser: bool,
) -> anyhow::Result<()> {
let get_issue_url = forge!(api_type, get_url_for_issue);
let get_issue_url = forge::function!(api_type, get_url_for_issue);
let url = get_issue_url(remote, issue_number);

print_or_open(&url, no_browser)
Expand All @@ -162,7 +160,7 @@ fn browse_issues(
api_type: &ApiType,
no_browser: bool,
) -> anyhow::Result<()> {
let get_issues_url = forge!(api_type, get_url_for_issues);
let get_issues_url = forge::function!(api_type, get_url_for_issues);
let url = get_issues_url(remote);

print_or_open(&url, no_browser)
Expand All @@ -174,14 +172,14 @@ fn browse_pr(
pr_number: u32,
no_browser: bool,
) -> anyhow::Result<()> {
let get_pr_url = forge!(api_type, get_url_for_pr);
let get_pr_url = forge::function!(api_type, get_url_for_pr);
let url = get_pr_url(remote, pr_number);

print_or_open(&url, no_browser)
}

fn browse_prs(remote: &GitRemoteData, api_type: &ApiType, no_browser: bool) -> anyhow::Result<()> {
let get_prs_url = forge!(api_type, get_url_for_prs);
let get_prs_url = forge::function!(api_type, get_url_for_prs);
let url = get_prs_url(remote);

print_or_open(&url, no_browser)
Expand All @@ -192,7 +190,7 @@ fn browse_releases(
api_type: &ApiType,
no_browser: bool,
) -> anyhow::Result<()> {
let get_releases_url = forge!(api_type, get_url_for_releases);
let get_releases_url = forge::function!(api_type, get_url_for_releases);
let url = get_releases_url(remote);

print_or_open(&url, no_browser)
Expand Down Expand Up @@ -222,7 +220,7 @@ fn browse_path(
.strip_prefix(git::get_absolute_repo_root()?)
.context("The specified file is not within the current git repository")?;
let file_path = path_with_forward_slashes(file_path);
let get_path_url = forge!(api_type, get_url_for_path);
let get_path_url = forge::function!(api_type, get_url_for_path);
let commit = match commit_ish {
Some(c) => {
&git::rev_parse(c).with_context(|| format!("Failed to resolve commit-ish: {c}"))?
Expand Down
8 changes: 4 additions & 4 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,14 @@ impl<'a> ConfigSource<'a> {
/// );
/// }
/// ```
#[macro_export]
macro_rules! merge_config_into_args {
($config:expr, $args:expr, $remote:expr, $command_path:literal, [$($field:ident),* $(,)?]$(,)?) => {
$(
{
let field_name = stringify!($field).replace('_', "-");
let config_path = format!("{}/{}", $command_path, field_name);

$crate::cli::config::__macro_internals::MergeConfigIntoArg::__merge_with_config(
$crate::cli::config::macro_internals::MergeConfigIntoArg::__merge_with_config(
&mut $args.$field,
$config,
&config_path,
Expand All @@ -443,11 +442,12 @@ macro_rules! merge_config_into_args {
};
}

pub(crate) use merge_config_into_args;

/// Internal module for macro implementation details.
///
/// This module is public only for macro access but hidden from documentation.
#[doc(hidden)]
pub mod __macro_internals {
pub(crate) mod macro_internals {
use super::{ApiType, Config, GitRemoteData, IssueState, OutputFormat, PrState};
use clap::ValueEnum;

Expand Down
7 changes: 4 additions & 3 deletions src/cli/forge/api_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ pub fn guess_api_type_from_host(host: &str) -> anyhow::Result<ApiType> {
///
/// ```rust,ignore
/// let api_type = ApiType::GitHub;
/// let get_issues = forge!(api_type, get_issues);
/// let get_issues = forge::function!(api_type, get_issues);
///
/// assert_eq!(get_issues, crate::cli::forge::github::get_issues);
/// ```
#[macro_export]
macro_rules! forge {
macro_rules! function {
($type:expr, $fn_name:ident) => {
match $type {
ApiType::GitHub => github::$fn_name,
Expand All @@ -48,6 +47,8 @@ macro_rules! forge {
};
}

pub(crate) use function;

#[cfg(test)]
mod tests {
use super::*;
Expand Down
21 changes: 9 additions & 12 deletions src/cli/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use dialoguer::Input;
use serde::{Deserialize, Serialize};

use crate::{
build_fetch_options,
cli::{
config::Config,
config::{self, Config},
forge::{self, ApiType, HttpClient, gitea, github, gitlab},
},
forge,
git::{self, GitRemoteData},
io::{self, OutputFormat},
merge_config_into_args,
tui::{self, FetchOptions, ListableItem},
};

Expand Down Expand Up @@ -242,7 +239,7 @@ pub fn list_issues(mut args: IssueListCommandArgs) -> anyhow::Result<()> {
let remote = git::get_remote_data(&remote_name)
.with_context(|| format!("Failed to parse remote URL for remote '{}'", &remote_name))?;

merge_config_into_args!(
config::merge_config_into_args!(
&config,
args,
Some(&remote),
Expand Down Expand Up @@ -301,7 +298,7 @@ pub fn create_issue(mut args: IssueCreateCommandArgs) -> anyhow::Result<()> {
let remote = git::get_remote_data(&remote_name)
.with_context(|| format!("Failed to parse remote URL for remote '{}'", &remote_name))?;

merge_config_into_args!(
config::merge_config_into_args!(
&config,
args,
Some(&remote),
Expand Down Expand Up @@ -355,7 +352,7 @@ pub fn create_issue(mut args: IssueCreateCommandArgs) -> anyhow::Result<()> {
// =============================================================================

fn list_issues_in_web_browser(remote: &GitRemoteData, api_type: &ApiType) -> anyhow::Result<()> {
let get_issues_url = forge!(api_type, get_url_for_issues);
let get_issues_url = forge::function!(api_type, get_url_for_issues);

open::that(get_issues_url(remote))?;

Expand All @@ -371,7 +368,7 @@ fn list_issues_to_stdout(
output_format: &OutputFormat,
use_auth: bool,
) -> anyhow::Result<()> {
let get_issues = forge!(api_type, get_issues);
let get_issues = forge::function!(api_type, get_issues);
let response = get_issues(&HttpClient::new(), remote, api_url, filters, use_auth)
.context("Failed fetching issues")?;

Expand All @@ -393,7 +390,7 @@ fn list_issues_interactively(
api_type: ApiType,
args: IssueListCommandArgs,
) -> anyhow::Result<()> {
let fetch_options = build_fetch_options! {
let fetch_options = tui::build_fetch_options! {
"assignee": args.assignee,
"author": args.author,
"labels": args.labels,
Expand Down Expand Up @@ -436,7 +433,7 @@ fn select_issue_interactively(
per_page: u32,
use_auth: bool,
) -> anyhow::Result<Issue> {
let get_issues = forge!(api_type, get_issues);
let get_issues = forge::function!(api_type, get_issues);
let http_client = HttpClient::new();

tui::select_item_with(initial_options, move |page, options, result| {
Expand Down Expand Up @@ -469,7 +466,7 @@ fn select_issue_interactively(
}

fn create_issue_via_browser(remote: &GitRemoteData, api_type: &ApiType) -> anyhow::Result<()> {
let url = forge!(api_type, get_url_for_issue_creation)(remote);
let url = forge::function!(api_type, get_url_for_issue_creation)(remote);

open::that(url)?;

Expand Down Expand Up @@ -512,7 +509,7 @@ fn create_issue_via_api(
no_browser: bool,
) -> anyhow::Result<()> {
let http_client = HttpClient::new();
let create_issue = forge!(api_type, create_issue);
let create_issue = forge::function!(api_type, create_issue);
let issue = create_issue(&http_client, remote, api_url, create_options)?;

if no_browser {
Expand Down
25 changes: 11 additions & 14 deletions src/cli/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ use dialoguer::Input;
use serde::{Deserialize, Serialize};

use crate::{
build_fetch_options,
cli::{
config::Config,
config::{self, Config},
forge::{self, ApiType, HttpClient, gitea, github, gitlab},
},
forge,
git::{self, GitRemoteData},
io::{self, OutputFormat},
merge_config_into_args,
tui::{self, FetchOptions, ListableItem},
};

Expand Down Expand Up @@ -336,7 +333,7 @@ pub fn list_prs(mut args: PrListCommandArgs) -> anyhow::Result<()> {
let remote = git::get_remote_data(&remote_name)
.with_context(|| format!("Failed to parse remote URL for remote '{}'", &remote_name))?;

merge_config_into_args!(
config::merge_config_into_args!(
&config,
args,
Some(&remote),
Expand Down Expand Up @@ -395,7 +392,7 @@ pub fn checkout_pr(mut args: PrCheckoutCommandArgs) -> anyhow::Result<()> {
});
let remote_result = git::get_remote_data(&remote_name);

merge_config_into_args!(
config::merge_config_into_args!(
&config,
args,
remote_result.as_ref().ok(),
Expand All @@ -414,12 +411,12 @@ pub fn checkout_pr(mut args: PrCheckoutCommandArgs) -> anyhow::Result<()> {
),
},
};
let get_pr_ref = forge!(api_type, get_pr_ref);
let get_pr_ref = forge::function!(api_type, get_pr_ref);
let pr_number = match args.number {
Some(nr) => nr,
None => {
let remote = remote_result?;
let fetch_options = build_fetch_options! {
let fetch_options = tui::build_fetch_options! {
"author": args.author,
"draft": args.draft,
"labels": args.labels,
Expand Down Expand Up @@ -466,7 +463,7 @@ pub fn create_pr(mut args: PrCreateCommandArgs) -> anyhow::Result<()> {
let remote = git::get_remote_data(&remote_name)
.with_context(|| format!("Failed to parse remote URL for remote '{}'", &remote_name))?;

merge_config_into_args!(
config::merge_config_into_args!(
&config,
args,
Some(&remote),
Expand Down Expand Up @@ -512,7 +509,7 @@ pub fn create_pr(mut args: PrCreateCommandArgs) -> anyhow::Result<()> {
git::push_branch(&current_branch, &remote_name, true)?;
}

let create_pr = forge!(api_type, create_pr);
let create_pr = forge::function!(api_type, create_pr);

let (title, body) = if args.editor {
get_title_and_body_for_pr_for_editor_flag(
Expand Down Expand Up @@ -683,7 +680,7 @@ fn get_title_and_body_for_pr_for_fill_verbose_flag(
}

fn list_prs_in_web_browser(remote: &GitRemoteData, api_type: &ApiType) -> anyhow::Result<()> {
let get_prs_url = forge!(api_type, get_url_for_prs);
let get_prs_url = forge::function!(api_type, get_url_for_prs);

open::that(get_prs_url(remote))?;

Expand All @@ -699,7 +696,7 @@ fn list_prs_to_stdout(
output_format: &OutputFormat,
use_auth: bool,
) -> anyhow::Result<()> {
let get_prs = forge!(api_type, get_prs);
let get_prs = forge::function!(api_type, get_prs);
let response = get_prs(&HttpClient::new(), remote, api_url, filters, use_auth)?;

let fields = if fields.is_empty() {
Expand All @@ -720,7 +717,7 @@ fn list_prs_interactively(
api_type: ApiType,
args: PrListCommandArgs,
) -> anyhow::Result<()> {
let fetch_options = build_fetch_options!(
let fetch_options = tui::build_fetch_options!(
"author": args.author,
"draft": args.draft,
"labels": args.labels,
Expand Down Expand Up @@ -763,7 +760,7 @@ fn select_pr_interactively(
per_page: u32,
use_auth: bool,
) -> anyhow::Result<Pr> {
let get_prs = forge!(api_type, get_prs);
let get_prs = forge::function!(api_type, get_prs);

let http_client = HttpClient::new();

Expand Down
Loading