Skip to content

Commit 57531ee

Browse files
committed
refactor: pull request number is now a newtype PrNumber(NonZeroU32)
1 parent b19bc2d commit 57531ee

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clap::{
99

1010
use crate::{
1111
commands,
12-
config::{BranchName, Commit, Remote},
12+
config::{BranchName, Commit, PrNumber, Remote},
1313
};
1414

1515
/// A tool which makes it easy to declaratively manage personal forks by automatically merging pull requests
@@ -45,7 +45,7 @@ pub enum Command {
4545
/// Fetch pull request for a GitHub repository as a local branch
4646
PrFetch {
4747
/// Fetch PR of this number
48-
pr: u32,
48+
pr: PrNumber,
4949
/// The remote branch in the format `repo-owner/repo/branch`
5050
///
5151
/// The final part (`/branch`) is optional and defaults to `main`

src/commands/pr_fetch.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
use anyhow::{Context as _, anyhow};
44
use colored::Colorize as _;
55

6-
use crate::config::{BranchName, Commit, Remote};
6+
use crate::config::{BranchName, Commit, PrNumber, Remote};
77
use crate::git::{fetch_pull_request, git};
88

99
/// Fetch the given `pr` of `remote` at `commit` and store it in local `branch`
1010
///
1111
/// If `checkout`, `--checkout` the `branch`
1212
pub async fn pr_fetch(
13-
pr: u32,
13+
pr: PrNumber,
1414
remote: Option<Remote>,
1515
branch: Option<BranchName>,
1616
commit: Option<Commit>,

src/config.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use anyhow::{anyhow, bail};
44
use itertools::Itertools;
55
use nutype::nutype;
6-
use std::{convert::Infallible, str::FromStr};
6+
use std::{convert::Infallible, num::NonZeroU32, str::FromStr};
77
use tap::Pipe as _;
88

99
use indexmap::IndexSet;
@@ -99,7 +99,7 @@ impl FromStr for Remote {
9999
#[derive(Debug, Eq, PartialEq)]
100100
pub struct PullRequest {
101101
/// Number of the pull request
102-
pub number: u32,
102+
pub number: PrNumber,
103103
/// Commit to checkout of the pull request. If none, uses the latest commit
104104
pub commit: Option<Commit>,
105105
}
@@ -113,16 +113,13 @@ impl FromStr for PullRequest {
113113
commit,
114114
}) = s.parse::<Ref>();
115115

116-
let pr_number = pr_number
116+
let number = pr_number
117117
.strip_prefix('#')
118118
.unwrap_or(&pr_number)
119-
.parse::<u32>()
119+
.parse()
120120
.map_err(|err| anyhow!("invalid PR number: {pr_number}: {err}"))?;
121121

122-
Ok(Self {
123-
number: pr_number,
124-
commit,
125-
})
122+
Ok(Self { number, commit })
126123
}
127124
}
128125

@@ -187,6 +184,10 @@ impl FromStr for Ref {
187184
}
188185
}
189186

187+
/// Number of a pull request
188+
#[nutype(const_fn, derive(Eq, PartialEq, Display, Debug, FromStr, Copy, Clone))]
189+
pub struct PrNumber(NonZeroU32);
190+
190191
/// Name of a branch in git
191192
#[nutype(
192193
validate(not_empty),
@@ -327,26 +328,32 @@ patches = ['remove-tab']"#;
327328

328329
let conf = toml::from_str::<Config>(config).unwrap();
329330

331+
macro_rules! pr_number {
332+
($num:literal) => {
333+
PrNumber::new(const { ::std::num::NonZeroU32::new($num).expect("nonzero") })
334+
};
335+
}
336+
330337
pretty_assertions::assert_eq!(
331338
conf,
332339
Config {
333340
local_branch: BranchName::try_new("patchy".to_string()).unwrap(),
334341
patches: indexset!["remove-tab".to_string()],
335342
pull_requests: vec![
336343
PullRequest {
337-
number: 10000,
344+
number: pr_number!(10000),
338345
commit: None
339346
},
340347
PullRequest {
341-
number: 10000,
348+
number: pr_number!(10000),
342349
commit: None
343350
},
344351
PullRequest {
345-
number: 454,
352+
number: pr_number!(454),
346353
commit: Some(Commit::try_new("a1b2c3").unwrap())
347354
},
348355
PullRequest {
349-
number: 1,
356+
number: pr_number!(1),
350357
commit: Some(Commit::try_new("a1b2c3").unwrap())
351358
},
352359
],

src/git.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! - Extract into a separate module, put it behind some more nice API
55
//! - Use `gix`? Or anyways, we could go without spawning an entire process each
66
//! time we want to interact with Git
7-
use crate::config::{BranchName, Commit};
7+
use crate::config::{BranchName, Commit, PrNumber};
88
use std::path::{Path, PathBuf};
99
use std::process::{self, Output};
1010
use std::sync::LazyLock;
@@ -190,7 +190,7 @@ pub fn merge_into_main(
190190
/// Merge the `pull_request` into patchy's branch
191191
pub fn merge_pull_request(
192192
info: &RemoteBranch,
193-
pull_request: u32,
193+
pull_request: PrNumber,
194194
pr_title: &str,
195195
pr_url: &str,
196196
) -> Result<()> {
@@ -345,7 +345,7 @@ pub async fn fetch_branch(remote: &crate::config::Remote) -> Result<(Repo, Remot
345345
/// the branch name is generated if not supplied
346346
pub async fn fetch_pull_request(
347347
repo: &str,
348-
pull_request: u32,
348+
pull_request: PrNumber,
349349
custom_branch_name: Option<BranchName>,
350350
commit_hash: Option<&Commit>,
351351
) -> Result<(GitHubResponse, RemoteBranch)> {

0 commit comments

Comments
 (0)